First time here? Please, start at the beginning.

Wednesday, August 25

uChat: Network Protocol

(I think I'm going to abandon the "Part X.Y" thing, for now at least.)

As mentioned once before, uChat uses UDP broadcast packets to communicate. This isn't exactly ideal: every computer on the network can "listen in" if they listen on the right port. But it's an easy way to communicate without the need for a central server.

How do you send a UDP broadcast? It's dead-simple: just set the destination IP address of your packets to 255.255.255.255, the broadcast address. These packets will be discarded by a router, so we're not clogging up the entire Internet, just the local LAN.

By the way, the port number used is 12121. Just a randomly chosen number above 1024 (Unix-like systems generally require root access to use ports below 1024). Any port would do really, as long as everyone you want to talk to uses the same one.

Now, just what do these packets say? The content of each packet is a 4 character code followed by the message. The current version uses only 4 message types, but more could be added without trouble as the program discards a packet that it doesn't understand.

Here's the message types and their meanings:
  • "SAY:": this one should be self explanatory, the message is what the sender of the packet "said"
  • "JOIN": sent when someone clicks the "join" button, it announces that someone has joined the chat, the message is the name/alias entered in the upper text area
  • "LEAV": reverse of "JOIN", message is also the name/alias
  • "HERE": sent as a response to a "JOIN" packet, it informs the joinee of who is already present, the message is, again, the sender's name/alias

Why so many devoted to keeping track of who's present? Because the "SAY:" packet doesn't include the name of the sender, only the IP address. Each instance of uChat is responsible for maintaining an IP-address-to-name mapping table (the lookupTable variable).

That's really it. Simple, eh? You can watch it in action with a program like WireShark (I've never tried that myself, but there's no reason it wouldn't work), or by running netcat on another machine with: "nc -lup 12121".

No comments:

Post a Comment