views:

197

answers:

4

I'm writting a java application, and I need to quickly discover any other running clients on any wired or wireless local networks in order to establish a TCP connection.

what's the best way of doing this? Are there libraries or code snippets that would do this?

+1  A: 

I guess you need to do a scan on your application's port on all IPs in your subnet.

Just what are the available IPs - or what is your subnet for that matter?
I'm afraid determining that could turn out to be impossible as the network is designed to be transparent to your application.

So, i'd use brute force: pick your IP and change the last byte. Might be too much, might be not enough though.

Or you send a broadcast (which usually would be targeted at x.x.x.255) and see who answers. See Datagram Broadcasting and Multicasts. But i think that's not TCP/IP anymore.

Stroboskop
+1  A: 

A network scan can be very long, even longer on wireless networks. If you need them quickly thru Java you may implement a "meeting point" server on your network. This server listen to a predefined port, clients register on the server on startup and the server can distribute information about the clients on request.

HTH.

PeterMmm
+3  A: 

Multicast UDP is a good way of doing this. It's used in a couple of technologies that support automatic discovery of networked devices over local IP networks (UPnP and ZeroConf).

Multicast UDP is not TCP, but it is still based on IP and, so, uses the same addressing mechanism i.e. IP addresses. Quite often it is compared to radio broadcasting i.e. a multicast sender only needs to send 1 message (i.e. it is like a broadcast) but only clients that are "tuned-in" to the multicast channel will receive it.

You can do a quick search on google or wikipedia for these as a starter, but the basic idea is as follows:

  • when a client starts, it sends out a multicast UDP "hello" message to some pre-specified multicast address and port (e.g. UPnP uses 239.255.255.250:1900)
  • existing clients are listening for incoming multicast "hello" messages on the specified address and port - when a client receives one, it sends a response to the sender
  • the client sending the "hello" message receives a response from each existing client on the network and is now aware of the IP address of each client

If you are looking for libraries to use, UPnP libraries can tend to be a bit heavyweight and a lot of folk generally don't like them, so ZeroConf might be a little more suitable. I don't know of any java implementations of such things but I'm sure you can find some with a little digging around.

Steg
A: 

There is a JGroups toolkit for reliable multicast communications. It allows automatic discovery of additional clients using Multicast techniques as described in other answers.

It also provides communication APIs on top of multicast sockets.

It is used in a number of projects such as JBoss, Tomcat and more to provide an infrastructure for distributed cache. See more here.

Gregory Mostizky