By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,289 Members | 1,351 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,289 IT Pros & Developers. It's quick & easy.

Why Socket is faster than TcpClient?

P: n/a
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
Nov 17 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
Here some links with information about the differences between TcpClient &
Socket....

http://www.kdkeys.net/forums/4002/ShowPost.aspx
http://www.dotnet247.com/247reference/msgs/6/31683.aspx
http://docendo.bai.nu/img/kapitel/0735618917.htm
http://docs.msdnaa.net/ark_new/Webfi...rk_Library.doc
Regards,
--
Angel J. Hernández M.
MCP - MCAD - MCSD - MCDBA
http://groups.msn.com/desarrolladoresmiranda
http://www.consein.com
"Ricardo Quintanilla" <Ri****************@discussions.microsoft.com> wrote
in message news:36**********************************@microsof t.com...
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

Nov 17 '05 #2

P: n/a

"Ricardo Quintanilla" <Ri****************@discussions.microsoft.com> wrote in message
news:36**********************************@microsof t.com...
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
Nov 17 '05 #3

P: n/a
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

Nov 17 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.