Re: BeginReceive return zero length buffer when run ,and work correctly when use step by step debug mode
- From: "Daniel" <DanielV@xxxxxxxxxxxxxxxx>
- Date: Fri, 15 Sep 2006 12:32:15 +0100
Bit confused by what you are saying is the problem but can you calrify.
If you send an object to the other client, it is not always getting there.
But if you debug, it does? Is the problem that you are 'losing' bytes?
"semedao" <semedao@xxxxxxxxxxxxxxxx> wrote in message
news:eiYtLmL2GHA.1588@xxxxxxxxxxxxxxxxxxxxxxx
Hi , I am using asyc sockets p2p connection between 2 clients.
when I debug step by step the both sides , i'ts work ok.
when I run it , in somepoint (same location in the code)
when I want to receive 5 bytes buffer , I call the BeginReceive and then
wait on AsyncWaitHandle.WaitOne()
but it is signald imidiatly , and the next call to EndReceive return zero
bytes length , also the buffer is empty.
here is the code:
public static byte[] Receive(Socket OpenSocket, int length)
{
byte[] buffer = new byte[length];
int totalreceive = 0;
int _lastReceive = 0;
if (OpenSocket.Blocking)
totalreceive = OpenSocket.Receive(buffer);
else
{
IAsyncResult ar;
do
{
//in this point the OpenSocket.Available == 0 !
ar = OpenSocket.BeginReceive(buffer, 0, length, SocketFlags.None, null,
null);
ar.AsyncWaitHandle.WaitOne(); // something "signald it before data
received?
_lastReceive = OpenSocket.EndReceive(ar);
if (_lastReceive == 0) // happen when run the code , doeas not happen if
step by step slowly!
{
buffer = null;
throw new Exception("No data receive");
}
totalreceive += _lastReceive;
} while (totalreceive < length);
}
return buffer;
}
what is my problem ? I don't want to use sync calls because the rest of
the session is async also , so I only want to makethe async - sinc like
forthis part ofthe code , also , when in the debug mode I change the
OpenSocket.Blocking to True - cause it to call sync receive instead of
beginrecv , also return with zero length buffer
any ideas ?
.
- Follow-Ups:
- References:
- Prev by Date: Re: day-difference between 2 datetimes
- Next by Date: [C# 2.0] CustomControl and DrawString (GDI+)
- Previous by thread: BeginReceive return zero length buffer when run ,and work correctly when use step by step debug mode
- Next by thread: Re: BeginReceive return zero length buffer when run ,and work correctly when use step by step debug mode
- Index(es):
Relevant Pages
|
Loading