为什么我的安卓UDP只能接收一次数据,点一下监听接收一次数据,后面的数据都接收不到,想问一下原因?

// 与UDP数据接收线程通信更新UI线程中EditText控件中的内容
receiveHandler = new Handler() {
public void handleMessage(Message msg) {

            point2.setText(receiveInfo);

        }
    };
}

public class udpReceiveThread extends Thread {
@Override
public void run() {
try {
socket = new DatagramSocket(8081);
//listenStatus = true;
while (true){
// byte[] inBuf= new byte[1024];
byte[] inBuf = new byte[1024];
DatagramPacket inPacket = new DatagramPacket(inBuf,
inBuf.length);
socket.receive(inPacket);
String str = new String(inPacket.getData());
receiveInfo = Change1(str, inPacket.getLength()).trim();

                //  receiveInfo = new String (inPacket.getData());  
                // receiveInfo= stringtopoint(inBuf,inPacket.getLength());
                Message msg = new Message();
                receiveHandler.sendMessage(msg);

            }
        } catch (Exception e) {
            e.printStackTrace();
            // TODO Auto-generated catch block
        }
    }

}public void ListenButtonClick(View source) {
    if (listenStatus == false) {
        stop.setText("停止监听");
        new udpReceiveThread().start();
    } else {
        stop.setText("开始监听");
        socket.close();
        listenStatus = false;   
        new udpReceiveThread().interrupt();
        //new udpReceiveThread().start();

    }
}

1个回答

我想要时时更新数据,为什么只能一次呢?

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐