JAVA UDP服务器无法接收数据包

时间:2016-06-17 07:58:00

标签: java udp

我有一个示例代码,如下所示,套接字绑定到IP 10.10.88.11和端口9876.我使用wireshark测试了2个条件,如下所示。两台PC都在同一个子网中。

  1. 从同一台PC(10.10.88.11)发送UDP数据包 - 能够接收的UDP服务器
  2. 从另一台电脑发送UDP数据包(10.10.88.10) - UDP服务器无法接收,但Wireshark(10.10.88.11)能够捕获数据包
  3. 我搜索了互联网,但无法找到解决方案。我在创建InetScoketAddress时有什么问题吗?

    import java.io.*;
    import java.net.*;
    
    public class UDPServer {
        public static void main(String args[]) throws Exception {        
    
         InetSocketAddress address = new InetSocketAddress("10.10.88.11", 9876);
    
         DatagramSocket serverSocket = new DatagramSocket(address);
            byte[] receiveData = new byte[1024];
            byte[] sendData = new byte[1024];
            while(true)
               {
                  DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length);
                  System.out.println("Waiting to receive");
                  serverSocket.receive(receivePacket);
                  String sentence = new String( receivePacket.getData());
                  System.out.println("RECEIVED: " + sentence);
                  InetAddress IPAddress = receivePacket.getAddress();
                  int port = receivePacket.getPort();
                  String capitalizedSentence = sentence.toUpperCase();
                  sendData = capitalizedSentence.getBytes();
                  DatagramPacket sendPacket =
                  new DatagramPacket(sendData, sendData.length, IPAddress, port);
                  serverSocket.send(sendPacket);
               }
    
      }
    }
    

1 个答案:

答案 0 :(得分:2)

我相信Wireshark能够在防火墙评估之前抓取数据包,这意味着您将检测到它们但它们永远不会到达Java应用程序。您是否尝试停用防火墙?