JDK-4191980 : DatagramSocket won't see packets sent to a broadcast address
  • Type: Bug
  • Component: core-libs
  • Sub-Component: java.net
  • Affected Version: 1.1.6
  • Priority: P3
  • Status: Resolved
  • Resolution: Fixed
  • OS: solaris_2.5.1
  • CPU: sparc
  • Submitted: 1998-11-20
  • Updated: 2001-02-08
  • Resolved: 2001-02-08
The Version table provides details related to the release that this issue/RFE will be addressed.

Unresolved : Release in which this issue/RFE will be addressed.
Resolved: Release in which this issue/RFE has been resolved.
Fixed : Release in which this issue/RFE has been fixed. The release containing this fix may be available for download as an Early Access Release or a General Availability Release.

To download the current JDK release, click here.
Other
1.4.0 betaFixed
Related Reports
Relates :  
Description

Name: jn10789			Date: 11/20/98


These steps seem to run fine on NT 4.0, but broadcast packets are not received on Sun Solaris ("uname -a" returns "SunOS wally 5.5.1 Generic_103640-17 sun4m sparc SUNW,SPARCstation-10")

1. On machine 1, type "java Server ip port", where ip is the ip address of the interface that will be receiving datagrams.  This machine should be a Solaris machine.

2. On machine 2, type "java Broadcaster ip port", where ip and port are the ip and port used in step 1.  At this point you should see messages that indicate that packets are being sent and received.

3. Stop the broadcaster on machine 2 and re-run the broadcaster, specifying a broadcast address for the ip (e.g. 164.217.255.255).  The broadcaster will indicate that it is sending packets, but machine 1 will not be receiving them.

Server.java:
package SocketTest;
import java.net.*;
import java.io.*;

public class Server
{
    public Server()
    {
    }

    public static void main(String[] args)
    {
        Server server = new Server();
        server.invokedStandalone = true;

        if (args.length < 2)
        {
            System.out.println ("Usage: Server IP port");
            return;
        }

        InetAddress a;
        try
        {
            a = InetAddress.getByName (args [0]);
        }
        catch (UnknownHostException uhe)
        {
            System.out.println (args [0] + " is an unknown interface");
            return;
        }

        Integer port = new Integer (args [1]);
        DatagramSocket serverSocket;

        try
        {
            serverSocket = new DatagramSocket (port.intValue (), a);
        }
        catch (IOException ioe)
        {
            System.out.println ("IO exception on ServerSocket: '" + ioe.toString () + "'");
            return;
        }

        System.out.println ("Socket opened");

        boolean quit = false;
        byte [] bytes = new byte [512];
        int numPackets = 0;
        while (!quit)
        {
            DatagramPacket dp = new DatagramPacket (bytes, 512);
            try
            {
                serverSocket.receive (dp);
            }
            catch (IOException ioe)
            {
                System.out.println ("IO exception on accept: '" + ioe.toString () + "'");
                return;
            }

            ++numPackets;
            System.out.println ("Packets received:" + numPackets);
        }

        serverSocket.close ();
    }

    private boolean invokedStandalone = false;
}


Broadcaster.java:
package SocketTest;
import java.net.*;
import java.io.*;

public class Broadcaster
{

    public Broadcaster()
    {
    }

    public static void main(String[] args)
    {
        Broadcaster broadcaster = new Broadcaster();
        broadcaster.invokedStandalone = true;

        if (args.length < 2)
        {
            System.out.println ("Usage: Broadcaster IP port");
            return;
        }

        InetAddress dest;
        try
        {
            dest = InetAddress.getByName (args [0]);
        }
        catch (UnknownHostException uhe)
        {
            System.out.println (args [0] + " is an unknown interface");
            return;
        }

        int port = (new Integer (args [1])).intValue ();
        DatagramSocket socket;
        try
        {
            socket = new DatagramSocket ();
        }
        catch (IOException ioe)
        {
            System.out.println ("IO exception on ServerSocket: '" + ioe.toString () + "'");
            return;
        }

        System.out.println ("Opened socket");

        boolean quit = false;
        byte [] bytes = new byte [512];
        int numPackets = 0;
        while (!quit)
        {
            DatagramPacket dp = new DatagramPacket (bytes, 512, dest, port);
            try
            {
                socket.send (dp);
            }
            catch (IOException ioe)
            {
                System.out.println ("IO exception on send: '" + ioe.toString () + "'");
                return;
            }

            ++numPackets;
            System.out.println ("Packets sent:" + numPackets);
        }

        socket.close ();
    }
    private boolean invokedStandalone = false;
}
(Review ID: 36115)
======================================================================

Comments
CONVERTED DATA BugTraq+ Release Management Values COMMIT TO FIX: merlin FIXED IN: merlin-beta INTEGRATED IN: merlin-beta
14-06-2004

EVALUATION Will evaluate. So it looks like the OS may be interferring at this point, the challenge is for Java to provide a consistent api to the user and maintain a consistent behavior calvin.austin@Eng 2000-11-22 This turns out as "not a bug". The Solaris (& Linux for that matter) behavior is that to be able to receive broadcasts a UDP socket has to be bound to the wildcard address (aka anylocal). This has been the defacto standard behavior since the days of BSD 4.3. Win32 behaviors differs from that. So the only way to be guaranteed to receive the broadcasts on any platform is to bind to the wildcard address. So, it's actually more of a Documentation bug. I'll update the documentation on DatagramSocket to reflect this. As for the need to be able to tell which interface a datagram came through, see bugID 4212324 jean-christophe.collet@Eng 2001-01-30
30-01-2001