Socket Underflow or Overflow

  • Thread starter Thread starter Chan
  • Start date Start date
C

Chan

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;
}
....
 
Chan said:
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
 
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?

Etienne Boucher
 
Chan said:
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.
 
Etienne Boucher said:
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.
 
Back
Top