Click here to Skip to main content
12,550,545 members (44,068 online)
Click here to Skip to main content
Add your own
alternative version


65 bookmarked

XML Based Communication library

, 18 Dec 2003
Rate this:
Please Sign up or sign in to vote.
XML based communication library to use command/object based through TCP/IP connections.


The TCP/IP communication requires a lot of processing of incoming and outgoing data. The XML communication library uses XML streams to transfer data between the TCP/IP connections. The main idea is to use predefined communication objects. These communication objects can be serialized into XML streams. This XML stream, with a very small header to identify the serialized object and the XML size, can be used to communicate.


XML communication library uses a straight forward idea:

  • Writing
    1. Define objects for each XML message wanted to be transferred.
    2. Serialize created object into XML stream.
    3. Determine the size of the generated XML stream.
    4. Construct communication header. I used a very simple header with the following format:
      CommandID:XML Stream Size:XML Stream String
    5. Write the constructed header/XML data to the network stream.
  • Reading
    1. Analyze incoming data for appropriate header (command ID, and XML data size)
    2. Use the header to read and deserialize into the appropriate object. This can be determined using the command ID.

This mechanism makes the communication more easier and more clear to code.

Using the code

The XML communication library is mainly based on one static class' operations. This class is called CommProtocol. This class defines a private default constructor to prevent any creation of objects for this class. This class has four static functions:

  1. Read: Reads data from network stream.
  2. Write: Writes data to network stream.
  3. ReadHeader: Reads incoming header from network stream. This function is used mainly by the Read function.
  4. ReadCommand: Deserialize the incoming XML stream to its appropriate object using the header information. This function is also mainly used by the Read function.

The following code describes how we can write to network stream using the CommProtocol class:

CommProtocol.Write(netStream,object,object type);

The Write function will take care of object serialization and header building, and writing to the network stream.

The following code describes how we can read from the network stream using the CommProtocol class:

object o = CommProtocol.Read(Networkstream,out cmd);
case /*your commands selector*/: 
/*Function to process the incoming object*/ 

The Read function will throw an exception if the network stream is closed or header is corrupted.

The XML serialized objects are defined in the library main file CommLib.cs. For the article code, I defined one class called CommSendText and one communication command called CommandSendText.

I developed the communication library mainly to help myself in the development of a server based chat application. The main issue I considered when I developed this library was to make the communication that simple. For this, I used double handshaking between the server and the clients. This means, each command sent to the server must return a status to the client. This makes the communication more reliable and up to date.

There are many enhancements I want to add to this library, so your feedback is very important to me.


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

Hesham Desouky
Architect Nebras Technology
Egypt Egypt
With a heavy C++/MFC background started my way to the DotNet and C# world.
Going from device drivers to standard windows applications development using C++/MFC since 1998, I started the switch to .Net technology in 2004.
Currently I am the Technical Development Manager and Software Architect of Nebras Technology (Medical Software Vendor) and one of its executives owners.

You may also be interested in...


Comments and Discussions

SuggestionDemofile here! Working Communication. Pin
ayeks13-May-13 22:47
memberayeks13-May-13 22:47 
GeneralDemo Pin
acheoacheo19-Oct-10 3:39
memberacheoacheo19-Oct-10 3:39 
GeneralHelp [modified] Pin
nintondo30-Nov-08 22:24
membernintondo30-Nov-08 22:24 
GeneralRe: Help Pin
fabianse22-Apr-10 7:35
memberfabianse22-Apr-10 7:35 
GeneralHELP!!!! Pin
tmn11418-Sep-06 11:29
membertmn11418-Sep-06 11:29 
AnswerRe: HELP!!!! Pin
Hesham Desouky18-Sep-06 12:11
memberHesham Desouky18-Sep-06 12:11 
QuestionReading more than one command at a time? Pin
Ikram Shaikh17-Nov-05 23:30
memberIkram Shaikh17-Nov-05 23:30 
AnswerRe: Reading more than one command at a time? Pin
Ikram Shaikh18-Nov-05 18:58
memberIkram Shaikh18-Nov-05 18:58 
GeneralProtocol Error, Header Corrupted Pin
EclecticFrog13-Sep-04 9:45
memberEclecticFrog13-Sep-04 9:45 
This code seems great for serializing and sending objects over TCP/IP. Thanks!

However I'm having a problem with it right now...

When my client connects to the server, the server sends the client 16 messages in quick succession.

The first 2 messages come across ok. After that I get 10 more read attempts which all fail with the exception (Protocol Error, Header Corrupted). (so total reads only equal 12, which makes me think this is a message length problem)

Has anyone had this problem or know why it might be happening?

Confused... Confused | :confused:

Many thanks!!

GeneralRe: Protocol Error, Header Corrupted Pin
Hesham Desuky17-Sep-04 6:49
memberHesham Desuky17-Sep-04 6:49 
GeneralRe: Protocol Error, Header Corrupted Pin
EclecticFrog25-Oct-04 15:55
memberEclecticFrog25-Oct-04 15:55 
GeneralNew at C# with a couple questions... Pin
James Bengard13-Jul-04 9:06
sussJames Bengard13-Jul-04 9:06 
GeneralRe: New at C# with a couple questions... Pin
Hesham Desuky14-Jul-04 10:24
memberHesham Desuky14-Jul-04 10:24 
GeneralRe: New at C# with a couple questions... Pin
Jonathan Merriweather19-Aug-04 7:40
memberJonathan Merriweather19-Aug-04 7:40 
GeneralRe: New at C# with a couple questions... Pin
FocusedWolf30-Apr-05 14:04
memberFocusedWolf30-Apr-05 14:04 
GeneralRe: New at C# with a couple questions... Pin
Hesham Desouky6-Jun-05 21:09
memberHesham Desouky6-Jun-05 21:09 
GeneralRe: New at C# with a couple questions... Pin
acheoacheo19-Oct-10 4:11
memberacheoacheo19-Oct-10 4:11 
General"There is an error in XML document" Pin
Chimerique17-May-04 14:13
memberChimerique17-May-04 14:13 
GeneralRe: "There is an error in XML document" Pin
Anonymous17-May-04 20:34
sussAnonymous17-May-04 20:34 
GeneralRe: "There is an error in XML document" Pin
Chimerique17-May-04 22:55
memberChimerique17-May-04 22:55 
GeneralRe: "There is an error in XML document" Pin
Chimerique17-May-04 23:06
memberChimerique17-May-04 23:06 
GeneralRe: "There is an error in XML document" Pin
Hesham Desuky5-Jun-04 21:30
memberHesham Desuky5-Jun-04 21:30 
GeneralProblem in Using DLL in Visual C++ 6.0 Pin
BeeAsif11-Nov-09 1:06
memberBeeAsif11-Nov-09 1:06 
GeneralRe: "There is an error in XML document" Pin
Hesham Desuky5-Jun-04 21:33
memberHesham Desuky5-Jun-04 21:33 
GeneralRe: "There is an error in XML document" Pin
pinartekin17-Apr-07 4:11
memberpinartekin17-Apr-07 4:11 
GeneralGood Pin
m a y s a m31-Dec-03 18:17
memberm a y s a m31-Dec-03 18:17 
GeneralRe: Good Pin
hesham desouky12-Jan-04 22:41
susshesham desouky12-Jan-04 22:41 
GeneralRe: Good Pin
h_Nebeway21-May-06 22:00
memberh_Nebeway21-May-06 22:00 
GeneralFix the appearance Pin
Jonathan de Halleux19-Dec-03 3:23
memberJonathan de Halleux19-Dec-03 3:23 
GeneralRe: Fix the appearance Pin
l a u r e n19-Dec-03 7:52
memberl a u r e n19-Dec-03 7:52 
GeneralRe: Fix the appearance Pin
Hesham Desokey19-Dec-03 22:46
memberHesham Desokey19-Dec-03 22:46 

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.

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.161021.1 | Last Updated 19 Dec 2003
Article Copyright 2003 by Hesham Desouky
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid