我有一个示例代码,如下所示,套接字绑定到IP 10.10.88.11和端口9876.我使用wireshark测试了2个条件,如下所示。两台PC都在同一个子网中。
我搜索了互联网,但无法找到解决方案。我在创建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);
}
}
}
答案 0 :(得分:2)
我相信Wireshark能够在防火墙评估之前抓取数据包,这意味着您将检测到它们但它们永远不会到达Java应用程序。您是否尝试停用防火墙?