Multithreaded Telnet Server - Chess Game Example

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.

Downloads

Download source & Sample - 49 KB

History



Comments

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

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

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

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

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

    Originally posted by: dupont

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

    Reply
  • I think NULL != INVALID_SOCKET ...

    Posted by Legacy on 10/20/2003 12: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),
    so NULL != INVALID_SOCKET.

    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.

    Witek

    Reply
  • That's the beauty of a code

    Posted by Legacy on 07/14/2003 12: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.
    

    Reply
  • Thanks, helped me fix precompiled header compile error C1010

    Posted by Legacy on 05/23/2003 12: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.

    Reply
  • send me

    Posted by Legacy on 04/15/2003 12: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

    Reply
  • Voice Chat, Please

    Posted by Legacy on 01/26/2003 12: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,
    Manju

    Reply
  • Nice

    Posted by Legacy on 01/22/2003 12: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.

    Reply
  • ReadLine function..?

    Posted by Legacy on 01/19/2003 12: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.

    Reply
  • Confused.

    Posted by Legacy on 04/03/2002 12: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

    try
    {
    nReturnCode = m_pSocket->Send( (char*)&eRequestCode,
    sizeof( E_PMA_COMM_REQUEST_CODES ),
    1 );
    }

    Reply
  • Loading, Please Wait ...

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

Top White Papers and Webcasts

  • Protecting business operations means shifting the priorities around availability from disaster recovery to business continuity. Enterprises are shifting their focus from recovery from a disaster to preventing the disaster in the first place. With this change in mindset, disaster recovery is no longer the first line of defense; the organizations with a smarter business continuity practice are less impacted when disasters strike. This SmartSelect will provide insight to help guide your enterprise toward better …

  • Java developers know that testing code changes can be a huge pain, and waiting for an application to redeploy after a code fix can take an eternity. Wouldn't it be great if you could see your code changes immediately, fine-tune, debug, explore and deploy code without waiting for ages? In this white paper, find out how that's possible with a Java plugin that drastically changes the way you develop, test and run Java applications. Discover the advantages of this plugin, and the changes you can expect to see …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds