在客户端中使用 C 编程中的 UDP 检查服务器何时上线

标签 c linux udp

我用 C 语言编写了一个普通的 udp 客户端服务器程序,用于将数据从客户端发送到服务器,并从服务器接收已发送消息的确认。 所以我的问题是,如果我的服务器关闭并且我的客户端向服务器发送消息.. 我的客户如何感知服务器是启动还是关闭(不工作)。 欢迎所有建议..并且没有找到与感测服务器相关的任何内容..所以请帮助我解决这个问题

客户端代码在这里

 #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;

}

最佳答案

检测服务器是否宕机的常用方法是向其发送消息并在有限的时间内等待答复(也许 30 秒,也许 15 分钟,取决于应用程序和网络......)。在您的情况下,您必须在客户端中设置超时,您将在发送消息时开始并在收到确认时停止。如果在“唤醒”之前未停止超时,则会触发异常或设置特定变量(这些是通知应用程序超时已过而服务器没有答复的常用方法)。

关于在客户端中使用 C 编程中的 UDP 检查服务器何时上线,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25692947/

相关文章:

c++ - 使用 UDP(WinSock 和 C++)发送和接收结构?

c - 需要帮助使用 gcc 编译 c 程序

c++ - int32 溢出后可能的值是多少?

c - 警报处理程序完成时出现 malloc 段错误

javascript - (GObjectIntrospection) 尝试使用 javascript 创建 ibus 引擎时出现段错误

java - 通过 udp 发送的图像已损坏

python - 通过 serial1 将 Arduno Yun pin 信息发送到 linino python 脚本,通过 UDP 发送到 Max

c - GCC 将指针转换为不兼容的类型

linux - 无法准备上下文 : path ".\r" not found

linux - 如何在 VM box 中的 Ubuntu Linux 中安装 Hadoop 和 Hive?