Monday, April 14, 2008

A Simple Chat Room Server

Server programming poses a lot of challenges but nowadays with the advent of high-level languages such as python, the code that actually makes the basic server functions is put out of the equation giving us the time to think more about the logic and features that were going to implement.

Last March I conducted a network programming seminar in PUP's College of Eng'g. I expected a good question and answer session from them but sad to say, there were very few questions that was thrown at me. Probably the reason was that the seminar was a little bit too technical for the students and network programming is very rarely taught in College. (I wish I could've at least had more time to teach them with a hands on session)

On the seminar I provided a simple Chat Room server program written in python. This is a very good example how easy it is to write server programs nowadays. The code is roughly 100 lines and is self-explanatory (rewriting this to C++ could take more lines).

The Chat room server implements a very simple protocol:

LIST - returns the nickname of the people that are online
LOGIN: - login to the chat room with the provided nick name (Default is 'Anonymous Coward')
EXIT - leave the chat room

The server assumes that it's a message if the typed message does not match the existing commands. The chat_server.py is tested to work under linux and should be started just like:

# ./chat_server.py "port number to listen to"

You can use telnet to connect to the server:

# telnet localhost "port number where the server is listening"


We use localhost or 127.0.0.1 assuming that the server in running in the same machine. The interface is similar to the old unix talk program.



So there, a Chat Room server written in 100 lines of code! If you're bored, you can download the Source Code and see for yourself (some explanatory comments added).

Now that I've posted a sample python code, next blog is about how to make your own python modules in C. That's it for now. ^_^


2 comments:

JoJosho said...

See Please Here

Anonymous said...

Nice blog, glad to know that "Alvinator" has gone a long way :-D). The title sounds a bit porn-ish tho. But "Alvinatorrr's Anal Adventures" would have done worse. Just kidding. Hey, btw, I spelled it with triple 'r', remember?!!

Seriously, congats dude! More power helping bringing our favorite language more into geeks consoles.

-- Vincent