Edgar Huckert
 

TCP and UDP in C++


Socket based clients and servers

General remarks

TCP communication establishes a reliable point-to-point communication between a server and a client. TCP is based on the concept of a connection, UDP is said to be connection-less or "datagram based". Data are transmitted in packages, i.e. in blocks of data. You cannot send TCP packages without having opend a connection to a server. UDP packages can be sent without knowing that there is someone (a server) listening for packages. TCP packages arrive in the server in the same order as sent whereas UDP packages are not guaranteed to arrive in the same order - this is why UDP is said to be less reliable than TCP. On the IP level (i.e. one level beyond TCP/UDP) both types of packages are exchanged with the same degree of security. If they are sent and received on bot sides then no explicit confirmation (acknowledgement) is needed. The difference in reliability is not on the IP level - it is on the application level!

The differences between client and server is sometimes blurry. Normally a client starts a request and the server waits for a request. In many cases server are prestarted (often running in the background and waiting for requests) and clients are started as program or as thread.

A typical client - server scenario is given on the Internet: a typical server is a WEB server like Apache and the clients are the browsers like FireFox or Internet Explorer. WEB connections are often one-time connections, i.e. the server terminates the connection as soon as he has served the request. The next request from a client establishes a new TCP connection. Servers outside the WEB context normally keep the connection open until the client terminates.

Sample programs

This ZIP contains C++ sources and makefiles for three TCP classes called CLIENT_SOCKET, SERVER_SOCKET and MSGSOCKET. The two first classes deal with the TCP transport whereas the third class defines the contents (messages).

In the same ZIP you will find two sample applications called tcpcl4 and tcpsrv4 . While the client (tcpcl4) is rather basic the server (tcpsrv4) is a lot more complicated: this is a multithreaded server capable to run under Linux and Windows. This server can handle simultaneous requests from clients on the same port thus making the code difficult to understand if you are a beginner. Under Linux POSIX threads are used. Make files for Windows are included. I used here the C++ compiler from Digital Mars - but you can use the Microsoft or GNU compilers as well if you modify the make files. For a more sophisticated application using the same classes see hu_ftp below.

The ZIP file also includes a simple URL fetch program (a loader for HTML Pages) called urlfetch that uses the classes mentioned above. You can use this to download pages from web sites and store them (via redirection of the output) on your machine.

The ZIP also includes a simple file transfer program called hu_ftp acting as both a server and a client. This is a command line application i.e. it has no graphical interface. The program uses my classes SRVSOCKET, MSGSOCKET and CLIENT_SOCKET in a newer version. I designed this very simple program as I was tired to be forced to install a (usually large) ftp server. In contrast to LINUX the normal WINDOWS versions don't have ftp daemons (called ftpd or pure_ftpd) running in the background and waiting for ftp requests. Standard Windows only offers a ftp client as a console application. You can use this program with a direct network cable between two computers (a "crossover cable") or in a larger network using a switch. Communication over WLAN is also possible. The archive contains also a short user manual. Important note: the protocol is not identical with standard ftp. It doesn't have the same performance as it uses a flow direction change (an ACK message) for each package. I just demonstrates how a file transfer can work.

On my "Programming in D" page I offer a similar file transfer program entirely written in D. This program called hu_ftp2 ressembles much the C++ version mentioned above. It is however not totally compatible with the C++ version as I have modified the protocol a little bit. Like the C++ version the D version is portable between Windows and Linux.


UDP based logging

I never understood why the logging classes in Java and other programming paradigms are so complex - sometimes more complex than the applications that use these logging classes. Since 20 years I am fan of UDP based logging. I use this kind of logging in commmercial projects, in complex music software and in embedded projects using very small computers. Therefor I include here a portable UDP based logging receiver called ULS that shows incoming UDP messages and can write them into a file. This can also be used as a simple event logger accepting UDP input from small embedded devices like Arduino, Raspberry Pi, Beaglebone etc.

Here the reasons why I prefer UDP based logging:

A simple GUI for UDP based logging (a UDP server)

I have prepared two zips: one for Linux ( GTK2 based, uses wxWidgets V3.0.1) and one for Windows (uses wxWidgets V2.8.7). Both zips include identical sources and very simple make files (much simpler than the make files used normally in wxWidgets - why are these so complicated?). The client classes used to send UDP logging messages from applications ("hu_trace.xxx") are also included.

Screen dump for program ULS (the log receiver)

          


UDP based logging: a console application (a UDP server)

Here is the source code for a much simpler UDP based logging server called udp_server that I use since many years in projects. Normally I include in my C++ sources (also C#, Java etc.) trace functionality as shown in class HU_TRACE (see the uls*.zip mentioned above).

This is a simple command line (CLI, "black window") application that you can run under Windows or under Unix/Linux. If you need the trace output then redirect it into a file. This source code does not make use of the above mentioned classes - it uses the API calls for UDP based sockets directly from the operating system.

Screen dump for program udp_server (the log receiver)

          


A Simple UDP based client(UDP sender)

My u_trace.xxx logging classes mentioned above are UDP clients: they send data (logging messages) to an UDP server waiting for these messages. It is not important whether the UDP server has been started or not. Here is additionally a source code for a simple UDP client that sends the contents of a ASCII/ANSI encoded file to an UDP server like ULS or udp_server above. You can specify delays in milliseconds for the send operations.

There are two main reasons why such an UDP client (sender) is useful:

This is a simple command line ("black window") application that you can run under Windows or under Unix/Linux (it is portable). This source code does not make use of the above mentioned classes - it uses the API calls for UDP based sockets directly from the operating system.

On my "Programming in D" page I have included two sample programs demonstrating UDP sockets written in D. These two sample programs can be used to integrate an UDP based logger in D programs. A ready-for-use UDP based logger is contained in my hu_ftp2 file transfer program which is entirely written in D.


Copyright for all images, texts and software on this page: Dr. E. Huckert

Contact

If you want to contact me: this is my
mail address