How can I compare TCP and UDP using C?

I want to measure message delay and throughput for both TCP and UDP using sockets. I am writing client.c and server.c (question 1: should I?) That runs on different servers to accomplish this. I have to make several measurements using different packet sizes and several samples and compile the results.

To delay messages: Send a packet of different sizes and measure the time at both ends and divide it by 2 to get latency. Question 2: I send a packet of size x, and then from server.c I send the packet back. Therefore, at the client, I start the timer, send the packet, and then wait until I receive the packet, and then stop the timer. Timer / 2 - my latency? What are the steps to measure this?

For bandwidth: Question 3: How to measure the bandwidth of both? What are the steps for doing this?

I'm starting programming Unix sockets in C, so the details will be helpful, with particular attention to the bechnmarking aspect.

EDIT: I cannot use pre-existing tools. I need to write my own.

Thanks!

+4
source share
4 answers

There is already a program called ttcp that checks the performance of TCP and UDP:

DESCRIPTION

Ttcp multiplies the transmission and reception of data between two systems using the UDP or TCP protocols.

It does not calculate latency, but you can take a look at the source code of this program to learn how it performs other calculations. And you can also use this as a client or server if you do not want to write both.

Update: other similar TCP test programs

as well as related programs

+2
source

Question 1: You can probably find existing test cases with some use of Google. But if there are special situations that you need to deal with, then it probably makes sense to write your own so that you take this into account.

Question 2: Perhaps there is an official definition of β€œlatency” with respect to network operations, but I think what really matters is the cost of a round trip. Thus, I would not divide by 2. The total cost of a round trip is what the user (client) is experiencing. It will be the latency that they see.

Question 3: I think your plan to use different packet sizes is good for measuring throughput. There is another SO question related to this. I sent an answer to this question and provided some numbers from my own testing of UDP compared to TCP. This may be of interest as a sanity checkpoint.

A - I forgot one thing that I would like to mention. If you write a very simple test case with UDP, this may be somewhat unrealistic. If you use UDP directly, you will need to add your own error handling, packet checking, etc. This will increase the cost. In the end, we find that UDP is faster in most situations for us because we adapted it for our own use. But for this, of course, much more code is required for everything to work correctly.

+1
source

You can see the tools that already exist for this type of thing. Perhaps D-ITG or iperf combined with tcpdump or wireshark ? If you do not intend to learn more about socket programming itself.

0
source

Source: https://habr.com/ru/post/1302622/


All Articles