views:

93

answers:

1

Hi all,

Basically what I'm trying to achieve is a program which allow users to connect to a each other over a network in, essentially, a chat room. What I'm currently struggling with is writing the code so that the users can connect to each other without knowing the IP-address of the computer that the other users are using or knowing the IP-address of a server.

Does anyone know of a way in which I could simply have all of the users scan the IP range of my network in order to find any active 'room' and then give the user a chance to connect to it?

Also, the hope is that there will be no need for a central server to run this from, rather every user will simply be connected to all other user, essentially being the server and client at the same time.

+4  A: 

I can give you two suggestions. First of all, UDP packets to the broadcast address of your network will be received by everybody. Secondly, there is a protocol for programs offering certain services to find each other on a local network. That protocol is called mDNS, ZeroConf, or Bonjour.

Using broadcast UDP is likely going to be the faster route. But if I were you, I'd learn how to use ZeroConf instead. It's supported well under IPv6 and already used by several interesting programs such as SubEthaEdit and Gobby.

Here is a link to a nice tutorial for implementing something that speaks ZeroConf in Python.

Another recommendation... If you want to hand roll your own broadcast/multicast UDP code and you can be sure that all of the systems you're on are running a Linux that's newer than 2003 or so, and all the Windows systems are XP or better, you can probably get away with using IPv6. The IPv6 link-local (think same LAN) all hosts multicast address is ff02::1. That's really simple and easy, and it will reach all the other systems on the same LAN. It's much better than having to figure out what your network's broadcast address is with IPv4.

Omnifarious
Allow me to add that although the sourceforge pyzeroconf archive linked in that tutorial is buggy and seems to have been abandoned, there has been some more recent work on it here:https://launchpad.net/pyzeroconf Also, the protocol is built-in to pretty much all Macs, Linux boxes, and major band network printers these days.
Forest
Thanks for the extremely quick response, one area I hope to expand this application into will be to transfer files over the network to other users. Do you know if this will be easy to integrate with a program using ZeroConf?
Rakis
@lamnep: As @Rakis pointed out, ZeroConf is there so your programs can find each other. What they do once they know about each other and are connected is their business.
Omnifarious