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.
--
Cheers,
Rahul Anand
"Ricardo Quintanilla" wrote:
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