Click here to Skip to main content
15,902,939 members
Articles / Programming Languages / C#
Article

C# Sockets and Serialization

Rate me:
Please Sign up or sign in to vote.
1.61/5 (35 votes)
9 Jun 20032 min read 121.7K   2.1K   37   17
A simple game that runs based on client server programming.

Introduction

Dots game is a simple nice multiplayer game. Each player draws a line on a grid and gives the turn to other players. The aim is to get as much boxes as possible. The original game downloaded from the Internet lacked a lot of fun because it was playable on only one machine. There is a need for a way to play the game where each player uses his own computer and play his turn.

The project is a multiplayer game that can be played across LANs and Internet. The project intends to allow people to play across networks which solve the problem of taking turns. Also, making it playable via networks allow remote people to play together.

In order to develop the game, the following concepts were used:

  • Serialization.
  • Multithreading.
  • Logic Synchronization.

The idea of the program is to create two sockets for each player. One handles the game messages and the other handles the chatting part. When the player first runs the program, he has two basic options. Either Host or Join a game. To join a game, the player must have the IP address of the person that he will play with.

Serialization is used to pass an object that has all of the latest board information. So, whenever a player makes a move, that move is passed to the server which then passes it to the other players. But since the current implementation is limited to two players, the server receives the information, updates, and then allows a move send that moves to the player which then repeats the process.

Multithreading was needed in many places since there is extensive use of GDI commands. But the main part where it is used is chatting. For the chatting, there is a thread to handle incoming messages and pass them to the GUI.

The most crucial and difficult part was the synchronization. It is very fortunate that TCP handles many issues. But there were still many things to handle. Like turns, blocking, winning, scores, etc. The messy code impacted a lot which highlighted the importance of organized design and OOP design methods, even if it was simple; as long as there is messaging and other OOP concepts.

The following are the limitations of the final product:

  • Only two players can play.
  • Cannot play if behind a firewall or a NAT.
  • Players must agree before playing in order for them to know their IPs.
  • Since it is implemented in C#, it will not run on machines without .NET runtime environment.

These are the lines for the serialization part:

C#
IFormatter sender = new BinaryFormatter();
sender.Serialize(this.ns[n],o);
// ...
IFormatter getter = new BinaryFormatter();
if(ns[n].DataAvailable)
{
   packet = (Packet) getter.Deserialize(ns[n]);
   ns[n].Flush();
   return packet;
}

References:

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
Engineer
Saudi Arabia Saudi Arabia
Fadhel Al-Hashim, I study Software Engineering at KFUPM Dhahran, Saudi Arabia. Interested in Grphics and video production.

Comments and Discussions

 
GeneralMy vote of 1 Pin
root0013-Mar-12 7:54
root0013-Mar-12 7:54 
GeneralMy vote of 2 Pin
murphythefrog29-Jul-10 16:48
murphythefrog29-Jul-10 16:48 
General[My vote of 1] poor Pin
fatho121-Nov-09 0:44
fatho121-Nov-09 0:44 
General[Message Deleted] Pin
it.ragester2-Apr-09 21:52
it.ragester2-Apr-09 21:52 
Generalthat's a good job! Pin
chuong tahiti27-Nov-08 16:41
chuong tahiti27-Nov-08 16:41 
Generalif you dont know what your doing dont do it at all... Pin
owolf17-Sep-08 3:03
owolf17-Sep-08 3:03 
GeneralSorry but... Pin
sharpiesharpie31-Aug-07 15:19
sharpiesharpie31-Aug-07 15:19 
Generallive audio/video on separate sockets Pin
duaaali8-Jun-06 19:47
duaaali8-Jun-06 19:47 
GeneralCool! Pin
vn133712-Oct-05 7:30
vn133712-Oct-05 7:30 
GeneralThe best of the best Pin
AghaKhan21-Sep-04 14:18
AghaKhan21-Sep-04 14:18 
GeneralRe: The best of the best Pin
mattdruid27-Oct-05 8:19
mattdruid27-Oct-05 8:19 
GeneralExcellent Pin
morefire19-Aug-03 3:54
morefire19-Aug-03 3:54 
GeneralGood Stuff Pin
Avitchy14-Jun-03 3:53
sussAvitchy14-Jun-03 3:53 
GeneralSh*t Pin
Steve McLenithan10-Jun-03 4:35
Steve McLenithan10-Jun-03 4:35 
GeneralRe: Sh*t Pin
Paul Watson10-Jun-03 6:27
sitebuilderPaul Watson10-Jun-03 6:27 
GeneralRe: Sh*t Pin
Steve McLenithan10-Jun-03 6:31
Steve McLenithan10-Jun-03 6:31 
Generalarrogant Pin
tzehawhuang14-Jun-03 2:42
tzehawhuang14-Jun-03 2:42 

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.