<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	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/"
		>
<channel>
	<title>Comments on: Sending objects via high speed asynchronous sockets in C# (Serialization + Socket programming)</title>
	<atom:link href="http://roy-t.nl/index.php/2009/05/31/sending-objects-via-high-speed-asynchronous-sockets-in-c-serialization-socket-programming/feed/" rel="self" type="application/rss+xml" />
	<link>http://roy-t.nl/index.php/2009/05/31/sending-objects-via-high-speed-asynchronous-sockets-in-c-serialization-socket-programming/</link>
	<description>My programming world</description>
	<lastBuildDate>Fri, 03 Feb 2012 10:12:09 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
	<item>
		<title>By: Ciwan</title>
		<link>http://roy-t.nl/index.php/2009/05/31/sending-objects-via-high-speed-asynchronous-sockets-in-c-serialization-socket-programming/comment-page-1/#comment-2219</link>
		<dc:creator>Ciwan</dc:creator>
		<pubDate>Sat, 12 Feb 2011 16:38:04 +0000</pubDate>
		<guid isPermaLink="false">http://royalexander.wordpress.com/?p=165#comment-2219</guid>
		<description>Hello Roy

You were right, you can probably tell I am a n00b to this.

Here is what my code looks like right now:

&lt;a href=&quot;http://pastebin.com/aJAW7iYe&quot; title=&quot;Product C# Class&quot; rel=&quot;nofollow&quot;&gt;Product Class&lt;/a&gt;

&lt;a href=&quot;http://pastebin.com/Sx7JvU01&quot; title=&quot;Server C# Class&quot; rel=&quot;nofollow&quot;&gt;Server Class&lt;/a&gt;

I haven&#039;t touched the client class, because what I understood from you, is that it was fine ! :)

As you can see in my Product class I have added a Byte Array of size 1024 &lt;&lt; would that be enough to hold the byte data from a Product object ?

Also can you please tell me (in code) how I would grab the byte data, and transform it back to a Product object.

I greatly appreciate your help.

Thanks Roy.</description>
		<content:encoded><![CDATA[<p>Hello Roy</p>
<p>You were right, you can probably tell I am a n00b to this.</p>
<p>Here is what my code looks like right now:</p>
<p><a href="http://pastebin.com/aJAW7iYe" title="Product C# Class" rel="nofollow">Product Class</a></p>
<p><a href="http://pastebin.com/Sx7JvU01" title="Server C# Class" rel="nofollow">Server Class</a></p>
<p>I haven&#8217;t touched the client class, because what I understood from you, is that it was fine ! <img src='http://roy-t.nl/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p>As you can see in my Product class I have added a Byte Array of size 1024 &lt;&lt; would that be enough to hold the byte data from a Product object ?</p>
<p>Also can you please tell me (in code) how I would grab the byte data, and transform it back to a Product object.</p>
<p>I greatly appreciate your help.</p>
<p>Thanks Roy.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Roy Triesscheijn</title>
		<link>http://roy-t.nl/index.php/2009/05/31/sending-objects-via-high-speed-asynchronous-sockets-in-c-serialization-socket-programming/comment-page-1/#comment-2187</link>
		<dc:creator>Roy Triesscheijn</dc:creator>
		<pubDate>Mon, 07 Feb 2011 08:40:45 +0000</pubDate>
		<guid isPermaLink="false">http://royalexander.wordpress.com/?p=165#comment-2187</guid>
		<description>Hey Ciwan, your server is only listening for 100ms, you need to add a while(true) loop so that your server keeps listening. Also you&#039;re not doing anything with the data you might receive. After you&#039;ve accepted a connection (this will give you a client object I think) you need to start receiving data, this will probably have you end up with a byte[]. Then you have to convert this byte[] back to a product item.</description>
		<content:encoded><![CDATA[<p>Hey Ciwan, your server is only listening for 100ms, you need to add a while(true) loop so that your server keeps listening. Also you&#8217;re not doing anything with the data you might receive. After you&#8217;ve accepted a connection (this will give you a client object I think) you need to start receiving data, this will probably have you end up with a byte[]. Then you have to convert this byte[] back to a product item.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Ciwan</title>
		<link>http://roy-t.nl/index.php/2009/05/31/sending-objects-via-high-speed-asynchronous-sockets-in-c-serialization-socket-programming/comment-page-1/#comment-2186</link>
		<dc:creator>Ciwan</dc:creator>
		<pubDate>Sun, 06 Feb 2011 16:37:35 +0000</pubDate>
		<guid isPermaLink="false">http://royalexander.wordpress.com/?p=165#comment-2186</guid>
		<description>Sorry there is a mistake in my last comment.

What I mean to say was &quot; Without using Threading &quot; ... My bad :)</description>
		<content:encoded><![CDATA[<p>Sorry there is a mistake in my last comment.</p>
<p>What I mean to say was &#8221; Without using Threading &#8221; &#8230; My bad <img src='http://roy-t.nl/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Ciwan</title>
		<link>http://roy-t.nl/index.php/2009/05/31/sending-objects-via-high-speed-asynchronous-sockets-in-c-serialization-socket-programming/comment-page-1/#comment-2185</link>
		<dc:creator>Ciwan</dc:creator>
		<pubDate>Sun, 06 Feb 2011 16:34:37 +0000</pubDate>
		<guid isPermaLink="false">http://royalexander.wordpress.com/?p=165#comment-2185</guid>
		<description>Hi Roy

Thanks for the tutorial. I have tried doing the same thing, but without using serialization. I just want two programs (client and server) that will be able to send and receive a product object. Below is the code for the Product class, and the Client class &lt;&lt; I think them two are fine. However my Server class (also below) is messed up, I just don&#039;t know how to get it to work.

&lt;a href=&quot;http://pastebin.com/Cz9qsEZm&quot; title=&quot;Product Class&quot; rel=&quot;nofollow&quot;&gt;Product Class Code&lt;/a&gt;

&lt;a href=&quot;http://pastebin.com/PDyF2vWA&quot; title=&quot;Client Class&quot; rel=&quot;nofollow&quot;&gt;Client Class Code&lt;/a&gt;

&lt;a href=&quot;http://pastebin.com/jzVhjN7u&quot; title=&quot;Server Class&quot; rel=&quot;nofollow&quot;&gt;Server Class Code&lt;/a&gt;

As you can see my server class is hardly doing anything, but the problem is I dont&#039; know what to do. I know that I have to somehow de-serialize the product object, but I don&#039;t know how I&#039;d go about doing that. Looking at your code didn&#039;t help either :(

I would greatly appreciate it you could show me how this would be done.

Thank You.</description>
		<content:encoded><![CDATA[<p>Hi Roy</p>
<p>Thanks for the tutorial. I have tried doing the same thing, but without using serialization. I just want two programs (client and server) that will be able to send and receive a product object. Below is the code for the Product class, and the Client class &lt;&lt; I think them two are fine. However my Server class (also below) is messed up, I just don&#039;t know how to get it to work.</p>
<p><a href="http://pastebin.com/Cz9qsEZm" title="Product Class" rel="nofollow">Product Class Code</a></p>
<p><a href="http://pastebin.com/PDyF2vWA" title="Client Class" rel="nofollow">Client Class Code</a></p>
<p><a href="http://pastebin.com/jzVhjN7u" title="Server Class" rel="nofollow">Server Class Code</a></p>
<p>As you can see my server class is hardly doing anything, but the problem is I dont&#8217; know what to do. I know that I have to somehow de-serialize the product object, but I don&#8217;t know how I&#8217;d go about doing that. Looking at your code didn&#8217;t help either <img src='http://roy-t.nl/wp-includes/images/smilies/icon_sad.gif' alt=':(' class='wp-smiley' /> </p>
<p>I would greatly appreciate it you could show me how this would be done.</p>
<p>Thank You.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Roy Triesscheijn</title>
		<link>http://roy-t.nl/index.php/2009/05/31/sending-objects-via-high-speed-asynchronous-sockets-in-c-serialization-socket-programming/comment-page-1/#comment-706</link>
		<dc:creator>Roy Triesscheijn</dc:creator>
		<pubDate>Tue, 05 Oct 2010 06:11:24 +0000</pubDate>
		<guid isPermaLink="false">http://royalexander.wordpress.com/?p=165#comment-706</guid>
		<description>Hey Tobias, thanks for the great comments. And sorry guys that I wasn&#039;t able to create a bigger sample. This article was supposed to be just a quick&#039;n&#039;dirty way to get everybody started :).</description>
		<content:encoded><![CDATA[<p>Hey Tobias, thanks for the great comments. And sorry guys that I wasn&#8217;t able to create a bigger sample. This article was supposed to be just a quick&#8217;n'dirty way to get everybody started <img src='http://roy-t.nl/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> .</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Tobias</title>
		<link>http://roy-t.nl/index.php/2009/05/31/sending-objects-via-high-speed-asynchronous-sockets-in-c-serialization-socket-programming/comment-page-1/#comment-705</link>
		<dc:creator>Tobias</dc:creator>
		<pubDate>Mon, 04 Oct 2010 16:43:08 +0000</pubDate>
		<guid isPermaLink="false">http://royalexander.wordpress.com/?p=165#comment-705</guid>
		<description>In solution 2 it has to be

//in the while loop where the server listens set
firstPackage = true;</description>
		<content:encoded><![CDATA[<p>In solution 2 it has to be</p>
<p>//in the while loop where the server listens set<br />
firstPackage = true;</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Tobias</title>
		<link>http://roy-t.nl/index.php/2009/05/31/sending-objects-via-high-speed-asynchronous-sockets-in-c-serialization-socket-programming/comment-page-1/#comment-704</link>
		<dc:creator>Tobias</dc:creator>
		<pubDate>Mon, 04 Oct 2010 16:40:04 +0000</pubDate>
		<guid isPermaLink="false">http://royalexander.wordpress.com/?p=165#comment-704</guid>
		<description>Hi Iko

campe up with 2 possible solutions. 

1) (quick and dirty) try to deserialize each time and catch the exception 

readRequest is function Receive, evaluateRequest is function Done 

&lt;code&gt;
private void readRequest(IAsyncResult result)
{
    Status status = (Status)result.AsyncState;
    int read = status.socket.EndReceive(result);

    if (read &gt; 0)
    {
	for (int i = 0; i &lt; read; i++) status.transmissionBuffer.Add(status.buffer[i]);
	//try to derialize to see if data send completely
	try
	{
	    Status receivedStatus = status.deserialize();
	    evaluateRequest(status);
	}
	catch (SerializationException)
	{
	    Console.WriteLine(&quot;read again..&quot;);
	    status.socket.BeginReceive(status.buffer, 0, status.buffer.Length, SocketFlags.None, readRequest, status);
	}
   } 
   else evaluateRequest(status);
}
&lt;/code&gt;

2) Much nicer solution is to send the message length at the beginning of each message. This solution fails if the message length exceeds int.MaxValue Bytes. (There is still the long datatype)

&lt;code&gt;
//Add in Server following variables
private int incomingMessageLength = 0;
private Boolean firstPackage = true;
byte[] tmpLentgh = new byte[256];

//in the while loop where the server listens set
firstPackage = false;

//modify the Receive function like this
private void Receive(IAsyncResult result)
{
    Status status = (Status)result.AsyncState;
    int read = status.socket.EndReceive(result);

    if (read &gt; 0)
    {
	if(firstPackage){
	    tmpLentgh = new byte[256];
	}

	if (firstPackage)
	{   //first 256 bytes are message length
	    for (int i = 0; i &lt; read; i++)
	    {
		if (i &lt; 256)
		{
		    tmpLentgh[i] = status.buffer[i];
		}
		else status.transmissionBuffer.Add(status.buffer[i]);
	    }
	    BinaryFormatter bin = new BinaryFormatter();
	    MemoryStream mem = new MemoryStream();

	    mem.Write(tmpLentgh, 0, tmpLentgh.Length);
	    mem.Seek(0, 0);
	    incomingMessageLength = (int)bin.Deserialize(mem);
	    firstPackage = false;
	}
	else if (!firstPackage)
	{
	    for (int i = 0; i  status.transmissionBuffer.Count)
	{   //read again
	    Console.WriteLine(&quot;read again&quot;);
	    status.socket.BeginReceive(status.buffer, 0, status.buffer.Length, SocketFlags.None, Receive, status);
	}
	else Done(status);
   } 
   else Done(status);
}

//in the connect function of the client change
byte[] buffer = status.Serialize(); //fills the buffer with data
status.Socket.BeginSend(buffer, 0, buffer.Length, SocketFlags.None, Send, status);

//to:
byte[] buffer = status.serialize();
                
outgoingMessageLength = buffer.Length;
BinaryFormatter bin = new BinaryFormatter();
MemoryStream mem = new MemoryStream();
bin.Serialize(mem, outgoingMessageLength);
byte[] messageLength = mem.GetBuffer();
List messageByteList = messageLength.ToList();
for (int i = 0; i &lt; buffer.Length; i++) messageByteList.Add(buffer[i]);
buffer = messageByteList.ToArray();

status.socket.BeginSend(buffer, 0, buffer.Length, SocketFlags.None, send, status);
&lt;/code&gt;

Thank you Roy for this nice tutorial. Pointed me in the right direction :)</description>
		<content:encoded><![CDATA[<p>Hi Iko</p>
<p>campe up with 2 possible solutions. </p>
<p>1) (quick and dirty) try to deserialize each time and catch the exception </p>
<p>readRequest is function Receive, evaluateRequest is function Done </p>
<p><code><br />
private void readRequest(IAsyncResult result)<br />
{<br />
    Status status = (Status)result.AsyncState;<br />
    int read = status.socket.EndReceive(result);</p>
<p>    if (read &gt; 0)<br />
    {<br />
	for (int i = 0; i &lt; read; i++) status.transmissionBuffer.Add(status.buffer[i]);<br />
	//try to derialize to see if data send completely<br />
	try<br />
	{<br />
	    Status receivedStatus = status.deserialize();<br />
	    evaluateRequest(status);<br />
	}<br />
	catch (SerializationException)<br />
	{<br />
	    Console.WriteLine(&quot;read again..&quot;);<br />
	    status.socket.BeginReceive(status.buffer, 0, status.buffer.Length, SocketFlags.None, readRequest, status);<br />
	}<br />
   }<br />
   else evaluateRequest(status);<br />
}<br />
</code></p>
<p>2) Much nicer solution is to send the message length at the beginning of each message. This solution fails if the message length exceeds int.MaxValue Bytes. (There is still the long datatype)</p>
<p><code><br />
//Add in Server following variables<br />
private int incomingMessageLength = 0;<br />
private Boolean firstPackage = true;<br />
byte[] tmpLentgh = new byte[256];</p>
<p>//in the while loop where the server listens set<br />
firstPackage = false;</p>
<p>//modify the Receive function like this<br />
private void Receive(IAsyncResult result)<br />
{<br />
    Status status = (Status)result.AsyncState;<br />
    int read = status.socket.EndReceive(result);</p>
<p>    if (read &gt; 0)<br />
    {<br />
	if(firstPackage){<br />
	    tmpLentgh = new byte[256];<br />
	}</p>
<p>	if (firstPackage)<br />
	{   //first 256 bytes are message length<br />
	    for (int i = 0; i &lt; read; i++)<br />
	    {<br />
		if (i &lt; 256)<br />
		{<br />
		    tmpLentgh[i] = status.buffer[i];<br />
		}<br />
		else status.transmissionBuffer.Add(status.buffer[i]);<br />
	    }<br />
	    BinaryFormatter bin = new BinaryFormatter();<br />
	    MemoryStream mem = new MemoryStream();</p>
<p>	    mem.Write(tmpLentgh, 0, tmpLentgh.Length);<br />
	    mem.Seek(0, 0);<br />
	    incomingMessageLength = (int)bin.Deserialize(mem);<br />
	    firstPackage = false;<br />
	}<br />
	else if (!firstPackage)<br />
	{<br />
	    for (int i = 0; i  status.transmissionBuffer.Count)<br />
	{   //read again<br />
	    Console.WriteLine("read again");<br />
	    status.socket.BeginReceive(status.buffer, 0, status.buffer.Length, SocketFlags.None, Receive, status);<br />
	}<br />
	else Done(status);<br />
   }<br />
   else Done(status);<br />
}</p>
<p>//in the connect function of the client change<br />
byte[] buffer = status.Serialize(); //fills the buffer with data<br />
status.Socket.BeginSend(buffer, 0, buffer.Length, SocketFlags.None, Send, status);</p>
<p>//to:<br />
byte[] buffer = status.serialize();</p>
<p>outgoingMessageLength = buffer.Length;<br />
BinaryFormatter bin = new BinaryFormatter();<br />
MemoryStream mem = new MemoryStream();<br />
bin.Serialize(mem, outgoingMessageLength);<br />
byte[] messageLength = mem.GetBuffer();<br />
List messageByteList = messageLength.ToList();<br />
for (int i = 0; i &lt; buffer.Length; i++) messageByteList.Add(buffer[i]);<br />
buffer = messageByteList.ToArray();</p>
<p>status.socket.BeginSend(buffer, 0, buffer.Length, SocketFlags.None, send, status);<br />
</code></p>
<p>Thank you Roy for this nice tutorial. Pointed me in the right direction <img src='http://roy-t.nl/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Tobias</title>
		<link>http://roy-t.nl/index.php/2009/05/31/sending-objects-via-high-speed-asynchronous-sockets-in-c-serialization-socket-programming/comment-page-1/#comment-703</link>
		<dc:creator>Tobias</dc:creator>
		<pubDate>Mon, 04 Oct 2010 14:26:52 +0000</pubDate>
		<guid isPermaLink="false">http://royalexander.wordpress.com/?p=165#comment-703</guid>
		<description>Hi Iko

have the same problem. But didn&#039;t have a solution right now. But still trying ...</description>
		<content:encoded><![CDATA[<p>Hi Iko</p>
<p>have the same problem. But didn&#8217;t have a solution right now. But still trying &#8230;</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Iko</title>
		<link>http://roy-t.nl/index.php/2009/05/31/sending-objects-via-high-speed-asynchronous-sockets-in-c-serialization-socket-programming/comment-page-1/#comment-639</link>
		<dc:creator>Iko</dc:creator>
		<pubDate>Thu, 09 Sep 2010 14:41:41 +0000</pubDate>
		<guid isPermaLink="false">http://royalexander.wordpress.com/?p=165#comment-639</guid>
		<description>Hi
I dont know why, but  this program doest work when you try to do this :   
example ..
 when the buffer in status is 256 bytes and you send message that has exactly 256 bytes.
or in other words  when the size of your message is   divisible   by the  size of the buffer .

in server after   (read == status.buffer.Length)  when it tries  BeginReceive   it just  freeze

can somebody try it and tell me ?

thx..</description>
		<content:encoded><![CDATA[<p>Hi<br />
I dont know why, but  this program doest work when you try to do this :<br />
example ..<br />
 when the buffer in status is 256 bytes and you send message that has exactly 256 bytes.<br />
or in other words  when the size of your message is   divisible   by the  size of the buffer .</p>
<p>in server after   (read == status.buffer.Length)  when it tries  BeginReceive   it just  freeze</p>
<p>can somebody try it and tell me ?</p>
<p>thx..</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Roy Triesscheijn</title>
		<link>http://roy-t.nl/index.php/2009/05/31/sending-objects-via-high-speed-asynchronous-sockets-in-c-serialization-socket-programming/comment-page-1/#comment-581</link>
		<dc:creator>Roy Triesscheijn</dc:creator>
		<pubDate>Sat, 24 Jul 2010 20:38:18 +0000</pubDate>
		<guid isPermaLink="false">http://royalexander.wordpress.com/?p=165#comment-581</guid>
		<description>Hey BigDirty, 

Thanks for your comment, I actually tried large texts and it seemed fine (there&#039;s a special check in the code to see if we could expect another fragment). Are you sure my code is faulty? Could be that I missed something. Unfortunately I don&#039;t have the sample project anymore, but please play around a bit and if you&#039;ve isolated the problem let me know :).</description>
		<content:encoded><![CDATA[<p>Hey BigDirty, </p>
<p>Thanks for your comment, I actually tried large texts and it seemed fine (there&#8217;s a special check in the code to see if we could expect another fragment). Are you sure my code is faulty? Could be that I missed something. Unfortunately I don&#8217;t have the sample project anymore, but please play around a bit and if you&#8217;ve isolated the problem let me know <img src='http://roy-t.nl/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> .</p>
]]></content:encoded>
	</item>
</channel>
</rss>

