Asynchronous client socket not receiving data

I am trying to implement my application with asynchronous socket communication. It linked fine and sent a request, but I didn't receive any data from the server (Java server). Connector connection

client.BeginConnect(hostname, port,
            new AsyncCallback(ConnectCallback), client);
connectDone.WaitOne();

  private static void ConnectCallback(IAsyncResult ar) {
    try {
        // Retrieve the socket from the state object.
        Socket client = (Socket) ar.AsyncState;

        // Complete the connection.
        client.EndConnect(ar);

        Console.WriteLine("Socket connected to {0}",
            client.RemoteEndPoint.ToString());

        // Signal that the connection has been made.
        connectDone.Set();
    } catch (Exception e) {
        Console.WriteLine(e.ToString());
    }
}

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, SocketFlags.None,
            new AsyncCallback(ReceiveCallback), client);
    }
    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);
        Console.WriteLine(response);
        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.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)
    {
        Console.WriteLine(e.ToString());
    }
}

      

Any help would be appreciated.

+3


source to share


1 answer


Your code is almost fine, with one major error. In your BeginReceive, you are placing a client socket instance as a state object. In the ReceiveCallback function, you are passing it to a StateObject instead of a socket. This will throw an exception on the console.

However, a breakpoint at the beginning of the ReceiveCallback function is also triggered with this error. Have you tried this? An exception should be thrown anyway.



If this breakpoint does not fire, you have to check yourself if something was actually sent from the server. Of course, this all assumed the connection was working as you said.

0


source







All Articles