Ejemplo n.º 1
0
        private void ReceiveCallback(IAsyncResult ar)
        {
            SocketState state = null;

            try
            {
                state = (SocketState)ar.AsyncState;

                int byteCount = state.Socket.EndReceive(ar);

                if (byteCount > 0)
                {
                    state.MessageData.Seek(0, SeekOrigin.End);
                    state.MessageData.Write(state.Buffer, 0, byteCount);
                    state.MessageData.Seek(0, SeekOrigin.Begin);

                    int b     = 0;
                    int count = 0;

                    while ((b = state.MessageData.ReadByte()) > -1)
                    {
                        if (b == 0)
                        {
                            count++;
                        }
                        else
                        {
                            count = 0;
                        }

                        if (count == EndOfFileLength)
                        {
                            state.Complete();
                            return;
                        }
                    }

                    state.Socket.BeginReceive(state.Buffer, 0, state.Buffer.Length, 0, ReceiveCallback, state);
                }
            }
            catch (Exception ex)
            {
                if (state != null)
                {
                    state.Exception = ex;
                    state.Complete();
                }
            }
        }
Ejemplo n.º 2
0
        private void ConnectCallback(IAsyncResult ar)
        {
            SocketState state = null;

            try
            {
                state = (SocketState)ar.AsyncState;
                state.Socket.EndConnect(ar);
            }
            catch (Exception ex)
            {
                if (state != null)
                {
                    state.Exception = ex;
                }
            }
            finally
            {
                if (state != null)
                {
                    state.Complete();
                }
            }
        }