tags:

views:

7

answers:

0

Ok My chat application when successfully set up, both server and clients start their Socket .beginrecieve() operation and listens throughout thier runtime.

    private void Accept(IAsyncResult iar)
    {

        Socket winsock = (Socket)iar.AsyncState;
        g_server_conn = winsock.EndAccept(iar);



        //Function that exchanges names of each other
        NewClient(g_server_conn);

        Socket server_conn = g_server_conn;

        chat_msg = new byte[1024];
        recv_result= server_conn.BeginReceive(chat_msg, 0, chat_msg.Length, SocketFlags.None, new AsyncCallback(Recieve), server_conn);

    }

    private void Recieve(IAsyncResult iar)
    {
        Socket server_conn = (Socket)iar.AsyncState;


        server_conn.EndReceive(iar);

        //If clients shutdown connection,Server recieves /exit command
        if (Encoding.ASCII.GetString(chat_msg, 0, chat_msg.Length) == "/exit")
        {
            g_server_conn.Shutdown(SocketShutdown.Both);
            g_server_conn.Close();
            return;
        }
        SetLabel(client_name, chatListBox);
        SetLabel(Encoding.ASCII.GetString(chat_msg), chatListBox);

        chat_msg = new byte[1024];
        server_conn.BeginReceive(chat_msg, 0, chat_msg.Length, SocketFlags.None, new AsyncCallback(Recieve), server_conn);


    }

Once i press the disconnect button this is what i did.

    private void stopButton_Click(object sender, EventArgs e)
    {

        recv_result.AsyncWaitHandle.Close();



       g_server_conn.Shutdown(SocketShutdown.Both);
        g_server_conn.Close();
    }

But it resulted in a error when i pressed the button. It states that problem with "server_conn.EndReceive(iar);" . Cannot access a disposed object