Connect a socket with timeout

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Mike Schilling

    Connect a socket with timeout

    How does one put a timeout on an attempt to connect a socket? There's no
    timeout parameter to Socket.Connect( ), and while there are socket options
    for send and receive timeouts, there is none for connect timeout.


  • Nicholas Paldino [.NET/C# MVP]

    #2
    Re: Connect a socket with timeout

    Mike,

    In order to do this, you will have to time the asynchronous event.
    Basically, make a call to BeginConnect, and start a timer at the same time,
    with an interval of your timeout.

    In the timer function, set a flag indicating if the timer has fired.

    Then, in the callback for the BeginConnect method, you check the flag.
    If it is set, then you timed out, if not, then you haven't timed out.

    You can then elaborate, by creating an Event that is set when the
    timeout occurs, or the connection is created. Then, you have your thread
    wait on both of those events. Depending on the event fired, you would then
    know if there is a timeout, or if the connection succeeded.

    Hope this helps.


    --
    - Nicholas Paldino [.NET/C# MVP]
    - mvp@spam.guard. caspershouse.co m

    "Mike Schilling" <ap@newsgroup.n ospam> wrote in message
    news:OyYLmJ3VGH A.4416@TK2MSFTN GP15.phx.gbl...[color=blue]
    > How does one put a timeout on an attempt to connect a socket? There's no
    > timeout parameter to Socket.Connect( ), and while there are socket options
    > for send and receive timeouts, there is none for connect timeout.
    >[/color]


    Comment

    • Mike Schilling

      #3
      Re: Connect a socket with timeout


      "Nicholas Paldino [.NET/C# MVP]" <mvp@spam.guard .caspershouse.c om> wrote in
      message news:u52JJq3VGH A.5288@TK2MSFTN GP14.phx.gbl...[color=blue]
      > Mike,
      >
      > In order to do this, you will have to time the asynchronous event.
      > Basically, make a call to BeginConnect, and start a timer at the same
      > time, with an interval of your timeout.
      >
      > In the timer function, set a flag indicating if the timer has fired.
      >
      > Then, in the callback for the BeginConnect method, you check the flag.
      > If it is set, then you timed out, if not, then you haven't timed out.[/color]

      I thought about something like that, but couldn't find a way to abort the
      connection attempt if the timer fires and it's still pending. EndConnect is
      documented as synchronously completing the connection, and it's not clear
      from the docs what Close or Shutdown would do.
      [color=blue]
      >
      > You can then elaborate, by creating an Event that is set when the
      > timeout occurs, or the connection is created. Then, you have your thread
      > wait on both of those events. Depending on the event fired, you would
      > then know if there is a timeout, or if the connection succeeded.
      >[/color]


      Comment

      • Kevin Yu [MSFT]

        #4
        Re: Connect a socket with timeout

        Hi Mike,

        The Close method will release all the resource on the Socket. Shutdown
        method will disable the socket from sending and receiving. So, I suggest
        you call Close method on the Socket to cancel connecting.

        Kevin Yu
        =======
        "This posting is provided "AS IS" with no warranties, and confers no
        rights."

        Comment

        Working...