<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://blogs.msdn.com/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>Simple Message Framing Sample for TCP Socket</title><link>http://blogs.msdn.com/b/joncole/archive/2006/03/20/simple-message-framing-sample-for-tcp-socket.aspx</link><description>A common misunderstanding for developers new to network programming over TCP sockets is how messages are sent and received. I frequently hear the statement that "my data is not arriving on the other side of the socket in the same format that I sent it</description><dc:language>en-US</dc:language><generator>Telligent Evolution Platform Developer Build (Build: 5.6.50428.7875)</generator><item><title>[收藏]General things to take into consideration while doing socket programming.</title><link>http://blogs.msdn.com/b/joncole/archive/2006/03/20/simple-message-framing-sample-for-tcp-socket.aspx#8625418</link><pubDate>Fri, 20 Jun 2008 17:40:39 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8625418</guid><dc:creator>verygis</dc:creator><description>&lt;p&gt;Ihadtorecentlywriteaclient-serversocketapplicationandsoIwasmullingoverallthatneeds...&lt;/p&gt;
&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=8625418" width="1" height="1"&gt;</description></item><item><title>re: Simple Message Framing Sample for TCP Socket</title><link>http://blogs.msdn.com/b/joncole/archive/2006/03/20/simple-message-framing-sample-for-tcp-socket.aspx#903827</link><pubDate>Mon, 30 Oct 2006 19:56:32 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:903827</guid><dc:creator>JonCole</dc:creator><description>&lt;P&gt;Sean,&lt;/P&gt;
&lt;P&gt;Yes, you are correct that I needed to put that line after the Socket.Receive inside the while loop. &amp;nbsp;Also, I&amp;nbsp;noticed I was using the wrong offset into the data buffer for that first read of the message body. &amp;nbsp;I have fixed both problems in the sample code above. &amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thanks for taking the time to point this out,&lt;BR&gt;Jon&lt;/P&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=903827" width="1" height="1"&gt;</description></item><item><title>re: Simple Message Framing Sample for TCP Socket</title><link>http://blogs.msdn.com/b/joncole/archive/2006/03/20/simple-message-framing-sample-for-tcp-socket.aspx#898480</link><pubDate>Mon, 30 Oct 2006 00:49:07 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:898480</guid><dc:creator>Sean Beebe</dc:creator><description>&lt;p&gt;Hi Jon,&lt;/p&gt;
&lt;p&gt;Thanks for the example. It was very helpful for me. &lt;/p&gt;
&lt;p&gt;Also, I think I may have discovered a bug. In the second while loop of ReadMessage shouldn't totalread += currentread come after the socket.Receive.&lt;/p&gt;
&lt;p&gt;Thanks,&lt;/p&gt;
&lt;p&gt;Sean&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=898480" width="1" height="1"&gt;</description></item><item><title>Simple Message Framing Sample for TCP Socket - Part 2 (Asynchronous)</title><link>http://blogs.msdn.com/b/joncole/archive/2006/03/20/simple-message-framing-sample-for-tcp-socket.aspx#583511</link><pubDate>Tue, 25 Apr 2006 23:18:47 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:583511</guid><dc:creator>Jon's WebLog</dc:creator><description>As a follow up to my last post (&lt;a rel="nofollow" target="_new" href="http://blogs.msdn.com/joncole/archive/2006/03/20/555721.aspx"&gt;http://blogs.msdn.com/joncole/archive/2006/03/20/555721.aspx&lt;/a&gt;) I decided...&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=583511" width="1" height="1"&gt;</description></item><item><title>re: Simple Message Framing Sample for TCP Socket</title><link>http://blogs.msdn.com/b/joncole/archive/2006/03/20/simple-message-framing-sample-for-tcp-socket.aspx#580311</link><pubDate>Fri, 21 Apr 2006 06:02:18 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:580311</guid><dc:creator>valkyrie</dc:creator><description>Thank you. &amp;nbsp;That was the most suscint framing of this particular conversation I have ever seen.&lt;br&gt;&lt;br&gt;Thank you again.&lt;br&gt;&lt;br&gt;Regards.&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=580311" width="1" height="1"&gt;</description></item><item><title>re: Simple Message Framing Sample for TCP Socket</title><link>http://blogs.msdn.com/b/joncole/archive/2006/03/20/simple-message-framing-sample-for-tcp-socket.aspx#573344</link><pubDate>Tue, 11 Apr 2006 16:57:59 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:573344</guid><dc:creator>Denis</dc:creator><description>While Alan is correct that this is one scenario is possible with Send(), Send() it makes it appear as if this is deterministic when it is in fact not. If you send two buffers (B1 and B2) the following are some of the possible scenarios:&lt;br&gt;&lt;br&gt;1. B1 is sent as one segment and B2 is sent as another over the wire.&lt;br&gt;2. B1 and B2 are coalesced into one segment that is sent over the wire&lt;br&gt;3. Part of B1 is sent in one segment, and the remaining bits are sent along with B2 in another segment.&lt;br&gt;4. B1 and part of B2 are sent in one segment followed by the rest of B2 in another segment.&lt;br&gt;&lt;br&gt;Many factors come into play. If the Nagle algorithm is disabled, the behavior will be different. If the connection is waiting for an ACK, buffers will likely be coalesced. etc...&lt;br&gt;&lt;br&gt;The important points are:&lt;br&gt;&lt;br&gt;1. As Alan stated, the Send(header), Send(payload) is probably not a good idea.&lt;br&gt;2. TCP is a stream protocol. Message boundaries are not guaranteed.&lt;br&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=573344" width="1" height="1"&gt;</description></item><item><title>re: Simple Message Framing Sample for TCP Socket</title><link>http://blogs.msdn.com/b/joncole/archive/2006/03/20/simple-message-framing-sample-for-tcp-socket.aspx#573117</link><pubDate>Tue, 11 Apr 2006 08:09:12 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:573117</guid><dc:creator>JonCole</dc:creator><description>Alan, &amp;nbsp;Good feedback. &amp;nbsp;I considered writing the code as you mention to eliminate the duplicate calls to Send(), but I purposely made the decision to leave the duplicate calls in to emphasize the fact that the message size is sent as a &amp;quot;prefix&amp;quot; to the actual message. &amp;nbsp;I will add a note to the post to encourage people to read the comments you have posted.&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=573117" width="1" height="1"&gt;</description></item><item><title>re: Simple Message Framing Sample for TCP Socket</title><link>http://blogs.msdn.com/b/joncole/archive/2006/03/20/simple-message-framing-sample-for-tcp-socket.aspx#573115</link><pubDate>Tue, 11 Apr 2006 08:00:51 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:573115</guid><dc:creator>JonCole</dc:creator><description>Ioan, &amp;nbsp;You are correct. &amp;nbsp;I definitely had a type-o in my explanation and I have fixed the mistake.&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=573115" width="1" height="1"&gt;</description></item><item><title>re: Simple Message Framing Sample for TCP Socket</title><link>http://blogs.msdn.com/b/joncole/archive/2006/03/20/simple-message-framing-sample-for-tcp-socket.aspx#572458</link><pubDate>Mon, 10 Apr 2006 16:19:57 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:572458</guid><dc:creator>Alan McFarlane</dc:creator><description>I've seen very many people misuenderstanding this and have answered many myself and agree with you diagnosis. &amp;nbsp;However your send side code provokes bad behaviour, almost guaranteeing that your readers' next question is &amp;quot;How do I turn off Nagle? &amp;nbsp;As the performance sucks...&amp;quot;&lt;br&gt;&lt;br&gt;By doing send(), send(), the stack will send the first chunk (the length), wait for TCP ACK before sending the second chunk (the data). &amp;nbsp;Thus breaking another of the cardinal TCP programmer rules, &amp;quot;Send *all* related data in one send()&amp;quot;. &amp;nbsp;See e.g. &lt;a rel="nofollow" target="_new" href="http://msdn.microsoft.com/library/en-us/dnwxp/html/comperfnetapppt1.asp?frame=true"&gt;http://msdn.microsoft.com/library/en-us/dnwxp/html/comperfnetapppt1.asp?frame=true&lt;/a&gt; &amp;nbsp;and &amp;nbsp;&lt;a rel="nofollow" target="_new" href="http://msdn.microsoft.com/library/en-us/winsock/winsock/tcp_ip_specific_issues_2.asp?frame=true"&gt;http://msdn.microsoft.com/library/en-us/winsock/winsock/tcp_ip_specific_issues_2.asp?frame=true&lt;/a&gt;&lt;br&gt;&lt;br&gt;Perhaps you can cover such subjects in subsequent postings. :-)&lt;br&gt;&lt;br&gt;Alan&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=572458" width="1" height="1"&gt;</description></item><item><title>re: Simple Message Framing Sample for TCP Socket</title><link>http://blogs.msdn.com/b/joncole/archive/2006/03/20/simple-message-framing-sample-for-tcp-socket.aspx#571591</link><pubDate>Sat, 08 Apr 2006 19:59:14 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:571591</guid><dc:creator>Ioan Herisanu</dc:creator><description>If you send &amp;quot;Hello World&amp;quot; and receive &amp;quot;he&amp;quot; + &amp;quot;llo world&amp;quot; then you have not a TCP socket. &amp;nbsp;For TCP one should have received &amp;quot;He&amp;quot; + &amp;quot;llo World&amp;quot;. Please, pay attention when working with these,as some novices will understand really bad things&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=571591" width="1" height="1"&gt;</description></item></channel></rss>