Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Simple UDP broadcast client and server on different machines

The following client and server UDP broadcast code works on fine when both are on the same PC. However when I have them on separate PC's in the same WIFI LAN nothing happens at all. I have managed to get a multicast version working fine on the two separate PC's but not this :(. I have shut down firewalls on both and succesfully pinged each from both PC's.

The idea behind this test is so I can use this method so a client can find a server on the LAN by sending a datagram packet (peer discovery). I think I'm doing something wrong with the host name or something but after a week of googling and testing new ideas I'm officially all out of them :(.

public class Client
{
    private String hostname= "localhost";
    private int port=1234;
    private InetAddress host;
    private DatagramSocket socket;
    DatagramPacket packet;

    public void run()
    {
        try
        {
            host = InetAddress.getByName(hostname);
            socket = new DatagramSocket (null);
            packet=new DatagramPacket (new byte[100], 0,host, port);
            socket.send (packet);
            packet.setLength(100);
            socket.receive (packet);
            socket.close ();
            byte[] data = packet.getData ();
            String time=new String(data);  // convert byte array data into string
            System.out.println(time);
        }
        catch(Exception e)
        {
            e.printStackTrace();
        }
    }
}



public class Server
{
    public static final int DEFAULT_PORT = 1234;
    private DatagramSocket socket;
    private DatagramPacket packet;

    public void run()
    {
        try
        {
            socket = new DatagramSocket(DEFAULT_PORT);
        }
        catch( Exception ex )
        {
            System.out.println("Problem creating socket on port: " + DEFAULT_PORT );
        }

        packet = new DatagramPacket (new byte[1], 1);

        while (true)
        {
            try
            {
                socket.receive (packet);
                System.out.println("Received from: " + packet.getAddress () + ":" +
                                   packet.getPort ());
                byte[] outBuffer = new java.util.Date ().toString ().getBytes ();
                packet.setData (outBuffer);
                packet.setLength (outBuffer.length);
                socket.send (packet);
            }
            catch (IOException ie)
            {
                ie.printStackTrace();
            }
        }
    }
}

Just wondering if anyone can help?

like image 348
Paul Avatar asked Oct 21 '12 15:10

Paul


People also ask

Is there client and server in UDP?

In UDP, the client does not form a connection with the server like in TCP and instead just sends a datagram. Similarly, the server need not accept a connection and just waits for datagrams to arrive. Datagrams upon arrival contain the address of the sender which the server uses to send data to the correct client.

What is the difference between UDP client and server?

A UDP server is always listening. A UDP client is only listening after sending a message, for a response.

How do I broadcast using UDP?

Connecting via UDPSet the IP address of the PC's Network card to the same subnet than your Audia/Nexia units. In Audia/Nexia/DaVinci software, go to the Tools menu > Options > Network tab. In the Network Device Discovery Method section, make sure UDP Broadcast is selected.

What is UDP sockets and explain UDP client server with example?

UDP - User Datagram Protocol sockets It is a connection-less protocol where you directly send packets without have to establish a proper connection. UDP packets have smaller headers compared to TCP headers. Also data communication is faster since no acknowledgement is exchanged for reliable packet delivery.


1 Answers

To actually broadcast you must send the packet to all the IP on the LAN. The range of possible IP is from 0.0.0.0 to 254.254.254.254 but to select all of them you could write: 255.255.255.255. But most of the routers will block this. They will allow something like 192.168.1.255 witch broadcasts to all the 255 ip from 192.168.1.0 to 192.168.1.254 which is what you need, I think.

like image 60
bodyady Avatar answered Sep 20 '22 21:09

bodyady