Hello Marine, there's a site which gives excellent example of a step-by-step build of a client/server program using the MFC CSocket class. It is at:
Take into account that these are STREAM sockets. If I was going to write an online chess game, I would probably opt for the much easier UDP (datagram) sockets. UDP sockets are "connectionless" - you simply create a CAsyncSocket object, bind it to a port and IP address and then send some info. You can have your send/recieve functions for the socket in one thread that continually loops and listens for incoming data. This isn't actual code sample but it should give you an idea of how UDP sockets work:
[tt]// create your CAsyncSocket here!
// Bind the socket to a port/IP address here!
// other stuff here..
int bytesRead = -1;
CString msgOut = "";
// now do a continuous loop here...
while(1)
{
bytesRead = mySocket.ReceiveFrom(....);
if (bytesRead > 0)
{
// we got some data in - process it here!
}
// to send a message out on the socket, we simply
// assign some data to the "msgOut" CString:
if (msgOut.GetLength()>0)
{
// send the data out here...
socket.SendTo(......);
// dont forget to clear the string once the
// message has been sent so it doesn't get sent again
msgOut = "";
}
}[/tt]
A couple of points to note about UDP sockets - they are not as relaible as STREAM sockets
BUT this only means you may drop one package in every 1,000 sent. For something like a chess game - this is not a big deal!
Also, it will be far, far easier to write your "servlet" application PLUS that servlet app will not need to be spawning new threads/processes for each new connection it receives because UDP sockets are connectionless!
Your servlet application (the program that sits on your web server and listens for incoming requests) can be written in a variety of languages including C/C++, Perl, Java, etc. (these all handle UDP sockets very well).
In a recent issue of CUJ Magazine (C/C++ User's Journal), there was an article stating that many applications would be better off being in UDP format rather than streaming sockets.
There's also a PDF document available for download off the web called "Beej's Guide to Network Programming" that'll give you a full insight into all types of sockets and also several examples of client/server programs written in C/C++. Although mainly for UNIX, 99% of it applies to windows too.
Hope this helps!
programmer (prog'ram'er), n A hot-headed, anorak wearing, pimple-faced computer geek.