Geeks With Blogs

Post Categories

Rahul Anand's Blog If my mind can conceive it, and my heart can believe it, I know I can achieve it.

A few days back I was just coding for a basic TCP/IP Client to send and
receive buffer across network.

I encountered a problem in receiving data through receive method.

I am sharing this in this blog as it may help others, who are novice is
.NET socket programming.

I searched all over net for samples and found all over (including MSDN).
Blocking mode was enough for me so I coded my receive as:



Byte[] bytesReceived = new Byte[256];
do
{
bytes = s.Receive(bytesReceived);
strReceived += Encoding.ASCII.GetString(bytesReceived, 0, bytes);
}
while (bytes > 0);



But later I noticed that if in last iteration the bytes come to be less than
256 i.e. when I have received all data the next receive call causes the
execution to block as receive has nothing to receive.

Realizing that I changed my code to:



Byte[] bytesReceived = new Byte[256];
do
{
bytes = s.Receive(bytesReceived);
strReceived += Encoding.ASCII.GetString(bytesReceived, 0, bytes);
}
while (bytes != 256);



It worked fine for all my real scenarios. But I kept thinking that it will
also cause problem if the bytes to be received is multiple of 256 chunks. In
that case it will also block execution when last time it calls receives
method.

Finally I got something better:



Byte[] bytesReceived = new Byte[256];
while (s.Available)
{
bytes = s.Receive(bytesReceived);
strReceived += Encoding.ASCII.GetString(bytesReceived, 0, bytes);
}



I thought it would be appropriate to change my do-while loop to while. But I
did a mistake there. As sometimes s.Available returns 0 even if receive is
able to receive data. And so in all such cases my loop to receive data got
bypassed.
As I analyzed this and guessed reason was the data chunk was not available
when s.Available is called but since receive is a blocking call it waits for
data arrival and finally returns with data.

So, finally I changed the code to following and it worked fine.



Byte[] bytesReceived = new Byte[256];
do
{
bytes = s.Receive(bytesReceived);
strReceived += Encoding.ASCII.GetString(bytesReceived, 0, bytes);
}
while (s.Available);

Posted on Wednesday, August 3, 2005 7:45 AM C# | Back to top


Comments on this post: Basic .NET Socket Programming

# re: Basic .NET Socket Programming
Requesting Gravatar...
your code snippet is a prime candidate for stringbuilder.append! :-)
Left by Eric Newton on Aug 04, 2005 2:59 AM

# re: Basic .NET Socket Programming
Requesting Gravatar...
hai this is sai ,
first step is configure some bytes of buffer at your side
such as
bytes byt[] = new bytes[1000];
now you keep the socket in receive mode if it takes only
200 bytes it will return the number of bytes the client has sent to server based on that you try fetch the data
in this case it is 200 so read the data(array) from 0 to 199
Left by saichand on Feb 02, 2008 5:27 AM

# re: Basic .NET Socket Programming
Requesting Gravatar...
Hi All

Have you some example with sqlconnection.

Regards,

Inder
Left by Inder on Apr 06, 2009 2:38 AM

# re: Basic .NET Socket Programming
Requesting Gravatar...
hi.. the code is ok.
even i m having a code to transfer small file betyween client and server. the code is working fine when i m running but i m unable to see that filr that i have transferred from one system to another.
pls can any one just let me know where i m going to have that transferred file on another system.
Left by nutan on Nov 05, 2009 12:40 AM

Your comment:
 (will show your gravatar)


Copyright © Rahul Anand | Powered by: GeeksWithBlogs.net