Esempio n. 1
0
        private static void ReceiveCallback(IAsyncResult ar)
        {
            try
            {
                CStateObject state  = (CStateObject)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, CStateObject.BufferSize, 0, new AsyncCallback(ReceiveCallback), state);
                }
                else
                {
                    if (state.sb.Length > 1)
                    {
                        response = state.sb.ToString();
                    }
                    receiveDone.Set();
                }
            }catch (Exception e)
            {
                e.ToString();
            }
        }
Esempio n. 2
0
        private static void Receive(Socket client)
        {
            try
            {
                CStateObject state = new CStateObject();
                state.workSocket = client;

                client.BeginReceive(state.buffer, 0, CStateObject.BufferSize, 0, new AsyncCallback(ReceiveCallback), state);
            }catch (Exception e)
            {
                Console.WriteLine(e.ToString());
            }
        }