Socket exception when trying to connect to the socket 
Author Message
 Socket exception when trying to connect to the socket

Hi,

I'm having strange problem with TcpClient.

I'm reporting message to me server and have to do it very often.

Very often I got those error when I tried to connect to the TcpClient
            An operation was attempted on something that is not a socket
or this one
            Cannot access a disposed object named
"System.Net.Sockets.TcpClient".

The code look as followed
    private void Connect( string strIp, int nPort )
    {
        if( m_oTcpClient != null )
        {
            m_oTcpClient.Close();
            GC.SuppressFinalize( m_oTcpClient );
            m_oTcpClient = null;
        }
        m_oTcpClient = new TcpClient( );
        m_oTcpClient.Connect( strIp, nPort );
    }

any ideas

Best regards
Gunnar



Wed, 06 Oct 2004 00:14:27 GMT  
 Socket exception when trying to connect to the socket
Gunnar,

    You shouldn't be doing what you are doing, trying to suppress the
finalization of the tcp client.  Basically, your code should look like this:

     private void Connect( string strIp, int nPort )
     {
         if( m_oTcpClient != null )
         {
             m_oTcpClient.Close();
            // THIS IS BAD!  DO NOT DO THIS!
            // GC.SuppressFinalize( m_oTcpClient );
             m_oTcpClient = null;
         }
         m_oTcpClient = new TcpClient( );
         m_oTcpClient.Connect( strIp, nPort );
     }

    See if that works.

    Hope this helps.

--
               - Nicholas Paldino [.NET MVP]


Quote:
> Hi,

> I'm having strange problem with TcpClient.

> I'm reporting message to me server and have to do it very often.

> Very often I got those error when I tried to connect to the TcpClient
>             An operation was attempted on something that is not a socket
> or this one
>             Cannot access a disposed object named
> "System.Net.Sockets.TcpClient".

> The code look as followed
>     private void Connect( string strIp, int nPort )
>     {
>         if( m_oTcpClient != null )
>         {
>             m_oTcpClient.Close();
>             GC.SuppressFinalize( m_oTcpClient );
>             m_oTcpClient = null;
>         }
>         m_oTcpClient = new TcpClient( );
>         m_oTcpClient.Connect( strIp, nPort );
>     }

> any ideas

> Best regards
> Gunnar



Wed, 06 Oct 2004 00:33:36 GMT  
 Socket exception when trying to connect to the socket
Nicholas, in the docs, the Active and Client properties of the TcpClient
class has this sample code:

// This derived class demonstrates the use of three protected methods
// belonging to the TcpClient class
public class MyTcpClientDerivedClass : TcpClient
{
// Constructor for the derived class.
public MyTcpClientDerivedClass() : base(){}
public void UsingProtectedMethods()
{
  // Uses the protected 'Active' property belonging to the TcpClient base
class
  // to determine if a connection is established.
  if (this.Active)
  {
      // Calls the protected 'Client' property belonging to the
      // TcpClient base class.
      Socket s = this.Client;
      // Uses the Socket returned by Client to set an option that
      // is not available using TcpClient.
      s.SetSocketOption(SocketOptionLevel.Socket,
SocketOptionName.Broadcast, 1);
  }
  // To free all resources, calls the protected virtual method Dispose
  // belonging to the TcpClient base class.
 this.Dispose(true);
 GC.SuppressFinalize(this);

Quote:
}
}

I don't understand whey the Finalize method is being suppressed in this
example. Ideas?

-glenn-



Quote:
> Gunnar,

>     You shouldn't be doing what you are doing, trying to suppress the
> finalization of the tcp client.  Basically, your code should look like
this:

>      private void Connect( string strIp, int nPort )
>      {
>          if( m_oTcpClient != null )
>          {
>              m_oTcpClient.Close();
>             // THIS IS BAD!  DO NOT DO THIS!
>             // GC.SuppressFinalize( m_oTcpClient );
>              m_oTcpClient = null;
>          }
>          m_oTcpClient = new TcpClient( );
>          m_oTcpClient.Connect( strIp, nPort );
>      }

>     See if that works.

>     Hope this helps.

> --
>                - Nicholas Paldino [.NET MVP]



> > Hi,

> > I'm having strange problem with TcpClient.

> > I'm reporting message to me server and have to do it very often.

> > Very often I got those error when I tried to connect to the TcpClient
> >             An operation was attempted on something that is not a socket
> > or this one
> >             Cannot access a disposed object named
> > "System.Net.Sockets.TcpClient".

> > The code look as followed
> >     private void Connect( string strIp, int nPort )
> >     {
> >         if( m_oTcpClient != null )
> >         {
> >             m_oTcpClient.Close();
> >             GC.SuppressFinalize( m_oTcpClient );
> >             m_oTcpClient = null;
> >         }
> >         m_oTcpClient = new TcpClient( );
> >         m_oTcpClient.Connect( strIp, nPort );
> >     }

> > any ideas

> > Best regards
> > Gunnar



Wed, 06 Oct 2004 00:51:51 GMT  
 Socket exception when trying to connect to the socket
In classes that implement both Finalize and Dispose ,SuppressFinalize should generally be called after
Dispose(true), since this method persumably had allready done all the necessary clean-up . Without
SuppressFinalize, the collection of the object will be unnecessarily delayed from the next garbage collection
to the one after.(in the first collection, the GC will only mark the object to begin Finalize).
HTH,
--
Yizhaq Shmaayahoo
Quote:

> Nicholas, in the docs, the Active and Client properties of the TcpClient
> class has this sample code:

> // This derived class demonstrates the use of three protected methods
> // belonging to the TcpClient class
> public class MyTcpClientDerivedClass : TcpClient
> {
> // Constructor for the derived class.
> public MyTcpClientDerivedClass() : base(){}
> public void UsingProtectedMethods()
> {
>   // Uses the protected 'Active' property belonging to the TcpClient base
> class
>   // to determine if a connection is established.
>   if (this.Active)
>   {
>       // Calls the protected 'Client' property belonging to the
>       // TcpClient base class.
>       Socket s = this.Client;
>       // Uses the Socket returned by Client to set an option that
>       // is not available using TcpClient.
>       s.SetSocketOption(SocketOptionLevel.Socket,
> SocketOptionName.Broadcast, 1);
>   }
>   // To free all resources, calls the protected virtual method Dispose
>   // belonging to the TcpClient base class.
>  this.Dispose(true);
>  GC.SuppressFinalize(this);
> }
> }

> I don't understand whey the Finalize method is being suppressed in this
> example. Ideas?

> -glenn-



> > Gunnar,

> >     You shouldn't be doing what you are doing, trying to suppress the
> > finalization of the tcp client.  Basically, your code should look like
> this:

> >      private void Connect( string strIp, int nPort )
> >      {
> >          if( m_oTcpClient != null )
> >          {
> >              m_oTcpClient.Close();
> >             // THIS IS BAD!  DO NOT DO THIS!
> >             // GC.SuppressFinalize( m_oTcpClient );
> >              m_oTcpClient = null;
> >          }
> >          m_oTcpClient = new TcpClient( );
> >          m_oTcpClient.Connect( strIp, nPort );
> >      }

> >     See if that works.

> >     Hope this helps.

> > --
> >                - Nicholas Paldino [.NET MVP]



> > > Hi,

> > > I'm having strange problem with TcpClient.

> > > I'm reporting message to me server and have to do it very often.

> > > Very often I got those error when I tried to connect to the TcpClient
> > >             An operation was attempted on something that is not a socket
> > > or this one
> > >             Cannot access a disposed object named
> > > "System.Net.Sockets.TcpClient".

> > > The code look as followed
> > >     private void Connect( string strIp, int nPort )
> > >     {
> > >         if( m_oTcpClient != null )
> > >         {
> > >             m_oTcpClient.Close();
> > >             GC.SuppressFinalize( m_oTcpClient );
> > >             m_oTcpClient = null;
> > >         }
> > >         m_oTcpClient = new TcpClient( );
> > >         m_oTcpClient.Connect( strIp, nPort );
> > >     }

> > > any ideas

> > > Best regards
> > > Gunnar



Wed, 06 Oct 2004 02:07:49 GMT  
 Socket exception when trying to connect to the socket
Could you explain the problem a little better?

Thorir

Quote:
>-----Original Message-----
>Hi,

>I'm having strange problem with TcpClient.

>I'm reporting message to me server and have to do it very
often.

>Very often I got those error when I tried to connect to
the TcpClient
>            An operation was attempted on something that
is not a socket
>or this one
>            Cannot access a disposed object named
>"System.Net.Sockets.TcpClient".

>The code look as followed
>    private void Connect( string strIp, int nPort )
>    {
>        if( m_oTcpClient != null )
>        {
>            m_oTcpClient.Close();
>            GC.SuppressFinalize( m_oTcpClient );
>            m_oTcpClient = null;
>        }
>        m_oTcpClient = new TcpClient( );
>        m_oTcpClient.Connect( strIp, nPort );
>    }

>any ideas

>Best regards
>Gunnar

>.



Wed, 06 Oct 2004 01:58:25 GMT  
 Socket exception when trying to connect to the socket
Hi glenn

I create a class that derive the Tcp client and add the Dispose( true ) and
GC.SuppressFinalize(this) in my Dispose() function.

I still get these errors
        Cannot access a disposed object named MyTcpClient
or    An operation was attempted on something that is not a socket

but it only happend in about 25 times each 1000 times that I connect to my
server.



Wed, 06 Oct 2004 02:02:36 GMT  
 Socket exception when trying to connect to the socket
Doh! Oh course. It looks like TcpClient implements IDisposable so of course
the SuppressFinalize call is made...

-glenn-


Quote:
> In classes that implement both Finalize and Dispose ,SuppressFinalize

should generally be called after
Quote:
> Dispose(true), since this method persumably had allready done all the

necessary clean-up . Without
Quote:
> SuppressFinalize, the collection of the object will be unnecessarily

delayed from the next garbage collection
Quote:
> to the one after.(in the first collection, the GC will only mark the

object to begin Finalize).
Quote:
> HTH,
> --
> Yizhaq Shmaayahoo



Quote:
> > Nicholas, in the docs, the Active and Client properties of the TcpClient
> > class has this sample code:

> > // This derived class demonstrates the use of three protected methods
> > // belonging to the TcpClient class
> > public class MyTcpClientDerivedClass : TcpClient
> > {
> > // Constructor for the derived class.
> > public MyTcpClientDerivedClass() : base(){}
> > public void UsingProtectedMethods()
> > {
> >   // Uses the protected 'Active' property belonging to the TcpClient
base
> > class
> >   // to determine if a connection is established.
> >   if (this.Active)
> >   {
> >       // Calls the protected 'Client' property belonging to the
> >       // TcpClient base class.
> >       Socket s = this.Client;
> >       // Uses the Socket returned by Client to set an option that
> >       // is not available using TcpClient.
> >       s.SetSocketOption(SocketOptionLevel.Socket,
> > SocketOptionName.Broadcast, 1);
> >   }
> >   // To free all resources, calls the protected virtual method Dispose
> >   // belonging to the TcpClient base class.
> >  this.Dispose(true);
> >  GC.SuppressFinalize(this);
> > }
> > }

> > I don't understand whey the Finalize method is being suppressed in this
> > example. Ideas?

> > -glenn-


wrote

> > > Gunnar,

> > >     You shouldn't be doing what you are doing, trying to suppress the
> > > finalization of the tcp client.  Basically, your code should look like
> > this:

> > >      private void Connect( string strIp, int nPort )
> > >      {
> > >          if( m_oTcpClient != null )
> > >          {
> > >              m_oTcpClient.Close();
> > >             // THIS IS BAD!  DO NOT DO THIS!
> > >             // GC.SuppressFinalize( m_oTcpClient );
> > >              m_oTcpClient = null;
> > >          }
> > >          m_oTcpClient = new TcpClient( );
> > >          m_oTcpClient.Connect( strIp, nPort );
> > >      }

> > >     See if that works.

> > >     Hope this helps.

> > > --
> > >                - Nicholas Paldino [.NET MVP]



> > > > Hi,

> > > > I'm having strange problem with TcpClient.

> > > > I'm reporting message to me server and have to do it very often.

> > > > Very often I got those error when I tried to connect to the
TcpClient
> > > >             An operation was attempted on something that is not a
socket
> > > > or this one
> > > >             Cannot access a disposed object named
> > > > "System.Net.Sockets.TcpClient".

> > > > The code look as followed
> > > >     private void Connect( string strIp, int nPort )
> > > >     {
> > > >         if( m_oTcpClient != null )
> > > >         {
> > > >             m_oTcpClient.Close();
> > > >             GC.SuppressFinalize( m_oTcpClient );
> > > >             m_oTcpClient = null;
> > > >         }
> > > >         m_oTcpClient = new TcpClient( );
> > > >         m_oTcpClient.Connect( strIp, nPort );
> > > >     }

> > > > any ideas

> > > > Best regards
> > > > Gunnar



Wed, 06 Oct 2004 03:04:11 GMT  
 Socket exception when trying to connect to the socket
    Bingo =)

    Sorry it took me so long to respond, but yes, because the Dispose method
is called, the suppression of finalization should occur in there.

    Also, the SuppressFinalize method on GC strikes me as a method that is
used by an object to control it's own finalization, not other object's
finalization routines.

i


Quote:
> Doh! Oh course. It looks like TcpClient implements IDisposable so of
course
> the SuppressFinalize call is made...

> -glenn-



> > In classes that implement both Finalize and Dispose ,SuppressFinalize
> should generally be called after
> > Dispose(true), since this method persumably had allready done all the
> necessary clean-up . Without
> > SuppressFinalize, the collection of the object will be unnecessarily
> delayed from the next garbage collection
> > to the one after.(in the first collection, the GC will only mark the
> object to begin Finalize).
> > HTH,
> > --
> > Yizhaq Shmaayahoo


> > > Nicholas, in the docs, the Active and Client properties of the
TcpClient
> > > class has this sample code:

> > > // This derived class demonstrates the use of three protected methods
> > > // belonging to the TcpClient class
> > > public class MyTcpClientDerivedClass : TcpClient
> > > {
> > > // Constructor for the derived class.
> > > public MyTcpClientDerivedClass() : base(){}
> > > public void UsingProtectedMethods()
> > > {
> > >   // Uses the protected 'Active' property belonging to the TcpClient
> base
> > > class
> > >   // to determine if a connection is established.
> > >   if (this.Active)
> > >   {
> > >       // Calls the protected 'Client' property belonging to the
> > >       // TcpClient base class.
> > >       Socket s = this.Client;
> > >       // Uses the Socket returned by Client to set an option that
> > >       // is not available using TcpClient.
> > >       s.SetSocketOption(SocketOptionLevel.Socket,
> > > SocketOptionName.Broadcast, 1);
> > >   }
> > >   // To free all resources, calls the protected virtual method Dispose
> > >   // belonging to the TcpClient base class.
> > >  this.Dispose(true);
> > >  GC.SuppressFinalize(this);
> > > }
> > > }

> > > I don't understand whey the Finalize method is being suppressed in
this
> > > example. Ideas?

> > > -glenn-


> wrote

> > > > Gunnar,

> > > >     You shouldn't be doing what you are doing, trying to suppress
the
> > > > finalization of the tcp client.  Basically, your code should look
like
> > > this:

> > > >      private void Connect( string strIp, int nPort )
> > > >      {
> > > >          if( m_oTcpClient != null )
> > > >          {
> > > >              m_oTcpClient.Close();
> > > >             // THIS IS BAD!  DO NOT DO THIS!
> > > >             // GC.SuppressFinalize( m_oTcpClient );
> > > >              m_oTcpClient = null;
> > > >          }
> > > >          m_oTcpClient = new TcpClient( );
> > > >          m_oTcpClient.Connect( strIp, nPort );
> > > >      }

> > > >     See if that works.

> > > >     Hope this helps.

> > > > --
> > > >                - Nicholas Paldino [.NET MVP]



> > > > > Hi,

> > > > > I'm having strange problem with TcpClient.

> > > > > I'm reporting message to me server and have to do it very often.

> > > > > Very often I got those error when I tried to connect to the
> TcpClient
> > > > >             An operation was attempted on something that is not a
> socket
> > > > > or this one
> > > > >             Cannot access a disposed object named
> > > > > "System.Net.Sockets.TcpClient".

> > > > > The code look as followed
> > > > >     private void Connect( string strIp, int nPort )
> > > > >     {
> > > > >         if( m_oTcpClient != null )
> > > > >         {
> > > > >             m_oTcpClient.Close();
> > > > >             GC.SuppressFinalize( m_oTcpClient );
> > > > >             m_oTcpClient = null;
> > > > >         }
> > > > >         m_oTcpClient = new TcpClient( );
> > > > >         m_oTcpClient.Connect( strIp, nPort );
> > > > >     }

> > > > > any ideas

> > > > > Best regards
> > > > > Gunnar



Wed, 06 Oct 2004 03:59:22 GMT  
 
 [ 8 post ] 

 Relevant Pages 

1. SocketException at Socket.Connect()

2. Socket Connect

3. Windows client does not connect to Unix socket

4. Help with connect to socket

5. socket (and connect problem)

6. connecting to smtp server through sockets

7. using socket to connect to telnet port

8. getpeername on connected sockets

9. Help: socket connecting timing

10. socket() and connect()?

11. Socket connect problem

12. Debugger will no longer connect with sockets.

 

 
Powered by phpBB® Forum Software