473,573 Members | 2,800 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.Soc kets.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.Soc kets.Socket) object and it resulted faster.
i mean System.Net.Sock ets.Socket is faster than System.Net.Sock ets.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 28630
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************ ****@discussion s.microsoft.com > wrote
in message news:36******** *************** ***********@mic rosoft.com...
i had a problem whom i do not know how to explain.

i was using a TcpClient (System.Net.Soc kets.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.Soc kets.Socket) object and it resulted faster.
i mean System.Net.Sock ets.Socket is faster than
System.Net.Sock ets.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************ ****@discussion s.microsoft.com > wrote in message
news:36******** *************** ***********@mic rosoft.com...
i had a problem whom i do not know how to explain.

i was using a TcpClient (System.Net.Soc kets.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(strin g hostname, int port);
I used
public TcpClient(IPEnd Point 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("host name", 2021);
while(iter++ < maxCount)
{
client.GetStrea m().Write(bytes Sent, 0, bytesSent.Lengt h);
Console.WriteLi ne("Sending data...");
do
{
bytes = client.GetStrea m().Read(bytesR eceived, 0, 256);
response += Encoding.ASCII. GetString(bytes Received, 0, bytes);
}
while(client.Ge tStream().DataA vailable);
Console.WriteLi ne(".. Data Received");
}

</CODE>

And following similar loop for Socket based communication

<CODE>

Socket s = new Socket(AddressF amily.InterNetw ork, SocketType.Stre am,
ProtocolType.Tc p);
s.Connect(remot eEndPoint);
while(iter++ < maxCount)
{
// Send request to the server.
Console.WriteLi ne("Sending Data...");
s.Send(bytesSen t);
int bytes = 0;
do
{
bytes = s.Receive(bytes Received);
strReceived += Encoding.ASCII. GetString(bytes Received, 0, bytes);
}
while (s.Available > 0);
Console.WriteLi ne("...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.Soc kets.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.Soc kets.Socket) object and it resulted faster.
i mean System.Net.Sock ets.Socket is faster than System.Net.Sock ets.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
25099
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 through proxy. eg, _socket = new TcpClient("http://msdn.microsoft.com", port); does not work. Thanx. Ab.
1
3198
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 using the socket class but I really need to know how to get this information using the TcpClient class. I tried writing a class from TcpCLient but then...
0
1688
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 sending data. It is sending a web page but it doesn't use the Content-Length header to indicate the size. While I can use the DataAvailable...
4
6307
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
2843
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 the TcpClient and when to use the Socket? Regards
11
8585
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 mechanisms. I use a non-blocking approach for this, using the call to 'poll' to support the async mechanism - rather than the 'begin' and 'end'...
1
1403
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 stream connection tcpClnt.EndConnect(Stm)>>> Error !!
4
7575
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 client to use is "XOVER articlenumber-" This return string after string of all the news articles from article number on.... Another newsclient, i...
1
3358
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 in the debug output that the client connects to the server, the events get fired and the disconnect event gets fired as well. I get confirmation...
0
7781
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
8202
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7788
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
8066
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
5292
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3733
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3733
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1304
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1041
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.