Cip
Cip

Reputation: 117

Find a java server on the LAN

How can I find a java server on the LAN by giving the client only the network part of the IP address? Can I do it this way?

Socket sock = new Socket("10.10.10.*", 4444);

Upvotes: 0

Views: 5062

Answers (1)

Jean
Jean

Reputation: 1857

I had the same problem and here is how I figured it out : UDP Broadcast. It will allow the client to connect to server regardless of its IP, so you don't have to hardcode the IP Address, only the port used for UDP (see below).

Here is how it works :

  1. Server watches port n
  2. Client sends packets at all ports n he can reach
  3. When a message reaches server's port, Server responses to the sender and includes its own IP address
  4. Client creates a socket and connect to the IP address he got from the server

Here is the tutorial that helped me : http://michieldemey.be/blog/network-discovery-using-udp-broadcast/

Upvotes: 6

Related Questions