Topic : Multi-threaded Servers with Win32
Author : Kurifu Roushu
Page : << Previous 2  
Go to page :

our client's thread.

DWORD WINAPI ClientThreadEntry( LPVOID Arg1 ){
  int       ClientNo = ClientID;
  CLIENTS   *Client = &Clients[ ClientID ];

  Client->InUse = true;

  memcpy( &Client->ClientSocket, &SinkSocket, sizeof( SinkSocket ));
  memcpy( &Client->ClientAddress, &SinkSockAddr, sizeof( SinkSockAddr ));

  ThreadInit = false;

  return 0;

This is the client thread, when the thread is started this code will be given its own point of execution and will only work with the one client that has connected.

I like to make things more convenient when I think of it, so I will create another Client Descriptor CLIENTS to point to the array item that this client connection sits on. I will also use ClientNo to store the index number to that array locally, remember that before the thread was called it was stored in a global which will be overwritten for the next client that tries to connect. We than set the client as in use using our pointer Client->InUse = true;

Now, before we can give control back to the parent thread to accept more connection we must copy the Socket, and sockaddr_in information. To do this we simply just perform a memcpy() on the global instances, and copy them into the client structure variables. Now they are ready for use by the next client so we set TreadInit to false and resume normal program execution of the parent thread.

When the function returns, the thread itself returns with that value and terminates so within this code a loop that would consistently checks for incoming data from the client would be needed. Once data is received, you simply check to see what is in the packet that was sent, and than make any changes that your require from that.

The next article will address receiving and decoding packets. I would write it with this article, but I am tired now and would like to get some of my own coding done.

With the code given above you should be able to write a basic server that will accept incoming connections and start a thread which will than copy the information into the client structure for its own use.

One thing you must remember to do is to clean up all pointers, closesocket( SOCKET ), on all opened sockets, and call WSACleanup() when your server shuts down.

Included with this article is a little program I wrote in linux to actually test the server. It will have more use for the next article, bur should provide a great demonstration of the Windows/Linux socket compatibilities.

In my next article I will address packet decoding and packet structures, as well as how to handle the different array of possible packets you could receive. I also intend to provide functioning cleanup code, and whatever else I can think of at that time.

If you have any suggestions or comments, please feel free to email them to me at, or you can even come online on to channel #gamedev, #necrosoft, or #Toronto and talk to me in there since I spend 98% of my internet life in there anyway.

Page : << Previous 2