Multithreaded Telnet Server - Chess Game Example


Desktop-as-a-Service Designed for Any Cloud ? Nutanix Frame

This sample server demonstraites a server that can handle multiple TCP/IP connections. It also shows how to make a simple telnet server that could be used for things like administrating a server, making a chat room, or even creating a MUD.

The project that I chose was a chess server that could handle multiple games of Chess, with multiple players & watchers. This sample project uses a very handy blocking socket class called CBlockingSocket. The blocking socket class was found in the book titled Inside Visual C++ 5.0 by MS PRESS. This class is free, and can be used where ever, by whom ever. The class handles timeouts and errors by throwing an exception, so you aren't checking for errors all the time.

In order to facilitate making a telnet server, I derived a CTelnetSocket class from CBlockingSocket to handle mundane tasks like reading an entire line of data from the socket & printing text to the socket.

This is a very simple console application that demontrates:

  • Internet Server that accepts telnet connections.
  • Using multiple threads to handle more then one connection.
  • Blocking socket class for reading/writting/accepting connections from sockets.
    (I'd recomend this class over the MS CSocket and such, as they still have some 16-bit stuff in them, and this class ports to Unix a lot easier. That was the main goal of my program.)

Also included:

  • Simple linked list class that can be used anywhere (unix/mfc app/console app).
    Note: The linked list class is not thread safe.

The project and sample is included in ChessServer.zip. Use telnet as a client to connect to this server (ie: "telnet localhost"). You need to make sure your telnet client sends both CR & LF's.

See server.cpp for additional comments.


Download source & Sample - 49 KB



  • fatal error C1083: Cannot open include file: 'iostream.h': No such file or directory

    Posted by RNEELY on 03/31/2008 08:51pm

    // replace #include with: #include using namespace std;

  • replace #include <llist.h> by #include "llist.h" to avoid error...

    Posted by Legacy on 12/29/2003 08:00am

    Originally posted by: dupont

    replace #include <llist.h> by #include "llist.h" to avoid error...

  • I think NULL != INVALID_SOCKET ...

    Posted by Legacy on 10/20/2003 07:00am

    Originally posted by: Witek

    it's funny but, is it error?

    in CBlockingSocket::Create we have

    if((m_hSocket = socket(AF_INET, nType, 0)) == INVALID_SOCKET){..}

    INVALID_SOCKET == ~0 (-1),

    In other words NULL can be correct socket value, but in all program You have:
    ASSERT(m_hSocket != NULL); or ASSERT(m_hSocket == NULL);
    If I understand well, then function socket() can return correct value 0 (NULL), but then program will give errors.

    I don't think, that this situation is possible (in this case), but better replace NULL by INVALID_SOCKET.


  • That's the beauty of a code

    Posted by Legacy on 07/14/2003 07:00am

    Originally posted by: Dani Thapa

    Hey, thanks man for such a beautyful code that helps students like us for network programming(especially for client server relation).you have done a great job with simple looking code.keep it up.

  • Thanks, helped me fix precompiled header compile error C1010

    Posted by Legacy on 05/23/2003 07:00am

    Originally posted by: Jens Winslow

    Thanks, this project showed my how to get CBlockingSocket to compile.

    For those who are getting a compile error
    "fatal error C1010: unexpected end of file while looking for precompiled header directive"
    when trying to compile code using CBlockingSocket, the problem is that as stated in h file:
    // needs winsock.h in the precompiled headers

    However, it may not be clear how to do this - at least to me it was not. This project showed me how.

    1) In Project | Settings choose C++ tab
    2) In Category, choose "Precompiled headers"
    3) Change "Use precompiled header file (.pch)" radio to "Automatic use of precompiled headers"

    Thanks for posting this project.

  • send me

    Posted by Legacy on 04/15/2003 07:00am

    Originally posted by: ch masroor

    router developement using c or c++, i need for help , send me if u can
    special thanks
    Ch masroor

  • Voice Chat, Please

    Posted by Legacy on 01/26/2003 08:00am

    Originally posted by: Manju

    I saw the given. Will you please provide code for a simple voice chat program in VC++.

    Also can you include code for Traffic Watcher in VC++.
    Thanking you,

  • Nice

    Posted by Legacy on 01/22/2003 08:00am

    Originally posted by: Mulock!

    This code is great had to change a few things to get it to work in vc6 but other than that it's awsome.

  • ReadLine function..?

    Posted by Legacy on 01/19/2003 08:00am

    Originally posted by: Charlie

    I've modified your chess server program to operate strictly as a chat server (I added user name stuff, and connect and disconnect notification), but I want to write an MFC dialog based client. When a message is sent as a big string, as opposed to how the standard windows telnet program does it, the server crashes. How can I modify ReadLine() to accept long strings for messages instead of individual characters (if that is how it works)? If you want to see my code, e-mail me, and I'll send it to you.

    • web server

      Posted by Carlos on 02/12/2015 04:24pm

      Hi Charlie I read your comment at codeguru.com... would you kindly uppload that chat server you talked about? I´m new at web servers and I need some help to get around a school´s task... Thanks a lot in advance!

  • Confused.

    Posted by Legacy on 04/03/2002 08:00am

    Originally posted by: Mark

    i have a question. i'm quite new to all this, nonetheless, i get an "Unhandled exception in Project.exe: 0xC0000005: Access Violation" and the debugger points to ASSERT line below.

    Does this mean I have to be connected to the device on the other end for this not to happen. In other words m_hSocket (a handle) is not set cause it was not created?
    How do i get around this for nwo.

    INT CBlockingSocket::Send(const char* pch, const INT nSize, const INT nSecs)
    ASSERT( m_hSocket != NULL );

    I have a button that i "hit" that will send data. Here's my call to "Send" in CBlockingSocket

    nReturnCode = m_pSocket->Send( (char*)&eRequestCode,
    1 );

  • Loading, Please Wait ...

  • You must have javascript enabled in order to post comments.

Leave a Comment
  • Your email address will not be published. All fields are required.

Most Popular Programming Stories

More for Developers

RSS Feeds

Thanks for your registration, follow us on our social networks to keep up-to-date