0

I want that my client application is able to connect to a Server application.

The problem is that my Client doesn't know the Server ip (in LAN).

So I tried to use java object MulticastSocket. Luckily Oracle have a page with an example of Broadcasting.

Here I have rearranged it for my use.

Server code:

long FIVE_SECONDS = 5000;        
int port = 4445;

DatagramSocket socket = new DatagramSocket(port);

while (true) {
    System.out.println("Server running...");
    try {
        // message for client
        String dString = "Hello Client";                
        byte[] buf = dString.getBytes();

        // send
        InetAddress group = InetAddress.getByName("230.0.0.1");
        DatagramPacket packet = new DatagramPacket(buf, buf.length, group, port);
        socket.send(packet);

        // sleep for a while
        try {
            Thread.sleep((long)(Math.random() * FIVE_SECONDS));
        }
        catch (InterruptedException e) {
            System.err.println("Interrupted Exception");
        }
    } catch (IOException e) {
        System.err.println("IOException");
    }
}

Client code:

MulticastSocket socket = new MulticastSocket(4445);
InetAddress address = InetAddress.getByName("230.0.0.1");
socket.joinGroup(address);

// receive the message
byte[] buf = new byte[256];
DatagramPacket packet = new DatagramPacket(buf, buf.length);
socket.receive(packet);

String received = new String(packet.getData(), 0, packet.getLength());
System.out.println("Received: " + received);

socket.leaveGroup(address);
socket.close();

When I run Srver: no problem, but when I try running client it throw java.net.BindException: Address already in use cause both client and server are listening/sending information on port 4445.

But isn't it right? To connect each other they must have the same port number, or they'll never 'meet'.

Can I solve this problem? How?

Are the port number correct?

Is this a right resolution to the problem about the unknown server ip?

Thanks!

xdola
  • 562
  • 2
  • 8
  • 23
  • While I can't anser the question, two listeners on the same port will not work. A port is like an IO-stream, when there are two writers/readers, bad things will happen. That's why a `BindException` is thrown to prevent bad things from happening. – WorldSEnder Sep 04 '15 at 18:05
  • @WorldSEnder : probably you are right but, in the example from Oracle linked in my question (and [here](https://docs.oracle.com/javase/tutorial/networking/datagrams/broadcasting.html)) they use the same port number... – xdola Sep 04 '15 at 18:15
  • Did you run their examples? If no, did you your Client and Server on the same Computer? If yes, then I can explain the problem, if no I have no idea – WorldSEnder Sep 04 '15 at 20:19
  • @WorldSEnder yes, I run their examples with Client and Server on the same computer. Does it affect something? – xdola Sep 04 '15 at 21:50
  • "But isn't it right? To connect each other they must have the same port number, or they'll never 'meet'." No, it isn't right. The sender sends to the receiver's port. They don't have to be the same. – user207421 Sep 04 '15 at 23:13

2 Answers2

3

As Warren mentioned in his answer, your client and server can't bind to the same port on the same machine. The Oracle example is not doing that.

The client should bind to port 4446 and the server should bind to port 4445. When the server create a DatagramPacket it should do so with the client's port which is 4446.

If you do this and the client still can't receive, you may need to set the outgoing interface for multicast on the server. You can do this with either the setInterface or setNetworkInterface methods.

For example, suppose your serverhas IP addresses 192.168.1.1 and 192.168.2.1. If you want your sender to send from 192.168.1.1, you would call:

multicastSocket.setInterface(InetAddress.getByName("192.168.1.1"));
dbush
  • 205,898
  • 23
  • 218
  • 273
1

You are getting this exception because you are trying to run your server application and your client application on the same machine. When you start your client, your server has already bound to port 4445, so it is already in use - and thus unavailable - when your client tries to bind to it.

Running your server and your client on different machines would get around that particular error. However, you could also get around it by choosing different ports for your server and your client.

For example if you ran your server on port 4445, and your client on port 4446, you could do the following. On the server, you would add a variable for the client port, and use the client port as the destination port when sending your DatagramPacket:

int clientPort = 4446;
...
DatagramPacket packet = new DatagramPacket(buf, buf.length, group, clientPort);

instead of

DatagramPacket packet = new DatagramPacket(buf, buf.length, group, port);

On the client, you would simply bind to the client port instead of the server port:

MulticastSocket socket = new MulticastSocket(4446);

instead of

MulticastSocket socket = new MulticastSocket(4445);

Using different port numbers for the server and for the client would allow you to run both the server application and the client application on the same machine and get you past this particular issue.

Warren Dew
  • 8,790
  • 3
  • 30
  • 44
  • Thanks for your answer but, your changes don't solve the problem: on the same machine client can't find server's packet. – xdola Sep 04 '15 at 21:57