<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Schotime.net &#187; Sockets</title>
	<atom:link href="http://schotime.net/blog/index.php/tag/sockets/feed/" rel="self" type="application/rss+xml" />
	<link>http://schotime.net/blog</link>
	<description>All Things .Net and Me</description>
	<lastBuildDate>Thu, 01 Jul 2010 14:42:28 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.1</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Slow TcpClient Connection (sockets)</title>
		<link>http://schotime.net/blog/index.php/2008/05/27/slow-tcpclient-connection-sockets/</link>
		<comments>http://schotime.net/blog/index.php/2008/05/27/slow-tcpclient-connection-sockets/#comments</comments>
		<pubDate>Tue, 27 May 2008 03:25:48 +0000</pubDate>
		<dc:creator>Schotime</dc:creator>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[Sockets]]></category>

		<guid isPermaLink="false">http://schotime.net/index.php/2008/05/27/slow-tcpclient-connection-sockets/</guid>
		<description><![CDATA[Recently I have been experimenting with Sockets and trying to communicate with a windows service both with a console app and a website.
After managing to get some lines of communication going between the client and server applications, I couldn&#8217;t help but notice that it was taking a little bit longer than it probably should have. [...]]]></description>
			<content:encoded><![CDATA[<p>Recently I have been experimenting with Sockets and trying to communicate with a windows service both with a console app and a website.</p>
<p>After managing to get some lines of communication going between the client and server applications, I couldn&#8217;t help but notice that it was taking a little bit longer than it probably should have. I decided to have a closer look. </p>
<p>Using some basic timestamping in the console/website application the whole process was taking just over 1 second. This was to connect, send and then receive a response. Further investigation revealed only 1 statement was causing the time to blow out. Below is the statement.</p>
<table cellspacing="0" cellpadding="2" width="400" border="1">
<tbody>
<tr>
<td valign="top" width="400">
<pre class="code"><span style="color: #2b91af">TcpClient </span>socketForServer = <span style="color: blue">new </span><span style="color: #2b91af">TcpClient</span>(<span style="color: #a31515">"localhost"</span>, 200);</pre>
</td>
</tr>
</tbody>
</table>
<p>Now in all the demos and tutorials this is how it showed to connect to the server. Pretty straightforward and simple. Or so I thought. Whilst having a look through the methods associated with the TcpClient, there was one that stood out: Connect(). Hmmm&#8230;this also took a host name and a port. I decided to give it a try.</p>
<table cellspacing="0" cellpadding="2" width="400" border="1">
<tbody>
<tr>
<td valign="top" width="400">
<pre class="code"><span style="color: #2b91af">TcpClient </span>socketForServer = <span style="color: blue">new </span><span style="color: #2b91af">TcpClient</span>();
socketForServer.Connect(<span style="color: #a31515">"localhost"</span>, 200);</pre>
</td>
</tr>
</tbody>
</table>
<p>I bet you&#8217;re wondering right now. What is the difference?? At this point in time I&#8217;m not quite sure but I&#8217;m determined to figure it out because after making that change the time to execute the previous two statements was about 1 millisecond or so. Much better.</p>
<p>One thing that did seem odd was if the TcpClient failed to make a connection, the response time was almost exactly the same (~1sec). </p>
<p>If anyone knows the reason, please leave a comment. Until then, two lines will have to do.</p>
]]></content:encoded>
			<wfw:commentRss>http://schotime.net/blog/index.php/2008/05/27/slow-tcpclient-connection-sockets/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
	</channel>
</rss>
