Ryan's Code Projects

Blog and showcase of Ryan Pridgeon

Multi-User Graphical Chat in C/SDL/Winsock

Long time, no post.

So anyway, recently I finished a simple TCP/IP multi-client chat program. It’s a pretty nifty, fun little app where you can run the client which will connect to the server and you can chat with other clients on the server. I wrote it using C.  For libraries, I used SDL for window management, rendering and threading, and I used the Windows Socket library to handle the networking.

You can find the source and Windows binaries here

Click here to download the ZIP

Unblock port 666 to allow people to connect to your server.

This project actually came out of a small, simple SDL command line which I wrote. It allows me to poll for string input, at the same time as printing to the console and displaying the whole thing.

(I even drew out the damn font using the pencil tool in Ms Paint)

This was awesome for me, because I used alot of threading for the networking. So I made this multi-client chat server and a nice client. The server is the most complex part. For this, I first start a listening thread which waits for an incoming connection from a client. On connection, a new socket is created for the new client, and the server starts a new thread which listens to any data being recieved from the client. The client is added to a main list of clients, so that the server can easily send messages to all the clients.

When a message is received from a client, the receiving thread calls a function to send the data to every other connected client on the server. This allows the multi-user chat which can become so fun.

As far as the client is concerned, It’s pretty simple. The user inputs the server IP into the console, this then sends a connection to the server. On accept, a thread is started to listen to the data recieved by the server, and a listener function is set to send all data which the user types in.

So basically, you type -> sends to server -> your thread in the server triggers a send -> the data is sent to all connected clients -> connected clients see your message in their command line.

I’m well proud 🙂

Categorised as: My Project Showcase

One Comment

  1. pixelp says:

    neat. good work. 🙂

Leave a Reply

Your email address will not be published. Required fields are marked *