Click here to Skip to main content
Click here to Skip to main content

Multithreaded TCP/IP Telnet Server - Chess Game Example

By , 22 Jan 2001
Rate this:
Please Sign up or sign in to vote.

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

About the Author

Lee Patterson

United States United States
No Biography provided

Comments and Discussions

 
GeneralMy vote of 1 Pinmembershenf20-Dec-12 14:44 
General[Message Deleted] Pinmemberit.ragester2-Apr-09 21:57 
Questionhow do i run this program Pinmemberabhilash934-Mar-09 0:27 
GeneralWorks Fine PinmemberAngelPortal24-Nov-06 1:02 
Generalis this code support in llinux Environment Pinmembersheladiya chetan28-Aug-06 18:06 
GeneralChinese symbols Pinmemberhaha6244-May-05 21:31 
Generalwant a free server to test online card game like Rummy PinsussPongsak Maneekul18-Jul-04 21:43 
QuestionHow to send message to one client PinsussAnonymous12-May-03 18:06 
GeneralChess game PinmemberRedeemed2-Apr-03 14:45 
GeneralRe: Chess game PinmemberIceman2-Apr-03 15:25 
GeneralRe: Chess game Pinmemberwait15-Oct-04 1:00 
GeneralRe: Chess game Pinmemberpegasusplus8-Jun-05 0:40 
QuestionHey aint we brothers? PinmemberPat Patterson5-Jun-02 5:21 
GeneralSmall bug maybe PinmemberAnonymous20-Feb-02 12:59 
GeneralRe: Small bug maybe PinsussAnonymous11-Nov-02 17:54 
GeneralRe: Small bug maybe Pinmemberpatarak31-May-04 20:12 
GeneralRe: Small bug maybe PinmemberAngelPortal24-Nov-06 0:23 
QuestionAlternative Telnet implementations ? PinsussRob W20-Jul-00 9:49 
AnswerRe: Alternative Telnet implementations ? PinmemberAnonymous16-Jun-01 17:06 

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

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

| Advertise | Privacy | Mobile
Web04 | 2.8.140415.2 | Last Updated 23 Jan 2001
Article Copyright 1999 by Lee Patterson
Everything else Copyright © CodeProject, 1999-2014
Terms of Use
Layout: fixed | fluid