PC Windows 正在发送确认。但这还没有到达 Linux。 我错过了什么? (但是当我不使用 Windows 7 并在 Linux 下对 Linux 执行相同的代码时,它可以工作。)
任何想法!!!
Windows 7 PC:发件人/回复者
System.out.println("[UDP]: "
+ "RemoteIP: " + IPAddress.toString() + " "
+ "Port: " + port + " "
+ "Length: " + send.length() + " "
+ "Sending confirmation!"); // it shows it has correct ip, port and lenght
sockOutput = new DatagramPacket(send.getBytes(), send.length(), IPAddress, port);
serverSock.send(sockOutput);// is it failing???
Linux PC:接收器/命令器
String receive = sendUDPBytes("request", ip).toString();
if (receive.length()<=0) // Waiting forever, and nothing happens LINUX.
{
System.out.println("FAILED");
System.exit(0);
} else {
try {
/* JOB todo */
} catch (Exception ex) {
}
}
sockOutput.write(receive.getBytes());
public static String sendUDPBytes(String bytes, String[] ip) throws IOException
{
String downloaded = null;
DatagramSocket socket = new DatagramSocket();
InetAddress IPAddress = InetAddress.getByName(ip[2]);
byte[] sendData = new byte[14024];
byte[] receiveData = new byte[14024];
sendData = bytes.getBytes();
DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.length);
socket.connect(IPAddress, 58889);
if (socket.isConnected())
{
System.out.println("[UDP]: sending....., waiting......... until receive....");
socket.send(sendPacket);
}
DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length);
socket.receive(receivePacket);
downloaded = new String(receivePacket.getData());
System.out.println("[UDP]: closing.....");
socket.close();
return downloaded;
}
跟进:
0) 不要混淆“你在同一个子集中吗???”,所以如果你有/28 网络,所有的都可以交换数据包。在那种情况下它可以工作,但大多数时候情况并非如此。
1) 代码是完美的,当我测试局域网到局域网时,无论它是什么操作系统,它都能工作(没有防火墙)
2) 当我在同一台 PC 上测试 Windows 到 Windows 时,它也能正常工作
3) 当我测试 Linux to Linux 时它也能工作
4) 当我测试 Linux 到 Windows 或 Windows 到 Linux 时,它也能正常工作
5) 我在一台中央服务器中放置的相同代码和我在本地 Windows PC 中运行的相同代码,其他 ISP 似乎允许这样做。
6) 花了 10 个小时才找到它
猜测:
那么究竟是什么原因呢?好吧,ISP 正在丢失发送到我的公共(public) IP 的数据包,或者他们可能会禁用那些即使我有公共(public) IP。
最佳答案
您正在执行 String bytes
和 getBytes()
取决于平台编码。 getBytes("UTF-8")
将是显式的。
另一件事是不同的换行符:Linux\n,Windows\r\n。
如果字节源自 Windows Latin-1 (Cp1252),则 toString()
可能会抛出异常,并且它会尝试将它们解释为 UTF-8。
private void test(OutputStream sockOutput, String[] ip) throws IOException {
String receive = sendUDPBytes("request", ip);
if (receive.length() <= 0) // Waiting forever, and nothing happens LINUX.
{
System.out.println("FAILED");
System.exit(0);
} else {
try {
/*
* JOB todo
*/
} catch (Exception ex) {
}
}
sockOutput.write(receive.getBytes("UTF-8"));
}
public static String sendUDPBytes(String bytes, String[] ip) throws IOException {
return new String(sendUDPBytesRaw(bytes.getBytes("UTF-8"), ip), "UTF-8");
}
public static byte[] sendUDPBytesRaw(byte[] sendData, String[] ip) throws IOException {
DatagramSocket socket = new DatagramSocket();
InetAddress IPAddress = InetAddress.getByName(ip[2]);
byte[] receiveData = new byte[14024];
DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.length);
socket.connect(IPAddress, 58889);
if (socket.isConnected()) {
System.out.println("[UDP]: sending....., waiting......... until receive....");
socket.send(sendPacket);
}
DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length);
socket.receive(receivePacket);
byte[] downloaded = receivePacket.getData();
System.out.println("[UDP]: closing.....");
socket.close();
return downloaded;
}
关于java - 如何修复 Windows 7 UDP 回复 Linux UDP 连接创建者? Linux 到 Linux 工作但 Linux 到 Windows 7 不工作的地方,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8741429/