简体   繁体   中英

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.

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

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 call DatagramSocket sc2 = new DatagramSocket(27020);

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. if a network interface is not configured to be promiscuous, it will not receive its own udp packets.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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