locked
Socket.Disconnect( ) RRS feed

  • Question

  • Socket.Disconnect(bool reuse) according to msdn enables the program to reuse the socket 
    my questions are:

    1)
    if we decide to reuse the socket does that mean it will reuse the same local end point as well,what i'm  asking is, 
    would it preserve the port for that socket or would it just(i don't believe it does) 
    save all the objects resources ? 

    2)if in effect it does only save the resources would they be allocated to the next connection and if so do the send/receive buffers automaticly flush or is there some way the flush them ,
    does Socket.ShutDown(ShutDown.Both) do that for me on disconnect ?

    3) in what situation would i use Socket.Disconnect(false) , if the all purpose is to reuse the socket what would this grant me ?
    Friday, February 18, 2011 7:13 AM

Answers

  • Hi Eranotz65,

    Welcome to the MSDN Forum.

    1. As the code shows below(the code comes from the reflector) .

    public void Disconnect(bool reuseSocket)
    {
      if (s_LoggingEnabled)
      {
        Logging.Enter(Logging.Sockets, this, "Disconnect", (string) null);
      }
      if (this.CleanedUp)
      {
        throw new ObjectDisposedException(base.GetType().FullName);
      }
      if (!ComNetOS.IsPostWin2K)
      {
        throw new PlatformNotSupportedException(SR.GetString("WinXPRequired"));
      }
      SocketError success = SocketError.Success;
      if (!this.DisconnectEx_Blocking(this.m_Handle.DangerousGetHandle(), IntPtr.Zero, reuseSocket ? 2 : 0, 0))
      {
        success = (SocketError) Marshal.GetLastWin32Error();
      }
      if (success != SocketError.Success)
      {
        SocketException socketException = new SocketException(success);
        this.UpdateStatusAfterSocketError(socketException);
        if (s_LoggingEnabled)
        {
          Logging.Exception(Logging.Sockets, this, "Disconnect", socketException);
        }
        throw socketException;
      }
      this.SetToDisconnected();
      this.m_RemoteEndPoint = null;
      if (s_LoggingEnabled)
      {
        Logging.Exit(Logging.Sockets, this, "Disconnect", (string) null);
      }
    }
    

    "this.m_RemoteEndPoint = null;" means we can not use the previous remote end point. But for the local one, we still can use it. 

    2. "Socket.Close()" will do work when you try to flush the resource.

     Socket.ShutDown() only disables sends and receives on a socket.

    3.Try to use Socket.Disconnect(false) in your most situation.

    Hope it is helpful for you.

    Best Regards,


    Cookie Luo[MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    • Marked as answer by Cookie Luo Saturday, February 26, 2011 4:31 PM
    Monday, February 21, 2011 6:32 AM

All replies

  • Hi Eranotz65,

    Welcome to the MSDN Forum.

    1. As the code shows below(the code comes from the reflector) .

    public void Disconnect(bool reuseSocket)
    {
      if (s_LoggingEnabled)
      {
        Logging.Enter(Logging.Sockets, this, "Disconnect", (string) null);
      }
      if (this.CleanedUp)
      {
        throw new ObjectDisposedException(base.GetType().FullName);
      }
      if (!ComNetOS.IsPostWin2K)
      {
        throw new PlatformNotSupportedException(SR.GetString("WinXPRequired"));
      }
      SocketError success = SocketError.Success;
      if (!this.DisconnectEx_Blocking(this.m_Handle.DangerousGetHandle(), IntPtr.Zero, reuseSocket ? 2 : 0, 0))
      {
        success = (SocketError) Marshal.GetLastWin32Error();
      }
      if (success != SocketError.Success)
      {
        SocketException socketException = new SocketException(success);
        this.UpdateStatusAfterSocketError(socketException);
        if (s_LoggingEnabled)
        {
          Logging.Exception(Logging.Sockets, this, "Disconnect", socketException);
        }
        throw socketException;
      }
      this.SetToDisconnected();
      this.m_RemoteEndPoint = null;
      if (s_LoggingEnabled)
      {
        Logging.Exit(Logging.Sockets, this, "Disconnect", (string) null);
      }
    }
    

    "this.m_RemoteEndPoint = null;" means we can not use the previous remote end point. But for the local one, we still can use it. 

    2. "Socket.Close()" will do work when you try to flush the resource.

     Socket.ShutDown() only disables sends and receives on a socket.

    3.Try to use Socket.Disconnect(false) in your most situation.

    Hope it is helpful for you.

    Best Regards,


    Cookie Luo[MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    • Marked as answer by Cookie Luo Saturday, February 26, 2011 4:31 PM
    Monday, February 21, 2011 6:32 AM
  • Hi Eranotz65,

    I mark the reply as answer. If you have any other questions, please unmark it.

    Thanks

     


    Cookie Luo[MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Saturday, February 26, 2011 4:33 PM