Multithreaded TCP/IP Telnet Server - Chess Game Example






3.64/5 (15 votes)
Nov 25, 1999
3 min read

194421

6962
Chess Game Example
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:
- Create an Accept thread to accept connections on port 23 (telnet)
- Get a connection, create a new accept thread to be able to handle more connections.
- Log this person in, getting user name.
- Create a lobby of sorts where you can do simple chat, create a new game, join a game, or watch a game.
- This lobby reads a line of input from the socket, and then checks what the command the user typed was via ParsCmd.
- When the person creates a game, we add the game object to a pending list, and wait for someone to join the game.
- 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.
- 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.