简体   繁体   English

UDP客户端向服务器发送数据,但未收到响应

[英]UDP client sending data to server, but not receiving response

I am creating a simple Android app which can communicate with my PC. 我正在创建一个可以与PC通讯的简单Android应用。 On my computer I have a very simple UDP server in Java. 在我的计算机上,我有一个非常简单的Java UDP服务器。

public void run(){

    try{
        DatagramSocket serverSocket = new DatagramSocket(port);
        byte[] receiveData = new byte[8];
        byte[] sendData = new byte[8];

        while(true)
           {
              DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length);
              serverSocket.receive(receivePacket);
              String sentence = new String( receivePacket.getData());
              System.out.println("RECEIVED: " + sentence);
              InetAddress IPAddress = receivePacket.getAddress();
              String sendString = "polo";
              sendData = sendString.getBytes();
              DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.length, IPAddress, port);
              serverSocket.send(sendPacket);
           }
    }catch (Exception e){
    }
}

and I have another simple piece of code inside my android app which sends a UDP packet to the server and awaits the response. 我的Android应用程序中还有另一段简单的代码,它向服务器发送UDP数据包并等待响应。

public void checkServerOnline(View v) {
    try {
        int port = 46001;
        DatagramSocket clientSocket = new DatagramSocket();
        clientSocket.setSoTimeout(1800);
        InetAddress IPAddress = InetAddress.getByName(host);
        byte[] sendData = new byte[8];
        byte[] receiveData = new byte[8];
        String sentence = "marco";
        sendData = sentence.getBytes();
        DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.length, IPAddress, port);
        clientSocket.send(sendPacket);
        DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length);
        clientSocket.receive(receivePacket);
        clientSocket.close();
    } catch (Exception e) {
    }

The problem I am having is that my Client is timing out waiting for a response. 我遇到的问题是客户正在等待响应。 The server is definitely receiving the string "marco" and is presumably sending the response "polo", but the client is not receiving it. 服务器肯定正在接收字符串“ marco”,并且大概正在发送响应“ polo”,但是客户端未接收到它。 I've tried removing the timeout on the client, but it just freezes up until I force close the application. 我曾尝试删除客户端上的超时,但是直到我强制关闭应用程序后,它才冻结。

Can anyone see an error in my code? 谁能看到我的代码中的错误? I can't understand why it won't work. 我不明白为什么它不起作用。 I've managed to successfully setup a TCP Server and client with the same setup, but cannot seem to do UDP. 我已经成功地使用相同的设置成功设置了TCP服务器和客户端,但似乎无法执行UDP。

The server is sending to the wrong port. 服务器发送到错误的端口。 It should send to the port in the received datagram, not its own port. 它应该发送到接收到的数据报中的端口,而不是自己的端口。 It is simplest to reuse the request datagram and just change the data to the response data: the return address is already there. 重用请求数据报并将数据更改为响应数据是最简单的:返回地址已经存在。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM