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

h9vpoimq  于 2023-11-15  发布在  Java
关注(0)|答案(1)|浏览(144)

我有一个示例代码,如下所示,套接字绑定到IP 10.10.88.11和端口9876。我用wireshark测试了2个条件,如下所示。两台PC都在同一个子网中。
1.从同一台PC发送UDP数据包(10.10.88.11)- UDP服务器能够接收
1.从另一台电脑(10.10.88.10)发送UDP数据包- UDP服务器无法接收,但Wireshark(10.10.88.11)能够捕获数据包
我已经搜索了互联网,但找不到解决方案。我在创建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);
           }

  }
}

字符串

ffscu2ro

ffscu2ro1#

我相信Wireshark能够在防火墙评估数据包之前抓取数据包,这意味着你会检测到它们,但它们永远不会到达java应用程序。

相关问题