Click here to Skip to main content
15,884,628 members
Please Sign up or sign in to vote.
3.00/5 (2 votes)
See more:
Please verify my code. I am working on tcp listener asynchronously. When i disconnect from all of my clients it gives the following error:
"Cannot access a disposed object.
Object name: System.Net.Sockets.Socket.
"

C#
 public static void StopServer()
        {
            try
            {
                Status = false;
                listenerSocket.Stop();
                backgroundworkerListener.CancelAsync();
                backgroundworkerListener.Dispose();

                if (ClientDevices != null)
                {
                    foreach (ClientManager mngr in ClientDevices)
                    {
                        mngr.Disconnect();
                    }
                    ClientDevices.Clear();
                }
                GC.Collect();
            }
            catch (ObjectDisposedException ode)
            {
                Console.WriteLine(ode.Message);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }


public bool Disconnect()
       {
           if (this.tcpClient != null )
           {
               try
               {
                   SocketAsyncEventArgs e = new SocketAsyncEventArgs();
                   this.networkStream.Flush();
                   this.networkStream.Close();
                   this.networkStream.Dispose();
                   this.tcpClient.Close();
                   this.tcpClient.Client.Close();
                  // this.tcpClient.Client.DisconnectAsync(e);
                   tcpClient.Client.Shutdown(SocketShutdown.Both);
                   GC.Collect();
                   return true;
               }
               catch (ObjectDisposedException oex)
               {
                   throw new ObjectDisposedException(base.GetType().FullName);
                 //  return false;
               }
               catch
               {
                   return false;
               }
           }
           else
               return true;
       }
Posted
Updated 30-Jun-14 22:31pm
v3
Comments
gggustafson 30-Jun-14 10:54am    
Why are you disconnecting clients from the server side? The usual model is to have the client disconnect from the server. I don't see the code where you set up your listener to handle multiple clients. You may need to consider a synchronous solution.
Muhammad Amman 1-Jul-14 3:23am    
Actually i created server app for 1000 of clients when i want to close my server application so that it will close and shutdown all clients that are connected it gives error on this line.

tcpClient.Client.Shutdown(SocketShutdown.Both);

It raises that exception:
Cannot access a disposed object.
Object name: System.Net.Sockets.Socket.

I want to shutdown it properly please help rahul thanks
Rahul VB 1-Jul-14 2:47am    
Ok, first of all tell me one thing : why have you used :
"this.networkStream.Dispose();". What i mean to say is that when you dispose an object then you cant access it again because the object becomes null.

also on which line does it throw an exception? To find that out firstly remove the try catch block and then tell me. You are bypassing the bug by using try-catch, so dont do that. Let the code break into the debugger and tell me exactly.

There are many things i need to point out:
1) You must know that system.exception is the base class of all the exceptions so if you want to catch an exception and you use a generalized exception handler, that will serve the purpose. But if you want to dig deep inside then you need to use specify the exception in try catch block so as to catch that exception.

So firstly remove the try catch block and tell me that as to where are you getting the exception.

Thanks and Regards,
Rahul
Muhammad Amman 1-Jul-14 3:23am    
Actually i created server app for 1000 of clients when i want to close my server application so that it will close and shutdown all clients that are connected it gives error on this line.

tcpClient.Client.Shutdown(SocketShutdown.Both);

It raises that exception:
Cannot access a disposed object.
Object name: System.Net.Sockets.Socket.

I want to shutdown it properly please help rahul thanks
Rahul VB 1-Jul-14 4:18am    
Ok, now do one thing just comment the line "this.tcpClient.Close()" and if you still get an error then comment the next line also which is : ""this.tcpClient.Client.Close();

Why am i saying this? It is because when you say Close() then the instance of that object is disposed. So you wont be able to access the object.

So now you need to try out what i said and then you need to run the code again.

Please do let me know, If it does not work, then we will try out something else.

Thanks a Ton,
Rahul

I resolved actually shutdown must be call before network stream closed.
Thanks Rahul
 
Share this answer
 
Comments
Rahul VB 1-Jul-14 7:16am    
Dont thank me friend. Its you who has done it. I just backed you.

Good....its Gooood !!!!
Use this one

C#
public bool Disconnect()
       {
           if (this.tcpClient != null )
           {
               try
               {
                   SocketAsyncEventArgs e = new SocketAsyncEventArgs();
                   this.networkStream.Flush();
                   this.networkStream.Close();
                   this.networkStream.Dispose(); 
                   //this.tcpClient.Client.DisconnectAsync(e);
                   tcpClient.Client.Shutdown(SocketShutdown.Both);
                   this.tcpClient.Client.Close();
                   this.tcpClient.Close();
                   GC.Collect();
                   return true;
               }
               catch (ObjectDisposedException oex)
               {
                   throw new ObjectDisposedException(base.GetType().FullName);
                 //  return false;
               }
               catch
               {
                   return false;
               }
           }
           else
               return true;
       }
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900