Click here to Skip to main content
15,892,797 members
Articles / Desktop Programming / MFC
Article

Multithreaded TCP/IP Telnet Server - Chess Game Example

Rate me:
Please Sign up or sign in to vote.
3.64/5 (15 votes)
22 Jan 20013 min read 193.1K   7K   74   19
Chess Game Example

Sample Image

Introduction

This sample server demonstrates 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 demonstrates:

  • Internet Server that accepts telnet connections.
  • Using multiple threads to handle more then one connection.
  • Blocking socket class for reading/writing/accepting connections from sockets. (I'd recommend 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.

Details

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

I'd be happy to field any questions about the workings of the server, or how I set things up, but I'm not interested in fixing up this server, or fixing any of the "chess game" bugs. Any bugs found in the socket classes I would be interested in, although they are thoroughly tested.

I built this server one night as a demo for a company in hopes of getting a job there. Didn't know a damn thing about sockets so I used, and built upon a blocking socket class that was found in Inside Visual C++ 5.0 by MS PRESS.M

There are no rules to either game, so players can move whatever piece to whatever square. There isn't even any error checking, and you can't backspace. This is a very limited server in that respect.

The layout of the server is:

  1. Create an Accept thread to accept connections on port 23 (telnet)
  2. Get a connection, create a new accept thread to be able to handle more connections.
  3. Log this person in, getting user name.
  4. Create a lobby of sorts where you can do simple chat, create a new game, join a game, or watch a game.
  5. This lobby reads a line of input from the socket, and then checks what the command the user typed was via ParsCmd.
  6. When the person creates a game, we add the game object to a pending list, and wait for someone to join the game.
  7. When a person joins a game, we check for the game name on the pending list, and assign that person as the second player on that game object.
  8. When you WATCH a game, you are added to a list of watches on that game object.

The game objects:

The base class is the BoardGame class. Chess and Checkers games are derived from BoardGame. This is to make a consistent interface for adding players to a board game, and drawing the board.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralMy vote of 1 Pin
shenf20-Dec-12 14:44
shenf20-Dec-12 14:44 
General[Message Deleted] Pin
it.ragester2-Apr-09 21:57
it.ragester2-Apr-09 21:57 
Questionhow do i run this program Pin
abhilash934-Mar-09 0:27
abhilash934-Mar-09 0:27 
GeneralWorks Fine Pin
AngelPortal24-Nov-06 1:02
professionalAngelPortal24-Nov-06 1:02 
Generalis this code support in llinux Environment Pin
Chetan Sheladiya28-Aug-06 18:06
professionalChetan Sheladiya28-Aug-06 18:06 
GeneralChinese symbols Pin
haha6244-May-05 21:31
haha6244-May-05 21:31 
Generalwant a free server to test online card game like Rummy Pin
Pongsak Maneekul18-Jul-04 21:43
sussPongsak Maneekul18-Jul-04 21:43 
QuestionHow to send message to one client Pin
Anonymous12-May-03 18:06
Anonymous12-May-03 18:06 
GeneralChess game Pin
Redeemed2-Apr-03 14:45
Redeemed2-Apr-03 14:45 
GeneralRe: Chess game Pin
Iceman2-Apr-03 15:25
Iceman2-Apr-03 15:25 
Try including process.h
GeneralRe: Chess game Pin
wait15-Oct-04 1:00
wait15-Oct-04 1:00 
GeneralRe: Chess game Pin
pegasusplus8-Jun-05 0:40
pegasusplus8-Jun-05 0:40 
QuestionHey aint we brothers? Pin
5-Jun-02 5:21
suss5-Jun-02 5:21 
GeneralSmall bug maybe Pin
20-Feb-02 12:59
suss20-Feb-02 12:59 
GeneralRe: Small bug maybe Pin
Anonymous11-Nov-02 17:54
Anonymous11-Nov-02 17:54 
GeneralRe: Small bug maybe Pin
patarak31-May-04 20:12
patarak31-May-04 20:12 
GeneralRe: Small bug maybe Pin
AngelPortal24-Nov-06 0:23
professionalAngelPortal24-Nov-06 0:23 
QuestionAlternative Telnet implementations ? Pin
Rob W20-Jul-00 9:49
Rob W20-Jul-00 9:49 
AnswerRe: Alternative Telnet implementations ? Pin
16-Jun-01 17:06
suss16-Jun-01 17:06 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.