Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3273916
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T18:58:56+00:00 2026-05-17T18:58:56+00:00

Hi i have a disconnect button click event in my SERVER application as follows.Before

  • 0

Hi i have a disconnect button click event in my SERVER application as follows.Before it dc, it will alert other clients by sending a “/exit” command.After that it will shutdown its connection.

 private void stopButton_Click(object sender, EventArgs e)
    {
        byte[] exit_command = Encoding.ASCII.GetBytes("/exit");
        g_server_conn.BeginSend(exit_command, 0, exit_command.Length, SocketFlags.None, new AsyncCallback(Send), g_server_conn);
        g_server_conn.Shutdown(SocketShutdown.Both);
        g_server_conn.Close();
    }

The problem is that the server is executing Socket.BeginRecieve() method all the time. How do we tell the begin recieve method to stop its operation so that i can close properly.

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];
        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);


    }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-17T18:58:57+00:00Added an answer on May 17, 2026 at 6:58 pm

    You should use the IAsyncResult returned by the BeginXXX methods. IIRC, you can dispose of the WaitHandle.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an application that connects to a server. In the status bar I
I have written a network server class that maintains a std::set of network clients.
I have a android application with lots of buttons. If a button is pressed
I have the following code: public void disconnect() { running = false; if(thread !=
have not tested on windows. but in ubuntu when u disconnect from the network,
I have a StartComObjects function called when the user presses the Login button and
I have a TCP server running which spits out messages of 2 bytes at
I have a custom templated control(toolbar) that contains a custom usercontrol(button) The button uses
I'm trying to figure out why when you have multiple callback clients connected using
i have two views 1st one is displaying list (uitableview used), user will select

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.