Asynchronous socket & recursive receiving 
Author Message
 Asynchronous socket & recursive receiving

Hi, all,

I have seen in so many web artices that passing a
delegated function into a BeginReceive socket method, and
then in the function recursively receiving data.

But my experience with this has been that it does not
work: (basically the receiving recursion hangs) Trying to
debug this, I can see that the recursive call does not
step into the recursive function --ReceiveCallBack()
except for the first time. Please help!

Please see the "////" comments ---

Thanks in advance

BILL
==============================

 private static ManualResetEvent receiveDone =
    new ManualResetEvent(false);

..... client is a connected socket,

.....

     Receive(client);
     receiveDone.WaitOne();

....
private static void Receive(Socket client) {
    try {
      StateObject state = new StateObject();
      state.workSocket = client;
      client.BeginReceive( state.buffer, 0,
StateObject.BufferSize, 0,
        new AsyncCallback(ReceiveCallback), state);
    } catch (Exception e) {
      Console.WriteLine(e.ToString());
    }
  }

  private static void ReceiveCallback( IAsyncResult ar ) {
    try {
      StateObject state = (StateObject) ar.AsyncState;
      Socket client = state.workSocket;
      int bytesRead = client.EndReceive(ar);

      if (bytesRead > 0) {
      state.sb.Append(Encoding.ASCII.GetString
(state.buffer,0,bytesRead));

////===============================================
        client.BeginReceive
(state.buffer,0,StateObject.BufferSize,0,
          new AsyncCallback(ReceiveCallback), state);
//// my program hangs here when trying to get the rest of
data, what could be wrong?

      } else {
        if (state.sb.Length > 1) {
          response = state.sb.ToString();
        }
        receiveDone.Set();
      }
    } catch (Exception e) {
      Console.WriteLine(e.ToString());
    }
  }

.



Mon, 30 May 2005 23:07:16 GMT  
 Asynchronous socket & recursive receiving

Quote:

> Hi, all,

> I have seen in so many web artices that passing a
> delegated function into a BeginReceive socket method, and
> then in the function recursively receiving data.

> But my experience with this has been that it does not
> work: (basically the receiving recursion hangs) Trying to
> debug this, I can see that the recursive call does not
> step into the recursive function --ReceiveCallBack()
> except for the first time. Please help!

Bill -

    When you call the WaitOne() method on the main thread, you cause
the BeginReceive() method to block the thread while it waits for data.
My guess is that when your program hangs, the BeginReceive() method is
waiting for more data to arrive from the other end of the connection,
blocking the main thread.

    Are you sure that not all of the data is received in the first
EndReceive() call? Remember that TCP is not a one-for-one protocol,
you can perform two Send() methods on one end and get all the data in
one Receive() method on the other. You should drop out of your loop
when all of the data is received (or not block on the BeginReceive()
method while waiting for data). Hope this helps get you going in the
right direction.

Rich Blum
Author of "C# Network Programming" (Sybex)
http://www.sybex.com/sybexbooks.nsf/Booklist/4176



Tue, 31 May 2005 04:26:09 GMT  
 Asynchronous socket & recursive receiving
Thanks a lot, Rich!

BILL

Quote:
>-----Original Message-----



Quote:
>> Hi, all,

>> I have seen in so many web artices that passing a
>> delegated function into a BeginReceive socket method,
and
>> then in the function recursively receiving data.

>> But my experience with this has been that it does not
>> work: (basically the receiving recursion hangs) Trying
to
>> debug this, I can see that the recursive call does not
>> step into the recursive function --ReceiveCallBack()
>> except for the first time. Please help!

>Bill -

>    When you call the WaitOne() method on the main
thread, you cause
>the BeginReceive() method to block the thread while it
waits for data.
>My guess is that when your program hangs, the BeginReceive
() method is
>waiting for more data to arrive from the other end of the
connection,
>blocking the main thread.

>    Are you sure that not all of the data is received in
the first
>EndReceive() call? Remember that TCP is not a one-for-one
protocol,
>you can perform two Send() methods on one end and get all
the data in
>one Receive() method on the other. You should drop out of
your loop
>when all of the data is received (or not block on the
BeginReceive()
>method while waiting for data). Hope this helps get you
going in the
>right direction.

>Rich Blum
>Author of "C# Network Programming" (Sybex)
>http://www.sybex.com/sybexbooks.nsf/Booklist/4176
>.



Tue, 31 May 2005 12:15:50 GMT  
 Asynchronous socket & recursive receiving
Thanks a ton, Rich.
Look forward to your book :)

BILL

Quote:
>-----Original Message-----



Quote:
>> Hi, all,

>> I have seen in so many web artices that passing a
>> delegated function into a BeginReceive socket method,
and
>> then in the function recursively receiving data.

>> But my experience with this has been that it does not
>> work: (basically the receiving recursion hangs) Trying
to
>> debug this, I can see that the recursive call does not
>> step into the recursive function --ReceiveCallBack()
>> except for the first time. Please help!

>Bill -

>    When you call the WaitOne() method on the main
thread, you cause
>the BeginReceive() method to block the thread while it
waits for data.
>My guess is that when your program hangs, the BeginReceive
() method is
>waiting for more data to arrive from the other end of the
connection,
>blocking the main thread.

>    Are you sure that not all of the data is received in
the first
>EndReceive() call? Remember that TCP is not a one-for-one
protocol,
>you can perform two Send() methods on one end and get all
the data in
>one Receive() method on the other. You should drop out of
your loop
>when all of the data is received (or not block on the
BeginReceive()
>method while waiting for data). Hope this helps get you
going in the
>right direction.

>Rich Blum
>Author of "C# Network Programming" (Sybex)
>http://www.sybex.com/sybexbooks.nsf/Booklist/4176
>.



Tue, 31 May 2005 12:20:45 GMT  
 
 [ 4 post ] 

 Relevant Pages 

1. Asynchronous Socket Data Receiving Problem

2. Recursive socket listener causes multiple LISTENING sockets

3. socket.Send and socket.Receive at the same time

4. Socket.Send Socket.Receive

5. asynchronous receive msg from queue

6. How to reset a pending asynchronous socket

7. Asynchronous sockets performance Issues

8. Multi-thread, thread pool, or asynchronous socket

9. Help: Multi-thread, thread pool, or asynchronous socket

10. Threading the Asynchronous Server Socket Sample?

11. How to set timeout for asynchronous socket call?

12. asynchronous socket buffer clearing

 

 
Powered by phpBB® Forum Software