Q:

Check whenever server comes online using UDP in C programming in client

i have written a normal udp client server programm in  C programming to send data from client to server and recieve an acknowledgment from it for the message been sent..
so my question is that incase if my server is down and my client sends a message to server ..
how can my client sense whether the server is up or down(nt working).
all sugesstions are welcomed .. and didnot find nythin related to sensing a server.. so please help me on this
the code for client is here
 #include<netinet/in.h> 
 #include <sys/types.h> 
 #include <sys/socket.h> 
 #include<stdio.h> 
 #include <arpa/inet.h> 
 #include <string.h> 
   #include<fcntl.h> 
#include<unistd.h>
#include<stdlib.h>
#include<sqlite3.h>

 void enterdb(char *);

int main() 
  { 
int sfd,l; 
char *buf=(char*)malloc(100*sizeof(char)),buf1[10]="";//=(char *)malloc(100*sizeof(char)); 
struct sockaddr_in server,client; 
sfd=socket(AF_INET,SOCK_DGRAM,0); 
     int sender_len=sizeof(server);
struct msghdr msg;
 struct iovec iov;

  bzero(&server,sizeof(server)); 
  server.sin_family=AF_INET; 
  server.sin_port=htons(1300); 
  server.sin_addr.s_addr=inet_addr("127.0.0.1");

   msg.msg_name = &server;
   msg.msg_namelen = sender_len;
msg.msg_iov = &iov;
msg.msg_iovlen = sizeof(buf1);
msg.msg_iov->iov_base = buf1;
msg.msg_iov->iov_len = 9;
msg.msg_control = 0;
msg.msg_controllen = 0;
 msg.msg_flags = 0;

    printf("Enter the message:"); 
    gets(buf); 
    char *test="quit";
    if(strcmp(test,buf)==0)
        {
        printf("now exiting\n");
        close(sfd);
        exit(0);
        return 0;
        }            
    else 
        {
        int s;
        s=sendto(sfd,buf,strlen(buf),0,(struct sockaddr *)&server,sizeof(server));

        printf("control passed on here and message sent is of %d bytes \n",s);  

        printf("control passed on here again and message sent is of %d bytes\n ",s);    
        //sleep(5);
        int length = sizeof(server);

        printf("%s\n",buf1);

        int x = recvmsg(sfd,&msg,MSG_DONTWAIT);
        if(x<0)
        {
        printf("error");
        }
        /*while( ( x =recvfrom(sfd,buf1,100,0,(struct sockaddr*)&server,&length)))
        { 
        if(x<0)
        return 1;   
        break;
        }*/

        else {
        printf("%s\n",buf1);
        char *recv="recieved";
        if(strcmp(recv,buf1) != 0 )
            {
            printf("**** control is in connect ***\n");  
            enterdb(buf);
            }
        else 
            {
            memset(buf,0,strlen(buf)); 
            printf("MESSAGE FOR SERVER : you sent me the message has been- %s\n",buf1);
                } 
        }
    }

close(sfd);
return 0;

} 

A:

The usual way of detecting whether a server is down is by sending it a message and waiting for the answer for a limited time (maybe 30s, maybe 15 min, depending on the application and the network...). In your case, you'd have to setup a time-out in your client, which you would start on sending a message and stop when receiving the acknowledgment. If the time-out is not stopped before it "wakes up", then it triggers an exception or sets a specific variable (these are usual ways of informing the application that the time-out has elapsed without an answer from the server).

