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:
- 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.
- 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:
- Start and initialize the TCP server.
- Wait for a new connection request.
- Giving a connection you can send and receive objects.
- Close the connection.
- 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 {
Network::NetworkTcpServer* tcpServer =
new Network::NetworkTcpServer(S"localhost", 4567);
Network::NetworkTcpServer::Connection* connection =
tcpServer->waitForNewConnection();
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);
ArrayList* list = new ArrayList();
list->Add(S"Hello World!");
list->Add(__box(Guid::NewGuid())) ;
Console::WriteLine(S"Creating object (list)");
tcpClient->send(list);
Console::WriteLine(S"object has been just sent");
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 {
Network::NetworkUdpServer* udpServer =
new Network::NetworkUdpServer(4567);
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 {
Network::NetworkUdpClient* udpClient =
new Network::NetworkUdpClient(S"localhost", 4567);
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.