472,798 Members | 1,145 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,798 software developers and data experts.

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 28447
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
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
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
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
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
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
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
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
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
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...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 2 August 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: erikbower65 | last post by:
Using CodiumAI's pr-agent is simple and powerful. Follow these steps: 1. Install CodiumAI CLI: Ensure Node.js is installed, then run 'npm install -g codiumai' in the terminal. 2. Connect to...
0
by: erikbower65 | last post by:
Here's a concise step-by-step guide for manually installing IntelliJ IDEA: 1. Download: Visit the official JetBrains website and download the IntelliJ IDEA Community or Ultimate edition based on...
0
by: kcodez | last post by:
As a H5 game development enthusiast, I recently wrote a very interesting little game - Toy Claw ((http://claw.kjeek.com/))。Here I will summarize and share the development experience here, and hope it...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Sept 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
14
DJRhino1175
by: DJRhino1175 | last post by:
When I run this code I get an error, its Run-time error# 424 Object required...This is my first attempt at doing something like this. I test the entire code and it worked until I added this - If...
5
by: DJRhino | last post by:
Private Sub CboDrawingID_BeforeUpdate(Cancel As Integer) If = 310029923 Or 310030138 Or 310030152 Or 310030346 Or 310030348 Or _ 310030356 Or 310030359 Or 310030362 Or...
0
by: lllomh | last post by:
How does React native implement an English player?
0
by: Mushico | last post by:
How to calculate date of retirement from date of birth

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.