两个Android设备之间的udp通信:如何响应

时间:2019-04-05 20:02:18

标签: java android udp

我正在开发应用程序,如果用户连接到同一网络,它将允许用户彼此通信。 我有udp_server类和sendMessage客户端方法。 问题是客户端将消息发送到服务器时(sendMessage方法),服务器正确地读取了消息,但是当服务器发送答复时,显示了已发送消息的Toast-这表示已发送响应,但客户端未接收到它。尝试catc块也不会捕获任何异常。

udp_server类:

private class MyDatagramReceiver extends Thread {
        private boolean bKeepRunning = true;
        private String lastMessage = "";

        public void run() {
            String message;
            byte[] lmessage = new byte[1024*512];
            final DatagramPacket packet = new DatagramPacket(lmessage, lmessage.length);
            try {
                final DatagramSocket socket = new DatagramSocket(11234);
                while(bKeepRunning) {
                    socket.receive(packet);
                    message = new String(lmessage, 0, packet.getLength());
                    lastMessage = message;
                    runOnUiThread(updateTextMessage);

                    byte[] response = "response".getBytes();
                    final DatagramPacket response_packet = new DatagramPacket(response,response.length,InetAddress.getByName("192.168.8.111"),11234);
                    socket.send(response_packet);
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {                                Toast.makeText(getApplicationContext(),"sended",Toast.LENGTH_LONG).show();
                        }
                    });
                }
            } catch (final Throwable e) {                 
                e.printStackTrace();
            }
        }
        public void kill() {
            bKeepRunning = false;
        }
        public String getLastMessage() {
            return lastMessage;
        }
    }

    private Runnable updateTextMessage = new Runnable() {
        public void run() {
            Toast.makeText(getApplicationContext(),"recived",Toast.LENGTH_LONG).show();
            if (myDatagramReceiver == null) return;
                adapter.add(myDatagramReceiver.getLastMessage());
        }
    }

客户端发送Messenger方法:

 private void sendMessage(final String message) {
        Thread thread = new Thread(new Runnable() {

            String stringData;

            @Override
            public void run() {
                DatagramSocket ds = null;
                try {
                    ds = new DatagramSocket();                      
                    InetAddress serverAddr = InetAddress.getByName("192.168.8.188");
                    DatagramPacket dp;
                    dp = new DatagramPacket(message.getBytes(), message.length(), serverAddr, 11234);
                    ds.send(dp);
                    byte[] lMsg = new byte[1000];
                    dp = new DatagramPacket(lMsg, lMsg.length);
                    ds.receive(dp);
                    stringData = new String(lMsg, 0, dp.getLength());

                } catch (final IOException e) {
                    e.printStackTrace();                      
                } finally {
                    if (ds != null) {
                        ds.close();
                    }
                }
            }
        });
        thread.start();
    }

有没有人知道那里发生了什么事?

已解决: 我将sendMessage方法中的代码接收部分放入while循环中,如下所示:

boolean keepWaiting = true;
while(keepWaiting) {
    ds.receive(packet);
    stringData = new String(lMsg, 0, packet.getLength());
    runOnUiThread(respone); //there I analize response
    keepWaiting = false;
}

0 个答案:

没有答案