How do I check if the connection is still alive?

This is a discussion on How do I check if the connection is still alive? within the C Programming forums, part of the General Programming Boards category; Hi there, I'm wondering is there a function out there that would tell if the socket connection is still alive(or ...

  1. #1
    Registered User jaro's Avatar
    Join Date
    Mar 2006
    Location
    In my Parent House
    Posts
    34

    How do I check if the connection is still alive?

    Hi there,

    I'm wondering is there a function out there that would tell if the socket connection is still alive(or connected).


    basically I use some of this to connect to the other "station" ( don't know if these code below would help,but I just want you to know)
    Code:
    gethostbyname(HOST)
    //..............
    clientService.sin_addr=*((struct in_addr*)h->h_addr);	 	/* IP address for hostname */
    clientService.sin_port = htons("4040");			/* smtp port */
    clientService.sin_family = AF_INET;  			/* internet */
    //................
    ConnectSocket = socket(AF_INET, SOCK_STREAM, 0);
    connect( ConnectSocket, (struct sockaddr*) &clientService, sizeof(struct sockaddr) )
    //...................
    Currently the programs works fine,it is able to retreive and process message from the other "station" (w/c acts a workspaceserver).
    The problem is that when I disconnect my pc from the network (by pulling out the network cable), the program still run as if it is still connected to the other "station".

    How do I check if the connection is still alive?

    Regards,
    Jaro

  2. #2
    cwr
    cwr is offline
    Registered Luser cwr's Avatar
    Join Date
    Jul 2005
    Location
    Sydney, Australia
    Posts
    869
    Presumably you later have a loop somewhere that checks for new data?

    If the connection is closed by the other end, you'll get an EOF on read() (it returns 0). On the other hand, if you try to write you'll get a return of -1 and EPIPE and the process will receive a SIGPIPE.

    In the case of your "pull out the network cable" scenario. TCP/IP has no in-built mechanism for detecting this (a TCP/IP stream with no data transfer results in no packets at all, ever), so you'd have to keep sending a "are you alive" type message, and wait for a reply or a socket error.

  3. #3
    Registered User jaro's Avatar
    Join Date
    Mar 2006
    Location
    In my Parent House
    Posts
    34
    thanks for the reply.
    appreciate it alot

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. multiple forks in one client connection (ftpclient)
    By Dynamo in forum Networking/Device Communication
    Replies: 5
    Last Post: 01-16-2011, 11:41 AM
  2. BN_CLICKED, change button style
    By bennyandthejets in forum Windows Programming
    Replies: 13
    Last Post: 07-05-2010, 11:42 PM
  3. reuse a socket connection
    By radeberger in forum Networking/Device Communication
    Replies: 0
    Last Post: 03-18-2009, 11:38 AM
  4. Replies: 6
    Last Post: 12-19-2008, 04:49 PM
  5. Replies: 1
    Last Post: 10-03-2002, 09:52 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21