Click here to Skip to main content
15,868,016 members
Articles / Desktop Programming / MFC
Article

Using TCP/IP or UDP to Communicate with Serialized Objects

Rate me:
Please Sign up or sign in to vote.
4.24/5 (24 votes)
26 Sep 20053 min read 136.2K   7.5K   56   25
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.

MC++
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.

MC++
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

MC++
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

MC++
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


Written By
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

 
GeneralMy vote of 1 Pin
Member 995056721-Dec-14 12:41
Member 995056721-Dec-14 12:41 
QuestionError on 64 bytes win 7 Pin
Sylvain Plourde15-Jul-14 3:06
Sylvain Plourde15-Jul-14 3:06 
GeneralMy vote of 5 Pin
Mykola Panas K.28-May-13 0:48
professionalMykola Panas K.28-May-13 0:48 
QuestionUDP Server Send missing second argument Pin
Aaron Ortiz16-Jul-08 4:45
Aaron Ortiz16-Jul-08 4:45 
QuestionHow to rwceive msg when receiver is behind router Pin
hafidz13-Feb-08 14:37
hafidz13-Feb-08 14:37 
GeneralHelp with client side Pin
itziki11-Feb-08 5:18
itziki11-Feb-08 5:18 
I don't understant what needs to be done on the client side in order that it will be able to receive messages always (not only in the constructor)
QuestionPrint message in the Server Pin
michelangelo7626-Sep-07 4:25
michelangelo7626-Sep-07 4:25 
GeneralHandle Events Pin
Paul the Great3-Jul-07 2:54
Paul the Great3-Jul-07 2:54 
GeneralRe: Handle Events Pin
Roy Ganor3-Jul-07 3:06
Roy Ganor3-Jul-07 3:06 
GeneralWhen Server do not reply Pin
Pawlucha11-Apr-07 2:32
Pawlucha11-Apr-07 2:32 
QuestionOher type of object ? Pin
Chindril6-Feb-07 16:24
Chindril6-Feb-07 16:24 
AnswerRe: Oher type of object ? Pin
Roy Ganor7-Feb-07 20:05
Roy Ganor7-Feb-07 20:05 
GeneralPerfect work! Pin
mikeeblau26-Feb-06 0:10
mikeeblau26-Feb-06 0:10 
GeneralRe: Perfect work! Pin
Roy Ganor26-Feb-06 9:43
Roy Ganor26-Feb-06 9:43 
QuestionCan u help me Pin
Syed Mazhar Hasan Qadri20-Feb-06 8:56
Syed Mazhar Hasan Qadri20-Feb-06 8:56 
AnswerRe: Can u help me Pin
Roy Ganor26-Feb-06 9:42
Roy Ganor26-Feb-06 9:42 
GeneralNice Idea Pin
teleolog15-Feb-06 19:54
teleolog15-Feb-06 19:54 
GeneralRe: Nice Idea Pin
Roy Ganor20-Feb-06 17:39
Roy Ganor20-Feb-06 17:39 
QuestionNice concept; how about without .NET dependency? Pin
Mike D.5-Oct-05 7:19
Mike D.5-Oct-05 7:19 
AnswerRe: Nice concept; how about without .NET dependency? Pin
Roy Ganor8-Oct-05 9:56
Roy Ganor8-Oct-05 9:56 
GeneralGreat Pin
eternalsoul27-Sep-05 22:08
eternalsoul27-Sep-05 22:08 
AnswerRe: Great Pin
Roy Ganor27-Sep-05 22:33
Roy Ganor27-Sep-05 22:33 
GeneralRe: Great Pin
eternalsoul27-Sep-05 22:43
eternalsoul27-Sep-05 22:43 
GeneralSweet! Pin
Nitron26-Sep-05 14:33
Nitron26-Sep-05 14:33 
GeneralRe: Sweet! Pin
Roy Ganor26-Sep-05 21:09
Roy 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.