Search code examples
c#socketsasynchronoustcpasyncsocket

Async Socket listener sending data error


I'm creating a wrapper class around the Socket class.

I have a connect async callback which does this:

public void StartConnecting()
{
    // Connect to a remote device.
    try
    {
        //_acceptIncomingData = acceptIncomingData;

        // Create a TCP/IP socket.
        _workingSocket = new Socket(AddressFamily.InterNetwork,
                SocketType.Stream, ProtocolType.Tcp);

        if (Information != null)
            Information(this, new InfoEventArgs("Connecting", "Working socket is initiating connection"));

        // Connect to the remote endpoint.
        _workingSocket.BeginConnect(_localEndPoint,
                new AsyncCallback(ConnectCallback), _workingSocket);
    }
    catch (Exception ex)
    {
        if (Error != null)
            Error(this, new ErrorEventArgs(ex.Message, ex));
    }
}

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

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

        // ACTION CONNECTED COMPLETE
        if (Information != null)
            Information(this, new InfoEventArgs("Connected", "Working socket has now connected"));

        // Start Receiving on the socket

        // 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 ex)
    {
        if (Error != null)
            Error(this, new ErrorEventArgs(ex.Message, ex));
    }
}

And for the state object it uses a predifined class:

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

I use this to connect to a socket that has begun listening, I call this listening socket the Server and it uses the Socket class. The code above is a socket that I call the Client. When the Client connects the Server, the Client is now allowed to send data to the Server. However when I want to receive data sent from the Server I get the following error message:

A request to send or receive data was disallowed because the socket is not connected and (when sending on the datagram socket using a sendto call) no address was supplied.

What am I missing? or not doing correctly?

After being prompted to check whether the Server is listening I looked at what I call the WorkingSocket in my wrapper class which is the socket that is being used, I checked this out after I had a successful Server listen/Client connect negotiate. It does say the socket isn't connected. So my question now is:

Does a Server (listening socket) need to be connected to the Client to send data, how do you do this if you have one Server for many Clients?


Solution

  • I had a misunderstanding of how sockets worked back in January. The error was caused because i didn't understand that when a Client connects to a listening socket, the listening socket then creates a new socket object where all data sent and received from the client will link back to.

    In my example above I should create and store a new socket for the client to act on, and use this to send and receive data on.