CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7
  1. #1
    Join Date
    Feb 2004
    Location
    Germany
    Posts
    153

    Detecting closed connection

    Hi,

    I'm using standard-winsock(2.0) writing a simple TCP server application.

    First i listen to a port, then accept an incomming connection and then
    doing some communication.

    I know, if the client closes the connection gracefully a final Send with 0-Bytes is done.

    Now my question:
    Is it possible to detect anyhow, if the client closed the connection NOT gracefully,
    for example removing the network-cable, killing his application, or something like this.

    Any suggestions are welcome.
    Matze

  2. #2
    Join Date
    Jan 2005
    Location
    Gothenburg, Sweden
    Posts
    134

    Re: Detecting closed connection

    If the application is killed then then kernel will close all open descriptors and when the socket gets closed, a FIN will be sent to the other side to indicate that the connection has been closed.

    There's no way to know if the cable has been disconnected except if you send something and doesn't get an answer at all. Also when the cable is disconnected the connection is not closed. It will remain open on both sides and once you connect the cable again you will be able to continue the communication.

    You might receive a RST if the cable is disconnected and the host is restared and then the cable is connected again. The connection will be ESTBLISHED on the other side and when that side retransmits the data a RST will be sent back from the restarted host.
    Last edited by svenhag; March 17th, 2005 at 04:37 AM.
    If you try and take a cat apart to see how it works,
    the first thing you'll have on your hands is a nonworking cat

  3. #3
    Join Date
    Feb 2004
    Location
    Germany
    Posts
    153

    Re: Detecting closed connection

    Hi svenhag,

    thanks for your reply.
    What exactly do i receive when a RST is sent.

    I know, that when a FIN is sent, i get 0 Bytes in a "recv" call.

    I think the disconnected-cable-case i can live with.

    Matze

  4. #4
    Join Date
    Jan 2005
    Location
    Gothenburg, Sweden
    Posts
    134

    Re: Detecting closed connection

    A RST will make recv return -1 with errno set to ECONNRESET.
    Last edited by svenhag; March 17th, 2005 at 04:35 AM.
    If you try and take a cat apart to see how it works,
    the first thing you'll have on your hands is a nonworking cat

  5. #5
    Join Date
    Feb 2004
    Location
    Germany
    Posts
    153

    Re: Detecting closed connection

    Voila,

    thank you very much.

    This will work for me.

    Matze

  6. #6
    Join Date
    Jan 2005
    Location
    Gothenburg, Sweden
    Posts
    134

    Re: Detecting closed connection

    Edit my posts a little since they were 'pre-coffee' posts :P and contained some errors.
    If you try and take a cat apart to see how it works,
    the first thing you'll have on your hands is a nonworking cat

  7. #7
    Join Date
    Feb 2004
    Location
    Germany
    Posts
    153

    Re: Detecting closed connection

    Ah see,

    don't mind.
    It's still OK for me.

    So i will go and get a cup of coffee too, to avoid
    dangerous pre-coffee effects on my programming .

    Greetings,
    Matze

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured