receive udp packets on windows xp

Discussion in 'Java' started by Andrew Chin, Dec 8, 2005.

  1. Andrew Chin

    Andrew Chin Guest

    Hi All,

    I'm trying to write some Java code that receives UDP packets on a
    specific port. I'm using jre1.5.0_06 on a windows XP pro machine. My
    problem is that the code works on a Linux box, but not my Windows box.
    Here is the code (which I modified from anohter Usenet post):
    public static void main(String[] args) {

    DatagramSocket socket;
    try {

    socket = new DatagramSocket(12345);

    byte[] buf = new byte[256];
    DatagramPacket packet = new DatagramPacket(buf, buf.length);
    System.out.println("Attempting to receive packet...");

    buf = packet.getData();
    System.out.println("received packet: "+(new
    String(buf)).substring(0,20) +
    " from " + packet.getAddress() );

    } catch (IOException e) {


    I'm generating the udp packets with the following linux command:
    nc -u 12345

    Again, this code works fine under linux, but does not work under
    Windows. Is this a known issue? Are there any workarounds or code
    changes I need to make? (sending UDP packets under Windows works

    -Andrew Chin
    Andrew Chin, Dec 8, 2005
    1. Advertisements

  2. Andrew Chin

    Chris Smith Guest

    I copied your code, ran it exactly as you have there, and it worked
    fine. That is, it printed out "received packet" with the packet
    information, and then exited. Windows XP, Java 1.5.0_04. Perhaps
    you're doing something wrong in testing, such as getting the wrong IP
    address for the Windows machine?

    The Easiest Way To Train Anyone... Anywhere.

    Chris Smith - Lead Software Developer/Technical Trainer
    MindIQ Corporation
    Chris Smith, Dec 8, 2005
    1. Advertisements

  3. Andrew Chin

    Andrew Chin Guest

    well, this is rather embarrassing, but the issue turned out to be a
    firewall issue.
    despite the fact that java was (supposedly) allowed through the
    firewall, the packet was apparently not getting through XP's built-in
    firewall. because ethereal was seeing the packet, I assumed that the
    firewall was not blocking it, but it looks like this was not a safe
    assumption to make. Anyway, thanks for you help -- as the code worked
    for you, i knew i should look at other potential problems beside the

    -Andrew Chin
    Andrew Chin, Dec 8, 2005
  4. Andrew Chin

    Roedy Green Guest

    try connecting your machines to the same LAN. If the code starts
    working, you have a firewall problem. You might even have a software
    firewall turned on in XP.
    Roedy Green, Dec 8, 2005
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.