473,503 Members | 2,075 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Why Socket is faster than TcpClient?

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
3 28613
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

"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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
25085
by: Abubakar | last post by:
hi, I work on a computer that is part of a network and uses proxy to connect to net. I cant connect to servers outside my proxy with simple ConnectTo code. I need to know how to make my requests go...
1
3195
by: Ethan | last post by:
Hi, In quite a fix here. Hope someone can help ASAP. I need to get the IP address of the client that connects when I use TcpListener.AcceptTcpConnections. I know I can get the information...
0
1677
by: Johann Blake | last post by:
I am using the TcpClient to connect to a web site. I then open a NetworkStream and read the contents that are being sent back. The problem is that I have no idea when the remote host is finished...
4
6297
by: BadOmen | last post by:
Hi, What is the different between 'System.Net.Sockets.Socket' and 'System.Net.Sockets.TcpClient'? When do I use System.Net.Sockets.TcpClient and System.Net.Sockets.Socket?? Yours, Jonas
2
2840
by: Martin Arvidsson | last post by:
Hi! I am just beginning to learn about the TcpListener and TcpClient and Socket. I have figured out the TcpListener, it is used for booth in and outgoing requests, right? But when to use...
11
8578
by: atlaste | last post by:
Hi, In an attempt to create a full-blown webcrawler I've found myself writing a wrapper around the Socket class in an attempt to make it completely async, supporting timeouts and some scheduling...
1
1402
by: Jordi | last post by:
Hi, I have my socket class using tcpClient framework 2.0... The socket is running perfectly but I've a problem for close the socket connection with the server! I try with this: Stm is the...
4
7566
by: Andrew Jackson | last post by:
I am writing a newsgroup client. I have the protocol figured out. But I get slow transfer speeds off any of the network objects read the data from For example one of the commands for a news...
1
3333
Airslash
by: Airslash | last post by:
Hello, The problem is that my server is not receiving data. The code below are the various classes I designed around sockets. It will be big... I have run the code with the debugger, and I see...
0
7067
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7264
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7316
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
6975
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7449
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
4992
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4666
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3148
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1495
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.