Client can connect to server but crash when receiving data

0

Here's the part of the error code:

private void buttonAbrirConexão_Click(object sender, EventArgs e)
{
    try
    {
        socketOfServer = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
        socketOfServer.Bind(new IPEndPoint(IPAddress.Any, 6554));
        socketOfServer.Listen(100);
        socketOfServer.BeginAccept(AcceptedConection_Callback, null);
        MessageBox.Show("Servidor aberto!");
        buttonAbrirConexão.Enabled = false;
    }
    catch(Exception ex)
    {
        buttonAbrirConexão.Enabled = true;
        MessageBox.Show(ex.Message);
    }
}

private void AcceptedConection_Callback(IAsyncResult e)
{
    socketOfClientOnServer = socketOfServer.EndAccept(e);
    socketOfServer.BeginReceive(bufferOfServer, 0, bufferOfServer.Length, SocketFlags.None, AcceptedDataFromClient_Callback, socketOfClientOnServer); //O erro da nessa linha
    MessageBox.Show("Cliente conectado!");
}

First of all I follow the following steps, I click open server, a message appears saying that the server opened, then I click on connect using ip 127.0.0.1, if I remove the line that is giving error, it displays me a message saying that the client is connected. The error you are giving in the line is as follows:

"A request to send or receive data was not allowed because the socket is not connected and (when sending to a datagram socket using a sendto call) an address" Note: I connect only one client.

    
asked by anonymous 30.08.2016 / 02:47

0 answers