如何在UDP Wi-Fi通信中获得响应

时间:2015-09-15 09:35:57

标签: android udp response wif

我正在从Android设备到某些硬件进行Wifi UDP通信。我可以通过以下代码成功发送请求

String json = "{\"data\":\"aaxxz\",\"command\":\"1\",\"type\":\"wificommand\",\"status\":\"request\"}";
                  int server_port = Integer.parseInt(port.getText().toString());
                  DatagramSocket s = null;
                try {
                    s = new DatagramSocket();
                } catch (SocketException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                  InetAddress local = null;
                try {
                    local = InetAddress.getByName(ipaDD.getText().toString());
                } catch (UnknownHostException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                  int msg_length=json.length();
                  byte[] message = json.getBytes();
                  DatagramPacket p = new DatagramPacket(message, msg_length,local,server_port);
                  try {
                    s.send(p);
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } 
 // i wrote code to get response here. which is mentioned below

上面的代码就像魅力一样没有任何问题,但我无法通过以下代码获得回复。

try {
        s.receive(p);
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }

       String  text = new String(message, 0, p.getLength());
       System.out.println("resp    "+text);

我做错了,请帮我摆脱这个问题。

0 个答案:

没有答案