detect UDP server failure

This is a discussion on detect UDP server failure within the Networking/Device Communication forums, part of the General Programming Boards category; I'm trying to write a udp client program that will detect if the server went down. This program is for ...

  1. #1
    Registered User
    Join Date
    Mar 2009
    Posts
    12

    detect UDP server failure

    I'm trying to write a udp client program that will detect if the server went down. This program is for a linux or BSD system. Here's a short test program:
    Code:
    #include <netinet/in.h>
    #include <stdio.h>
    #include <string.h>
    #include <unistd.h>  /*sleep()*/
    
    int main(int argc, char**argv)
    {
        int srcsock;
        struct sockaddr_in server_addr;
        char sendst[1000];    
        int PORTNUM = 4000;
        int numrec, res, junk;
    
        srcsock=socket(AF_INET,SOCK_DGRAM,0);
        server_addr.sin_family = AF_INET;
        server_addr.sin_addr.s_addr=inet_addr(argv[1]);
        server_addr.sin_port=htons(PORTNUM);
    
        connect(srcsock, (struct sockaddr *)&server_addr, sizeof(server_addr));
        sleep(1);
        strcpy(sendst,"This is just junk1");
        numrec = write(srcsock,sendst,strlen(sendst));
        printf("%d\n", numrec);
        sleep(1);
        strcpy(sendst,"This is just junk2");
        numrec = write(srcsock,sendst,strlen(sendst));
        printf("%d\n", numrec);
        sleep(1);
        strcpy(sendst,"This is just junk3");
        numrec = write(srcsock,sendst,strlen(sendst));
        printf("%d\n", numrec);
        sleep(1);
        strcpy(sendst,"This is just junk4");
        numrec = write(srcsock,sendst,strlen(sendst));
        printf("%d\n", numrec);
        sleep(1);
        strcpy(sendst,"This is just junk5");
        numrec = write(srcsock,sendst,strlen(sendst));
        printf("%d\n", numrec);
    
        close(srcsock);
    }
    The return from this is the correct string length for each string if the server is up, but when the server is down when I run the program, it returns:
    19
    -1
    19
    -1
    19

    Is there a reliable way to detect if the remote server died either during the message, or if the message could not be sent? I know it's UDP, but other programs seem to have this ability.
    Last edited by trinli; 03-17-2009 at 08:16 AM.

  2. #2
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    You may get more reliable results by using send instead of write...
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  3. #3
    Registered User
    Join Date
    Mar 2009
    Posts
    12
    Thanks for the input, but it didn't solve the problem.

    I replaced the write command with both:
    numrec = send(srcsock,sendst,strlen(sendst),0);

    or

    numrec = send(srcsock,sendst,strlen(sendst),MSG_CONFIRM);

    And, in both cases, I get the same result.
    I'm still playing with this using the various flags, but not making much headway.

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Just sending a message to a UDP server will probably not tell you if the packet arrived or not, or even if it was possible to send it. You need to find a way that the other end is responding to the UDP packet to find out if it's working or not.

    If you have control of the server-side software, you can introduce some sort of "If you receive this, send something back" type thingy. If that's not the case, then you need to figure some other way to send packets that get a response back (a server that simply swallows the packets is pretty useless, so one would expect that the server does have some sort of response to certain packets, right?)

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Well, according to the docs: "Note that the successful completion of a send does not indicate that the data was successfully delivered.". You might check errno, though, it might be set if an error occured...
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  6. #6
    Registered User
    Join Date
    Mar 2009
    Posts
    12
    I'll have to talk to the guy writing the server side. At current, as I understand it. There's no response at all. But, because I do get a -1 result from this (and it's in a pattern), it seems to me that I should be able to get a pass/fail result somewhat reliably. Is there something in the UDP protocol that responds and that value is getting returned/registered on the second send?

    I'm somewhat a novice at UDP programming, so hopefully I'm not asking completely stupid questions. TCP, I understand pretty well. And, TCP would be simple to detect a failure.

  7. #7
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    A couple more minor ideas. Specifiying SO_SNDTIMEO in your socket options (non-windows systems only) may work. Also, have you tried sendto? Sorry I'm not of much help - I'm flying blind here, never done any UDP myself...
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  8. #8
    Registered User
    Join Date
    Mar 2009
    Posts
    12
    Quote Originally Posted by Sebastiani View Post
    Well, according to the docs: "Note that the successful completion of a send does not indicate that the data was successfully delivered.". You might check errno, though, it might be set if an error occured...
    I'll check that. Thanks

    And, yeah. Unfortunately, I started with using sendto.

  9. #9
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,272
    With UDP you never know.

    The way other programs "deal with this" is to wait a while. Then they give up.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  10. #10
    Registered User
    Join Date
    Mar 2009
    Posts
    12
    Well, errno does give me back a connection error consistently after the first failed send. This might be good enough for what I need. I'll have to run some tests to figure out if it's reliable. But, being UDP, it might not be.

  11. #11
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by brewbuck View Post
    With UDP you never know.

    The way other programs "deal with this" is to wait a while. Then they give up.
    And I take it you mean "wait a while" means "wait for a response".

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Running two UDP server simultaneously using select()
    By PiJ in forum Networking/Device Communication
    Replies: 15
    Last Post: 01-31-2008, 10:49 AM
  2. problems with linux UDP server
    By Lopizda in forum Networking/Device Communication
    Replies: 5
    Last Post: 04-26-2007, 09:42 AM
  3. Using select() for client UDP
    By jazzman83 in forum C Programming
    Replies: 2
    Last Post: 04-03-2007, 06:31 AM
  4. Multi-threaded UDP Server Problem
    By nkhambal in forum C Programming
    Replies: 0
    Last Post: 07-05-2005, 03:27 PM
  5. socket question
    By Unregistered in forum C Programming
    Replies: 3
    Last Post: 07-19-2002, 02:54 PM

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