Why Socket is faster than TcpClient?

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

i had a problem whom i do not know how to explain.

i was using a TcpClient (System.Net.Sockets.TcpClient) object to send and
receive data to an AS400 socket. Two months ago it started to work slowly,
about 4 seconds between send and receive. In our production environment with
hundreds of transactions it was truly costly.

a while ago i changed de TcpClient object. Now i am using a Socket
(System.Net.Sockets.Socket) object and it resulted faster.
i mean System.Net.Sockets.Socket is faster than System.Net.Sockets.TcpClient

how can i explain that?
why Socket is faster than TcpClient?
Does TcpCliente do some extra work?

thanks to all
 
Ricardo Quintanilla said:
i had a problem whom i do not know how to explain.

i was using a TcpClient (System.Net.Sockets.TcpClient) object to send and
receive data to an AS400 socket. Two months ago it started to work slowly,
about 4 seconds between send and receive. In our production environment with
hundreds of transactions it was truly costly.

Not sure if this will fix your problem or not.
Here goes...

I ran into a problem last year where the creation of a TcpClient was taking ~5 sec
Once the connection was up everything was fine.
I traced it down to a problem with DNS being very slow.
Instead of using
public TcpClient(string hostname, int port);
I used
public TcpClient(IPEndPoint localEP);

I created the IPEndPoint and used it every time I needed to Open the connection.
Performance was no problem after that.
The tricky part was converting the 127.0.0.1 style IP address to a long
I wasn't 100% sure about Endianness and didn't find any automatic conversion utilities.

There is probably some method somewhere to do the job, but I just rolled my own. (pretty simple)

Hope this helps
Bill
 
I did a little bit testing and benchmarking the socket based communication
and TcpClient base communication. I really did not find much difference in
the execution timings.

I am using the following loop for TcpClient based communication, to benchmark:

<CODE>

TcpClient client = new TcpClient("hostname", 2021);
while(iter++ < maxCount)
{
client.GetStream().Write(bytesSent, 0, bytesSent.Length);
Console.WriteLine("Sending data...");
do
{
bytes = client.GetStream().Read(bytesReceived, 0, 256);
response += Encoding.ASCII.GetString(bytesReceived, 0, bytes);
}
while(client.GetStream().DataAvailable);
Console.WriteLine(".. Data Received");
}

</CODE>

And following similar loop for Socket based communication

<CODE>

Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Stream,
ProtocolType.Tcp);
s.Connect(remoteEndPoint);
while(iter++ < maxCount)
{
// Send request to the server.
Console.WriteLine("Sending Data...");
s.Send(bytesSent);
int bytes = 0;
do
{
bytes = s.Receive(bytesReceived);
strReceived += Encoding.ASCII.GetString(bytesReceived, 0, bytes);
}
while (s.Available > 0);
Console.WriteLine("...Data Received");
}

</CODE>

Following are my observations:

Run 1
==========

Iteration Count Time Taken (ms)
.NET Socket TcpClient
4 15.625 15.625
10 0 15.625
100 218.75 171.875
500 1203.125 1234.375
1000 5671.875 5718.75

Run 2
=========

Iteration Count Time Taken (ms)
.NET Socket TcpClient
4 0 15.625
10 0 0
100 125 234.375
500 1359.375 1203.125
1000 5796.875 5750

Run 3
=========

Iteration Count Time Taken (ms)
.NET Socket TcpClient
4 0 15.625
10 0 0
100 234.375 218.75
500 1187.5 1171.875
1000 5718.75 5734.375

Note: Iteration count here is number of transactions (send/receive a block
of data).

I am not able to reach at any conclusion. I guess somebody from community
might be able to explain the things.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top