我正在为 android 开发 P2P 聊天/共享应用程序。以下代码用于接收 UDP 数据包,但仅接收并显示第一个消息/数据包。(发送代码没有问题。) 不显示后续聊天消息。chathistory.setText(new String.... 执行后while循环似乎退出了。 eg:chathistory.setText("test"); 没有执行。谁能指出错误?没有抛出异常。
public void receive() throws Exception
{
(new Thread(new Runnable() {
@Override
public void run() {
try
{
ds1=new DatagramSocket(7777);
//chathistory.setText("Holding the port...");
while(true)
{
DatagramPacket p = new DatagramPacket(buffer, buffer.length);
ds1.receive(p);
chathistory.setText(new String(p.getData(), 0, p.getLength()));
chathistory.setText("test");
}
}
catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} })).start();
}