Exemple #1
0
        private void Receive(Socket client)
        {
            try
            {
                // Create the state object.
                stateobject state = new stateobject();
                state.workSocket = client;

                // Begin receiving the data from the remote device.
                client.BeginReceive(state.buffer, 0, stateobject.BufferSize, 0, new AsyncCallback(ReceiveCallback), state);
            }
            catch (Exception e)
            {
                //Console.WriteLine(e.ToString());
                //MethodInvoker action = delegate { textBoxOutput.Text += e.ToString() + "\r\n"; };
                //textBoxOutput.BeginInvoke(action);
            }
        }
Exemple #2
0
        private void ReceiveCallback(IAsyncResult ar)
        {
            string content = "";

            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));
                    content = state.sb.ToString();
                    if (content.IndexOf("EOF") > -1)
                    {
                        //All the data has arrived; put it in response.
                        if (state.sb.Length > 1)
                        {
                            response = state.sb.ToString(0, state.sb.Length - 3);
                        }
                        // Signal that all bytes have been received.
                        receiveDone.Set();
                    }
                    else
                    {
                        // Get the rest of the data.
                        client.BeginReceive(state.buffer, 0, stateobject.BufferSize, 0, new AsyncCallback(ReceiveCallback), state);
                    }
                }
            }
            catch (ObjectDisposedException e)
            {
                //Console.WriteLine(e.ToString());
                //MethodInvoker action = delegate { textBoxOutput.Text += e.ToString() + "\r\n"; };
                //textBoxOutput.BeginInvoke(action);
            }
        }