CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Nov 2006
    Posts
    83

    How to test for connection

    How can a client see if it still is connected to the server in an tcp -connection?
    What I do now is to use the top protocol above tcp (a protocol implemented by me) to sent a message.
    As long as I can send a message I am connected. But that is really not the method I like to use since the message I sent is a little bit long and trigger some actions from the server which are unwanted. Some says I can use "select", I just don't know how.

  2. #2
    Join Date
    Jun 2005
    Location
    Sweden
    Posts
    33

    Re: How to test for connection

    Hello

    The socket in question will be selected for readability when the connection is closed, if you're using ::select(...). Use the return value from ::recv(...) to determine when the connection is closed, it will return zero ( http://beej.us/guide/bgnet/, 6.2. select()--Synchronous I/O Multiplexing ). You might also want to check out SO_KEEPALIVE, read more about it here: http://www.hmug.org/man/2/setsockopt.php .

    / boolman
    Last edited by boolman; March 23rd, 2007 at 11:32 AM.

  3. #3
    Join Date
    Nov 2006
    Posts
    83

    Re: How to test for connection

    "Use the return value from ::recv(...) to determine when the connection is closed, it will return zero"

    Sure, but will ::recv() not block until something is read in case the connection isn't lost? That is not what I want.
    I want to see if the connection is still there, and if not, I will try to establish a new socket connection.

  4. #4
    Join Date
    Jun 2005
    Location
    Sweden
    Posts
    33

    Re: How to test for connection

    As I tried to say in my previous post; if ::select(...) says that your socket is readable it means that you can either read/recv from it or that the connection has been closed. Check out Beej's guide to network programming for a good introduction to ::select(...): http://beej.us/guide/bgnet/ .

    Edit: If you're using some kind of GUI which must not block waiting for select, call it with a timeout set to {0, 0}, it will cause select to poll the given sockets.

    Good luck.
    Last edited by boolman; March 24th, 2007 at 11:46 AM.

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