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

Using TCP/IP or UDP to Communicate with Serialized Objects

, 26 Sep 2005
Rate this:
Please Sign up or sign in to vote.
An article on transfering any serialized object through client and server.

TCP Schema


In this article, I will try to give you some tools to build your own simple client/server applications using the TCP/IP and UDP protocols.

The thing that is really special in this article is that it leaves you, the programmer, very little to handle with the network issues. You can build a server and client application in a single command, then send and receive objects between these two points, no parsing, no conversions, pure object messaging.


OK, let's have some network / communication definitions:

  • TCP/IP - Transmission Control Protocol. A method (protocol) used along with the Internet Protocol to send data in the form of message units between computers over the Internet. While IP takes care of handling the actual delivery of the data, TCP takes care of keeping track of the individual units of data (called a packet) that a message is divided into for efficient routing through the Internet.
  • UDP - User Datagram Protocol transports data as a connectionless protocol, using packet switching.

Two main issues differentiates UDP from TCP/IP protocols:

  1. TCP creates a connection while UDP is connectionless - that means that TCP can actually connect between a server and a client, so any communication will be using this connection. UDP doesn't connect the server to the client but enables you to send and receive from the server to any client and vice versa.
  2. TCP is a reliable way to send and receive data while UDP isn't - that means that if you want to send an important (like commands) message between two applications - use TCP, else (like images/voice streams) use UDP.

Understanding these two issues is the key to understanding the whole article.

Using the code

First of all, the classes:

  • NetworkTcpServer - handles the server side using TCP.
  • NetworkTcpClient - handles the client side using TCP.
  • NetworkUdpServer - handles the server side using UDP.
  • NetworkUdpClient - handles the client side using UDP.

Server side using TCP

Order of commands for the TCP server side:

  1. Start and initialize the TCP server.
  2. Wait for a new connection request.
  3. Giving a connection you can send and receive objects.
  4. Close the connection.
  5. Stop the TCP server.

Note that line number 2 is blocking, that means that the application waits until a new client tries to connect to the server.

try {

    // opens the listener
    Network::NetworkTcpServer* tcpServer = 
      new Network::NetworkTcpServer(S"localhost", 4567);

    // gets the first connection
    Network::NetworkTcpServer::Connection* connection = 

    // receives and sends an object through this connections
    Object* object = connection->recieve();


} catch(Exception* e) {
    Console::WriteLine(S"Error connecting the server");

My advice is to wrap the tcpServer->waitForNewConnection() command with a while loop, then start a thread with the Connection object that is returned. This way every thread handles its own connection (to a specific client).

Client side using TCP

Here the situation is simpler, just start the client and send and receive objects. As simple as that.

try {

    Network::NetworkTcpClient* tcpClient = 
             new Network::NetworkTcpClient(S"localhost", 4567);

    // Creating object to send
    ArrayList* list = new ArrayList();
    list->Add(S"Hello World!");
    list->Add(__box(Guid::NewGuid())) ;
    Console::WriteLine(S"Creating object (list)");

    // sending the object (list)
    Console::WriteLine(S"object has been just sent");

    // eceive an object from the server
    Object* object = tcpClient->recieve();
    Console::WriteLine(S"object received");


} catch(Exception* e) {
    Console::WriteLine(S"Error connecting the server");

Server side using UDP

try {

    // opens listener
    Network::NetworkUdpServer* udpServer = 
             new Network::NetworkUdpServer(4567);

    // sends and receives object through the two sides
    Object* object = udpServer->recieve();

} catch(Exception* e) {
    Console::WriteLine(S"Error connecting the server");
return 0;

Client side using UDP

try {

    // opens listener
    Network::NetworkUdpClient* udpClient = 
             new Network::NetworkUdpClient(S"localhost", 4567);

    // sends and receives object through the two sides
    udpClient->send(S"Hello World!");
    Object* object = udpClient->receive();


} catch(Exception* e) {
    Console::WriteLine(S"Error sending receiving messages");

Points of Interest

My initial needs were to build server/client applications that handle image streaming along with message passing. I could do it very simply by fixing a short message structure between the two applications. But then I understood that it will not be as flexible as I wanted.


  • 17/09/2005 - First version of my network classes.


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

Roy Ganor
Web Developer
Israel Israel
Undergraduate student, Computer Science in Tel Aviv University,
Developer in a Java team at Schema Ltd.
Programing since:
10 goto 20
20 goto 10

Comments and Discussions

QuestionError on 64 bytes win 7 PinmemberSylvain Plourde15-Jul-14 3:06 
GeneralMy vote of 5 PinmemberNikolay Aph K.28-May-13 0:48 
QuestionUDP Server Send missing second argument PinmemberAaron Ortiz16-Jul-08 4:45 
QuestionHow to rwceive msg when receiver is behind router Pinmemberhafidz13-Feb-08 14:37 
GeneralHelp with client side Pinmemberitziki11-Feb-08 5:18 
QuestionPrint message in the Server Pinmembermichelangelo7626-Sep-07 4:25 
GeneralHandle Events PinmemberPaul the Great3-Jul-07 2:54 
GeneralRe: Handle Events PinmemberRoy Ganor3-Jul-07 3:06 
GeneralWhen Server do not reply PinmemberPawlucha11-Apr-07 2:32 
QuestionOher type of object ? PinmemberChindril6-Feb-07 16:24 
AnswerRe: Oher type of object ? PinmemberRoy Ganor7-Feb-07 20:05 
GeneralPerfect work! Pinmembermikeeblau26-Feb-06 0:10 
GeneralRe: Perfect work! PinmemberRoy Ganor26-Feb-06 9:43 
QuestionCan u help me PinmemberSyed Mazhar Hasan Qadri20-Feb-06 8:56 
AnswerRe: Can u help me PinmemberRoy Ganor26-Feb-06 9:42 
GeneralNice Idea PinmemberNay Ko Oo15-Feb-06 19:54 
GeneralRe: Nice Idea PinmemberRoy Ganor20-Feb-06 17:39 
QuestionNice concept; how about without .NET dependency? PinmemberMike D.5-Oct-05 7:19 
AnswerRe: Nice concept; how about without .NET dependency? PinmemberRoy Ganor8-Oct-05 9:56 
GeneralGreat Pinmembereternalsoul27-Sep-05 22:08 
AnswerRe: Great PinmemberRoy Ganor27-Sep-05 22:33 
GeneralRe: Great Pinmembereternalsoul27-Sep-05 22:43 
GeneralSweet! PinmemberNitron26-Sep-05 14:33 
GeneralRe: Sweet! PinmemberRoy Ganor26-Sep-05 21:09 

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
Web02 | 2.8.141022.1 | Last Updated 26 Sep 2005
Article Copyright 2005 by Roy Ganor
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid