日期:2014-05-19  浏览次数:20754 次

对Socket beginreceive的异步接收回调函数有点疑问
请问下,关于那个socket的异步接收回调,为什么当bytesRead   <   StateObject.BufferSize的时候,只运行一次?
分值不多,还望高手慷慨赐教,谢!


private   static   void   ReceiveCallback(   IAsyncResult   ar   )   {
                try   {
                        //   Retrieve   the   state   object   and   the   client   socket  
                        //   from   the   asynchronous   state   object.
                        StateObject   state   =   (StateObject)   ar.AsyncState;
                        Socket   client   =   state.workSocket;

                        //   Read   data   from   the   remote   device.
                        int   bytesRead   =   client.EndReceive(ar);

                        if   (bytesRead   >   0)   {
                                //   There   might   be   more   data,   so   store   the   data   received   so   far.
                        state.sb.Append(Encoding.ASCII.GetString(state.buffer,0,bytesRead));

                                //   Get   the   rest   of   the   data.     bytesRead   <   StateObject.BufferSize的时候这句就没运行过,然后就无法继续了,因为receiveDone.Set();无法执行
                                client.BeginReceive(state.buffer,0,StateObject.BufferSize,0,
                                        new   AsyncCallback(ReceiveCallback),   state);
                        }  
                        else   {
                                //   All   the   data   has   arrived;   put   it   in   response.
                                if   (state.sb.Length   >   1)   {
                                        response   =   state.sb.ToString();
                                }
                                //   Signal   tha