Click here to Skip to main content
12,297,601 members (37,998 online)
Click here to Skip to main content
Add your own
alternative version

Stats

98.9K views
6.4K downloads
52 bookmarked
Posted

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

Introduction

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.

Background

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 = 
                      tcpServer->waitForNewConnection();

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

    connection->Close();
    tcpServer->stop();

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

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)
    tcpClient->send(list);
    Console::WriteLine(S"object has been just sent");


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

    tcpClient->stop();

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

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();
    udpServer->send(object);

    udpServer->stop();
} catch(Exception* e) {
    Console::WriteLine(S"Error connecting the server");
    Console::WriteLine(e->Message);
}
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();

    udpClient->stop();

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

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.

History

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

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

Share

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

You may also be interested in...

Comments and Discussions

 
GeneralMy vote of 1 Pin
Member 995056721-Dec-14 12:41
memberMember 995056721-Dec-14 12:41 
QuestionError on 64 bytes win 7 Pin
Sylvain Plourde15-Jul-14 3:06
memberSylvain Plourde15-Jul-14 3:06 
GeneralMy vote of 5 Pin
Nikolay Aph K.28-May-13 0:48
memberNikolay Aph K.28-May-13 0:48 
QuestionUDP Server Send missing second argument Pin
Aaron Ortiz16-Jul-08 4:45
memberAaron Ortiz16-Jul-08 4:45 
QuestionHow to rwceive msg when receiver is behind router Pin
hafidz13-Feb-08 14:37
memberhafidz13-Feb-08 14:37 
GeneralHelp with client side Pin
itziki11-Feb-08 5:18
memberitziki11-Feb-08 5:18 
QuestionPrint message in the Server Pin
michelangelo7626-Sep-07 4:25
membermichelangelo7626-Sep-07 4:25 
GeneralHandle Events Pin
Paul the Great3-Jul-07 2:54
memberPaul the Great3-Jul-07 2:54 
GeneralRe: Handle Events Pin
Roy Ganor3-Jul-07 3:06
memberRoy Ganor3-Jul-07 3:06 
GeneralWhen Server do not reply Pin
Pawlucha11-Apr-07 2:32
memberPawlucha11-Apr-07 2:32 
QuestionOher type of object ? Pin
Chindril6-Feb-07 16:24
memberChindril6-Feb-07 16:24 
AnswerRe: Oher type of object ? Pin
Roy Ganor7-Feb-07 20:05
memberRoy Ganor7-Feb-07 20:05 
GeneralPerfect work! Pin
mikeeblau26-Feb-06 0:10
membermikeeblau26-Feb-06 0:10 
GeneralRe: Perfect work! Pin
Roy Ganor26-Feb-06 9:43
memberRoy Ganor26-Feb-06 9:43 
QuestionCan u help me Pin
Syed Mazhar Hasan Qadri20-Feb-06 8:56
memberSyed Mazhar Hasan Qadri20-Feb-06 8:56 
AnswerRe: Can u help me Pin
Roy Ganor26-Feb-06 9:42
memberRoy Ganor26-Feb-06 9:42 
GeneralNice Idea Pin
Nay Ko Oo15-Feb-06 19:54
memberNay Ko Oo15-Feb-06 19:54 
GeneralRe: Nice Idea Pin
Roy Ganor20-Feb-06 17:39
memberRoy Ganor20-Feb-06 17:39 
QuestionNice concept; how about without .NET dependency? Pin
Mike D.5-Oct-05 7:19
memberMike D.5-Oct-05 7:19 
AnswerRe: Nice concept; how about without .NET dependency? Pin
Roy Ganor8-Oct-05 9:56
memberRoy Ganor8-Oct-05 9:56 
GeneralGreat Pin
eternalsoul27-Sep-05 22:08
membereternalsoul27-Sep-05 22:08 
AnswerRe: Great Pin
Roy Ganor27-Sep-05 22:33
memberRoy Ganor27-Sep-05 22:33 
GeneralRe: Great Pin
eternalsoul27-Sep-05 22:43
membereternalsoul27-Sep-05 22:43 
GeneralSweet! Pin
Nitron26-Sep-05 14:33
memberNitron26-Sep-05 14:33 
GeneralRe: Sweet! Pin
Roy Ganor26-Sep-05 21:09
memberRoy Ganor26-Sep-05 21:09 

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.160525.2 | Last Updated 26 Sep 2005
Article Copyright 2005 by Roy Ganor
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid