Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / desktop / MFC

Using TCP/IP or UDP to Communicate with Serialized Objects

4.24/5 (24 votes)
26 Sep 20053 min read 4   7.6K  
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