JAVA - 在 Linux 上接收 UDP 数据包时出现问题
你好,我编写了一个简单的代码来测试我正在做的程序。
代码在这里:
。 。 。
public static final byte precond[] = {(byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF};
public static final byte aftercond[] = {(byte) 0x0a,(byte) 0x00};
String msg = new String(precond) + "challenge rcon" + new String(aftercond);
String aux = "";
//Enviar
DatagramSocket sc2 = new DatagramSocket(27020);
//sc2.setSoTimeout(5000);
DatagramPacket pkt = new DatagramPacket(msg.getBytes(),msg.length(),InetAddress.getByName("82.102.15.70"),27050);
sc2.send(pkt);
System.out.println("SENT");
//Receber
DatagramPacket pkt2 = new DatagramPacket(new byte[1024],1024);
sc2.receive(pkt2);
String recived = new String(pkt2.getData(),0,pkt2.getLength());
aux = recived.split(" ")[2].trim();
sc2.close();
System.out.println("RECIVED - " + aux);
。 。 。
这是一个简单的代码,唯一认为它的作用是将 udp 数据包发送到服务器,服务器将响应。
问题是,这个工作在 Windows 上,但它不能在 ubuntu 上工作(服务器/桌面版本,我不是说在 Linux 上,因为我还没有在另一个 detro 中尝试过)。
我已经检查了 IPtables 与路由器相关的所有内容,但我无法解决这个问题,代码运行到第一个 System.out 然后它会阻止等待响应,但 ubuntu 上的响应从未到达:S
有人可以帮忙吗?
已经在另一台服务器(VPS)上尝试过了,仍然是同样的问题。
问题出在第一个数据包发送中!
Linux 屏幕: http://img853.imageshack.us/f/linuxr.png
Hello i made a simple code to test a program that i was doing.
The code is here:
.
.
.
public static final byte precond[] = {(byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF};
public static final byte aftercond[] = {(byte) 0x0a,(byte) 0x00};
String msg = new String(precond) + "challenge rcon" + new String(aftercond);
String aux = "";
//Enviar
DatagramSocket sc2 = new DatagramSocket(27020);
//sc2.setSoTimeout(5000);
DatagramPacket pkt = new DatagramPacket(msg.getBytes(),msg.length(),InetAddress.getByName("82.102.15.70"),27050);
sc2.send(pkt);
System.out.println("SENT");
//Receber
DatagramPacket pkt2 = new DatagramPacket(new byte[1024],1024);
sc2.receive(pkt2);
String recived = new String(pkt2.getData(),0,pkt2.getLength());
aux = recived.split(" ")[2].trim();
sc2.close();
System.out.println("RECIVED - " + aux);
.
.
.
Well this is a simple code the only think it does it's to send a udp packet to a server and server will respond.
The problem it's, this work's on Windows but it DON'T work on ubuntu(server/desktop edition, iam not saying in linux, because i haven't tried in another destro).
I already checked IPtables everything related with router but i can't solve this, the code run until 1st System.out then it block's waiting for the response, but the response on ubuntu never arrived :S
Can some one help please?
Already tried in another server (VPS) and it still the same problem.
Problem is in the 1st packet send!
linux screen: http://img853.imageshack.us/f/linuxr.png
windows screen: http://img339.imageshack.us/f/windowsep.png
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我怀疑这是“默认”IP 地址的差异。
您没有绑定到特定的 IP 地址,而是发送到计算机的公共 IP。
我猜在 Linux 中,当您调用
DatagramSocket sc2 = new DatagramSocket(27020);
时,您会得到127.0.0.1
尝试:
I suspect it's a difference in what the "default" IP address is.
You're not binding to a specific IP address but are sending to the public IP of the machine.
I'm guessing that in linux you're getting
127.0.0.1
when you callDatagramSocket sc2 = new DatagramSocket(27020);
Try:
这可能是由于网络接口是否配置为混杂所致。我有一些模糊的记忆,在Linux中,网络接口通常不会被配置为混杂的。如果网络接口未配置为混杂,则它将不会接收自己的 udp 数据包。
it may be due to whether or not the network interface is configured to be promiscuous. i have some vague recollection that in linux, network interfaces are not usually configured to be promiscuous. if a network interface is not configured to be promiscuous, it will not receive its own udp packets.
使用 Wireshark 检查线路上实际发送和接收的内容。这应该会给你更多关于去哪里寻找的指示。
Check what's actually being sent and received on the wire with Wireshark. That should give you more pointers as as to where to look.