Does TcpClient have a lot of overhead?

I tried sending / receiving data using TcpClient. I did two experiments and found something interesting.

I am setting up TcpListener on a server in Japan and TcpClient in the UK. I kept sending 500 bytes to TcpListener, and when TcpListener sends 10KB back to TcpClient. I saved this send / receive cycle 500 times in each experiment.

Experiment 1:

In each send / receive cycle, I create a new TcpClient (time is ticking just before creation) and sends / receives

Experiment 2:

For all loops, I only have one TcpClient, and it maintains a connection to TcpListener and does send / receive 500 times.

Result:

Average time value for one cycle:

E1: 1.8 seconds, E2: 0.49 seconds.

I am very surprised by this result. Therefore, maintaining communication for continuous sending / receiving can save a lot of time ??? almost 2/3 of the time.

It's true?

thanks

==== ==== new

@Jon Skeet, @dbemerlin, Thanks for the answer. I guessed that Tcp handshakes take some time.

So, I did experiment 3.

I configure HttpListener as a server and use WebClient to send / receive, the data sizes are exactly the same. And every time I used a new WebClient to send / receive between the UK and Japan.

The result is 0.86 (the average of a 500-fold cycle, i.e. sending / receiving).

I assume the WebClient / HttpLisener itself is Tcp, right? How can they be faster than the raw TcpClient / TcpListener in my experiments?

Thanks again

+4
source share
2 answers

This is not particularly surprising, but it is not the cost of creating an object - it is the cost of setting up a TCP connection, shaking hands, etc.

If you can work hard on one connection, it is more efficient than creating a new connection every time. To put it in real terms, consider a telephone conversation between two people.

Effective scenario: you dial, pick up, speak, answer, talk, answer, etc.

Ineffective scenario: you dial numebr, they pick up, you say, they answer, you hang up. Then you immediately dial the number again, they pick up, you say, they answer, you hung up, etc.

Imagine what to do last really! You would go crazy very quickly ...

EDIT: By default, WebClient will leave the hanging connection open for the web server. If you force the connections to be reset (basically disable KeepAlive), you will again see slow behavior.

+6
source

Each TCP connection requires a handshake when it is created (for example, a three-way handshake, not sure atm) that even if data is not sent, the packet is sent to the target, the other is sent back and - if it is a three-way handshake - the third is sent to the target.

A packet is sent from the UK to Japan, for example, 100 ms. This means that each "tcpClient.Connect ()" requires 300 ms without sending any data. Your normal sending and receiving consists of one packet sent to the destination, and the other back, requiring a total of 200 ms. Shutting down (if clean) requires another 100 ms.

This will result in a message sending in 600 ms, compared to 200 ms if you maintain the connection as soon as you save the handshake and shutdown.

+1
source

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


All Articles