By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
448,563 Members | 1,280 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 448,563 IT Pros & Developers. It's quick & easy.

Socket Underflow or Overflow

P: n/a
A TCP connection to retrieve a customer's order data. While the
sender's sending the same list of order data, say 10 orders, data
picked up from receiving buffer can be different and keep changing.

Further study reveals that the extra orders are duplicated.

I'm using synchronized way, here is my code,

....
client.SetSocketOption(SocketOptionLevel.Socket,
SocketOptionName.SendTimeout, 1000);
client.SetSocketOption(SocketOptionLevel.Socket,
SocketOptionName.ReceiveTimeout, 1000);
try
{
client.Connect(remoteEP);
byte[] byteData = Encoding.ASCII.GetBytes(sbPost.ToString()); //(PostContent);
client.Send(byteData, 0, byteData.Length, SocketFlags.None);

byte[] byteRec = new byte[5000];
int any = client.Receive(byteRec, byteRec.Length, SocketFlags.None);
while (any > 0)
{
response.Append(Encoding.ASCII.GetString(byteRec)) ;
any = client.Receive(byteRec, byteRec.Length,
SocketFlags.None);
}

client.Shutdown(SocketShutdown.Both);
client.Close();
client = null;
return response.ToString();
}

catch (Exception e)
{
throw e;
}
....
Nov 16 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a

"Chan" <hi*****@yahoo.com> wrote in message
news:fe**************************@posting.google.c om...
A TCP connection to retrieve a customer's order data. While the
sender's sending the same list of order data, say 10 orders, data
picked up from receiving buffer can be different and keep changing.

Further study reveals that the extra orders are duplicated.

I'm using synchronized way, here is my code,

...
client.SetSocketOption(SocketOptionLevel.Socket,
SocketOptionName.SendTimeout, 1000);
client.SetSocketOption(SocketOptionLevel.Socket,
SocketOptionName.ReceiveTimeout, 1000);
try
{
client.Connect(remoteEP);
byte[] byteData = Encoding.ASCII.GetBytes(sbPost.ToString());
//(PostContent);
client.Send(byteData, 0, byteData.Length, SocketFlags.None);

byte[] byteRec = new byte[5000];
int any = client.Receive(byteRec, byteRec.Length, SocketFlags.None);
while (any > 0)
{
response.Append(Encoding.ASCII.GetString(byteRec)) ;
any = client.Receive(byteRec, byteRec.Length,
SocketFlags.None);
}


That read loop is not correct. There's no telling how many bytes each
socket read will yeild. You must check the return value to see how many
bytes were read. You were writing 5000 chars into response no matter how
many bytes were read.

Also, you should avoid calling Encoder.GetString(byte[] bytes) in a loop,
since it causes a memory allocation of 2x bytes.length for each call.
Instead allocate a single char[] to be the targed of the decoding.
char[] chars = new char[byteRec.Length];
int any = client.Receive(byteRec, byteRec.Length, SocketFlags.None);
while (any > 0)
{
Encoding.ASCII.GetChars(byteRec,0,any,chars,0);
response.Append(chars,0,any);
any = client.Receive(byteRec, byteRec.Length, SocketFlags.None);
}

David
Nov 16 '05 #2

P: n/a
Chan <hi*****@yahoo.com> wrote:
A TCP connection to retrieve a customer's order data. While the
sender's sending the same list of order data, say 10 orders, data
picked up from receiving buffer can be different and keep changing.

Further study reveals that the extra orders are duplicated.


Well, you've got some problems with your code anyway - you're always
assuming that the whole of byteRec will be filled. You should use the
value of "any" to find out how many bytes have actually been received.
That may well sort out your problem.

Also, your try/catch isn't actually doing anything useful - if you're
just going to rethrow the exception, you might as well not have a try
block at all. On the other hand, you should be using a finally block to
close the socket if an exception is thrown - a "using" statement makes
this a lot easier.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #3

P: n/a
Etienne Boucher <et*****@novat.qc.ca> wrote:
since it causes a memory allocation of 2x bytes.length for each call.


Sorry, I'm just trying to understand... Since System.Char is a 16bit entity
in .NET, doesn't it come back to be the same total space?


No, because the encoding used maps one byte to one char - 8 bits to 16
bits. If Encoding.Unicode were being used, that would be a different
matter.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.