a avali
a avali

Reputation: 11

use asynchronus socket to receive data

I use 2 ways to receive data with a socket connected to an IP address.

  1. Synchronous receive.
  2. Asynchronous receive.

Using synchronous receive, I get all response from the server in less than 1 second. But by using asynchronous receive, it takes over 30 seconds to receive all the responses.

Here is the code:

 class StateObject
{
    // Client socket.
    public Socket workSocket = null;
    // Size of receive buffer.
    public const int BufferSize = 256;
    // Receive buffer.
    public byte[] buffer = new byte[BufferSize];
    // Received data string.
    public StringBuilder sb = new StringBuilder();
}

    private static 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());
        }
    }

    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.
                client.ReceiveTimeout = 500;
                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 that all bytes have been received.
                receiveDone.Set();
            }
        }
        catch (Exception e)
        {
            MessageBox.Show(e.ToString());
        }
    }

Upvotes: 0

Views: 604

Answers (1)

Ebad Masood
Ebad Masood

Reputation: 2379

You should check for your value after you append the current string to your buffer, in order to process the message and remove the client.ReceiveTimeout = 500; which i think is taking time:

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

            response = state.sb.ToString();
            if (response.IndexOf("<EOF>") != -1)
            {
                state.sb.Clear();
                receiveDone.Set();
            }
            else
            {
                client.BeginReceive(state.buffer, 0, StateObject.BufferSize, 0,
                    new AsyncCallback(ReceiveCallback), state);
            }
        }

This will only work for one message with , for real-time multiple messages, I think you should try different approach.

Upvotes: 0

Related Questions