我是Android和Java编程的新手。我必须在未来做一个更大的项目女巫包括一些android开发。所以我看了一些视频教程,现在我试着做一些应用程序来训练自己。现在我想用UDP在两个Android设备(摩托罗拉Defy和三星Galaxy Tab 2 7.0)之间进行聊天服务。
这是我的两个设备的代码:
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;
import java.net.UnknownHostException;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class udpserver extends Activity implements View.OnClickListener {
Button Send;
EditText IPAdresse;
EditText TEXT;
TextView RXtext,tstep,rstep;
private static final int TIMEOUT_MS = 1000;
private static final int server_port = 13011;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.udpserver);
IPAdresse = (EditText) findViewById(R.id.etIPAdresse);
TEXT = (EditText) findViewById(R.id.etTEXT);
Send = (Button) findViewById(R.id.bSendaa);
RXtext = (TextView) findViewById(R.id.tvRXtext);
tstep = (TextView) findViewById(R.id.tvTstep);
rstep = (TextView) findViewById(R.id.tvRstep);
Send.setOnClickListener(this);
String text;
byte[] message = new byte[1500];
DatagramSocket s;
//while(true){
try {
s= new DatagramSocket(server_port);
rstep.setText("1");
s.setBroadcast(true);
rstep.setText("2");
s.setSoTimeout(TIMEOUT_MS);
rstep.setText("3");
while(true){
DatagramPacket p = new DatagramPacket(message, message.length);
rstep.setText("4");
//InetAddress test = InetAddress.getByName("192.168.1.101");
// rstep.setText("5");
//s.connect(test,12345);
// rstep.setText("6");
s.receive(p);
rstep.setText("xxx");
text = new String(message, 0, p.getLength());
RXtext.setText(text);
}
} catch (SocketException e) {
// TODO Auto-generated catch block
e.printStackTrace();
rstep.setText("fail socket create");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
rstep.setText("fail receive");
}
}
//}
public void onClick(View arg0) {
// TODO Auto-generated method stub
switch(arg0.getId()){
case R.id.bSendaa:
tstep.setText("1");
String messageStr= TEXT.getText().toString();
tstep.setText("2");
DatagramSocket s;
try {
s = new DatagramSocket();
tstep.setText("3");
s.setBroadcast(true);
tstep.setText("4");
s.setSoTimeout(TIMEOUT_MS);
tstep.setText("5");
InetAddress local = InetAddress.getByName(IPAdresse.getText().toString());
tstep.setText("6");
int msg_length=messageStr.length();
tstep.setText("7");
byte[] message = messageStr.getBytes();
tstep.setText("8");
DatagramPacket p = new DatagramPacket(message, msg_length,local,server_port);
tstep.setText("9");
s.connect(local,server_port);
tstep.setText("10");
s.send(p);
tstep.setText("sending complete");
} catch (SocketException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (UnknownHostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
tstep.setText("sending failed");
}
}
}
}
我的用户界面看起来像这样(有翻译)
我现在的问题是:我只能在激活开始时收到1秒(1s = TIMOUT_MS)。但我希望得到一整个时间。我试过使用while(true)循环,但应用程序总是崩溃。
提前致谢!!! (对不起我的英语,我知道它不是最好的:))
答案 0 :(得分:0)
发生的事情就是当你这么做的时候(真的)你会陷入无限循环。这就是你的应用程序粉碎的原因。你需要做的是开始一个新的thread来做s.receive(p)。类似的东西:
new Thread(new Runnable() {
public void run() {
while(true){
DatagramPacket p = new DatagramPacket(message, message.length);
rstep.setText("4");
//InetAddress test = InetAddress.getByName("192.168.1.101");
// rstep.setText("5");
//s.connect(test,12345);
// rstep.setText("6");
s.receive(p);
rstep.setText("xxx");
text = new String(message, 0, p.getLength());
runOnUiThread(new Runnable() {
@Override
public void run() {
RXtext.setText(text);
}
});
}
}).start();