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
source share