Steps 2 & 3: Threading and Packet Handling

Multithreading didn’t turn out too bad. Actually, it was painless. But that’s because I did it the lazy way — I haven’t gotten around to making it thread-safe. So it sometimes happens that when you’re sending text you all of a sudden get a message received print out in the middle of your text, messing up the display of text on terminal. For personal use I can certainly live with it, but for the sake of learning threading I should learn how to use mutexes and semaphores.

Packet handling was all about string processing, which isn’t exactly C’s forte. I ended up using a mixture of strcat, strtok, and ptr++ to parse the packets I received. I also created a map that maps the string identifiers of messages in RFC to the integer constants I define for each RFC message, so this way I can switch the packet with ease.

Here is the working code. I plan to improve it later on by adding more functionality as well as thread synchronization.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s


%d bloggers like this: