473,324 Members | 2,248 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,324 software developers and data experts.

Socket Underflow or Overflow

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
3 2080

"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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: William Ryan | last post by:
As an experienced Java Programmer, trust me on this, stick with .NET if you are already using it. It's hard to tell based on what you post. Is all of your code wrapped in an exception handler? ...
7
by: Oplec | last post by:
Hello, How can underflow and overflow of the basic arithmetic types be tested for using standard C++? For instance, if two long doubles are multiplied together, test whether or not the result...
38
by: JKop | last post by:
union SignedChoice{ long with_sign; unsigned long without_sign; }; int main() { SignedChoice data;
5
by: Ian Pilcher | last post by:
I'm trying to figure out if an increment to a variable of an integer type, followed by a decrement, (or vice versa) is guaranteed to restore the variable to its initial value, even if the first...
13
by: tings | last post by:
An article states: "In floating point maths, where if you divide by a sufficiently large number sufficiently often, you will always be able to reach a value too small to distinguish from zero,...
2
by: alok | last post by:
I am getting inconsistent behvior on Linux and Solaris platfors while computing doule ( 64 bit precision ) multiplications. I have following two double numbers whose integer representation is as...
4
by: glenn | last post by:
I have a COM Server I've written in C#. I have a client app I've written in Delphi that is calling the C# COM Server. However, one of the functions in the COM Server creates a form and during the...
4
by: Tom | last post by:
I have a VB.NET framework 1.1 application that I am installing on my user's workstation. It works fine on EVERY machine except for one - on this one machine it generates a 'Overflow or underflow in...
11
by: pereges | last post by:
Hi, I have written a small function that can (I think) deal with overflow/underflow while adding integers and divide by zero problem. Can some one please tell me if I have done it correctly ? Would...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.