-4

I am having a hard time where to put the BeginReceive. I am new to this. Can someone help me with this. Every time I call the _clientSocket.Close(); I got an error message.

Cannot access a disposed object. Object name: System.net.Sockets.Socket

Your suggestions will much appreciated. Correct me if I'm wrong thank you!

I edited the whole question because I cannot ask again and I need to improve how to ask a question. Sorry I'm new to this page.

private void ReceiveData(IAsyncResult ar)
{
     try
     {
         int received = _clientSocket.EndReceive(ar);
         if (received == 0)
         {
             return;
         }

         Array.Resize(ref receivedBuf, received);
         string text = Encoding.Default.GetString(receivedBuf);

         if (text == "Server: -O")
         {
             Thread NT = new Thread(() =>
             {
                 this.BeginInvoke((Action)delegate ()
                 {
                   textBox1.Text = "Guest";
                   this.Hide();
                   _clientSocket.Close();
                   Usertimer us = new Usertimer(textBox1.Text);
                   us.Show();
                 });
             });
             NT.Start();

         }

         Array.Resize(ref receivedBuf, _clientSocket.ReceiveBufferSize);
         AppendtoTextBox(text);
         _clientSocket.BeginReceive(receivedBuf, 0, receivedBuf.Length, SocketFlags.None, new AsyncCallback(ReceiveData), null);
     }
     catch (Exception ex)
     {
         MessageBox.Show(ex.Message, Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error);
     }
}
John Denver
  • 11
  • 1
  • 7

1 Answers1

0

Before you do anything, please read this article on how to debug a small program. It will help this time and every time in the future.

Since we don't have a minimal, complete and verifiable example, I'm going to make some assumptions.

It looks like you continuously add to your __ClientSockets list every time you click. Since there is no code that removes from __ClientSockets, then every subsequent click and call to Sendata simply adds to more ip's, even if you unselected them from your checkboxes. Depending on how you want your program to work, you can either add code that removes clients from __ClientSockets, or you can clear the list completely every time you click on the button. Up to you.

Either way, you will find your problem by debugging a small program.

Guillaume CR
  • 3,006
  • 1
  • 19
  • 31
  • Sir are you there? Can you help me with this? – John Denver Aug 17 '18 at 20:15
  • @JohnDenver This seems like a completely new question. You should create a new question for it, that will get you a lot more attention. Did you read both articles I linked to? They should help you with your code and with asking the right question to get a quick answer. I can't tell which line in your new example fails, but it does look like you close your socket when you receive "Server: -O". Once you closed it, you can't call BeginReceive again. That will fail. – Guillaume CR Aug 21 '18 at 14:51
  • Asking another question takes a week. It fails when i try to Open another form I need to close the socket so the client sockets will not be doubled in Checklistbox. When another form opens it connects to server again. And the server can remote it back to Login form. I get error every time I Close the form and the sockets. Sorry for my English. – John Denver Aug 27 '18 at 06:51
  • @JohnDenver Your English is fine. This is a new question that needs a new minimal, complete and verifiable example. Please read the two pages I linked in my answer, and ask a new question. – Guillaume CR Aug 27 '18 at 14:00
  • Already read that. There's no error in my code. It can run smooth but every time I open and close a form it comes up. I want to get rid of it. The program can run with it but its to irritating. Because when I try to open a client it will pop up. And when the time of the client is done. The form will close and the login will come out with another pop up of Cannot access a disposed object. Object name: System.net.Sockets.Socket – John Denver Aug 27 '18 at 16:11
  • Thank you for your time I'm going to delete this and ask a new question. – John Denver Aug 27 '18 at 16:12