0

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());
        }
    }
Ebad Masood
  • 2,389
  • 28
  • 46
a avali
  • 11
  • What is your question? – Yuval Itzchakov Apr 16 '15 at 06:32
  • I think the question is clear (*why* the delay) - I am not 100% sure but can you test the `SocketFlags.Partial` flag with your code? – Random Dev Apr 16 '15 at 06:34
  • how can i receive data as fast as synchronous method? – a avali Apr 16 '15 at 06:37
  • also the `if (bytes Read > 0) ... else { ... receiveDone.Set(); }` looks dubious - you need a 0-byte read for this to be set (and this will most likely only happen if the other side disconnects - or so I guess) – Random Dev Apr 16 '15 at 06:38
  • based on my last comment and your code it seems like you can set `receiveDone` as soon as you read anything (you only need 1 ASCII character - 1 byte) - so why don't you `.Set()` just after `state.sb.Append(...)`)? – Random Dev Apr 16 '15 at 06:40
  • btw (sorry for the spam) can you please post the sync. code as well? – Random Dev Apr 16 '15 at 06:41
  • To Carsten Konig: after first call to BeginReceive when bytesRead = 0 it means no byte was read and all data was received, but why is take over 30 seconds?!! – a avali Apr 16 '15 at 06:48
  • I suspect it will take as long as the other side will be connected ... can you please post the sync. code? – Random Dev Apr 16 '15 at 07:27

1 Answers1

0

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.

Ebad Masood
  • 2,389
  • 28
  • 46