RStyle
RStyle

Reputation: 111

A request to send or receive data was disallowed because the socket is not connected . .

After a long break, i tried to refresh my memory of System.Net.Sockets but i am encountering problems with just connect even 2 machines.

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

Server Code:

private void startButton_Click(object sender, EventArgs e)
        {
            LocalEndpoint = new IPEndPoint(IPAddress.Parse("192.168.1.103"), 4444);
            _Socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);

            _Socket.Bind(LocalEndpoint);
            _Socket.Listen(10);
            _Socket.BeginAccept(new AsyncCallback(Accept), _Socket);
        }

private void Accept(IAsyncResult _IAsyncResult)
        {
            Socket AsyncSocket = (Socket)_IAsyncResult.AsyncState;
            AsyncSocket.EndAccept(_IAsyncResult);

            buffer = new byte[1024];

            AsyncSocket.BeginReceive(buffer, 0, buffer.Length, SocketFlags.None, new AsyncCallback(Receive), AsyncSocket);
        }

        private void Receive(IAsyncResult _IAsyncResult)
        {
            Socket AsyncSocket = (Socket)_IAsyncResult.AsyncState;
            AsyncSocket.EndReceive(_IAsyncResult);

            strReceive = Encoding.ASCII.GetString(buffer);

            Update_Textbox(strReceive);

            buffer = new byte[1024];

            AsyncSocket.BeginReceive(buffer, 0, buffer.Length, SocketFlags.None, new AsyncCallback(Receive), AsyncSocket);
        }

Client Code:

 private void connectButton_Click(object sender, EventArgs e)
        {
            RemoteEndPoint = new IPEndPoint(IPAddress.Parse("192.168.1.103"), 4444);
            _Socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);

            _Socket.BeginConnect(RemoteEndPoint, new AsyncCallback(Connect), _Socket);
        }

 private void Connect(IAsyncResult _IAsyncResult)
        {
            Socket RemoteSocket = (Socket)_IAsyncResult.AsyncState;
            RemoteSocket.EndConnect(_IAsyncResult);
        }

Upvotes: 3

Views: 10061

Answers (1)

Sali
Sali

Reputation: 71

The error is due to following line in your Accept function:

AsyncSocket.EndAccept(_IAsyncResult);

Server listens to a particular socket and it uses it to accept connections from the client. You can not use same socket to accept connections and receive data from the client. If you look at the help of Socket.EndAccept it says that it creates a new Socket to handle remote host communication. In your code you are using _Socket to listen for client connections and to receive data. You can modify this line to:

Socket dataSocket = AsyncSocket.EndAccept(_IAsyncResult);

You also need to pass this new socket to BeginReceive function parameter as:

AsyncSocket.BeginReceive(buffer, 0, buffer.Length, SocketFlags.None, new AsyncCallback(Receive), dataSocket);

Upvotes: 6

Related Questions