简体   繁体   English

JAVA - 在 Linux 上接收 UDP 数据包时出现问题

[英]JAVA - Problem receiving UDP Packets on Linux

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.好吧,这是一个简单的代码,唯一的想法是将 udp 数据包发送到服务器,服务器将响应。

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).问题是,这项工作在 Windows 上,但在 ubuntu 上不起作用(服务器/桌面版,我不是在 linux 中说,因为我还没有尝试过另一个 detro)。

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我已经检查了 IPtables 与路由器相关的所有内容,但我无法解决这个问题,代码运行到第一个 System.out 然后它阻塞等待响应,但 ubuntu 上的响应从未到达:S

Can some one help please?有人可以帮忙吗?

Already tried in another server (VPS) and it still the same problem.已经在另一台服务器(VPS)上尝试过,但仍然是同样的问题。

Problem is in the 1st packet send!问题在于第一个数据包发送!

linux screen: http://img853.imageshack.us/f/linuxr.png linux 屏幕: http://img853.imageshack.us/f/linuxr.png

windows screen: http://img339.imageshack.us/f/windowsep.png windows 屏幕: http://img339.imageshack.us/f/windowsep.png

I suspect it's a difference in what the "default" IP address is.我怀疑这是“默认” IP 地址的区别。

You're not binding to a specific IP address but are sending to the public IP of the machine.您没有绑定到特定的 IP 地址,而是发送到机器的公共 IP 地址。

I'm guessing that in linux you're getting 127.0.0.1 when you call DatagramSocket sc2 = new DatagramSocket(27020);我猜在 linux 中,当您调用DatagramSocket sc2 = new DatagramSocket(27020);时,您会得到127.0.0.1

Try:尝试:

DatagramSocket sc2 = 
    new DatagramSocket(27020, InetAddress.getByName("<my public IP here>"));

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.我有一些模糊的回忆,在 linux 中,网络接口通常不会配置为混杂。 if a network interface is not configured to be promiscuous, it will not receive its own udp packets.如果网络接口未配置为混杂,它将不会收到自己的 udp 数据包。

Check what's actually being sent and received on the wire with Wireshark .使用Wireshark检查网络上实际发送和接收的内容。 That should give you more pointers as as to where to look.这应该会给你更多关于在哪里看的指示。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM