UDP测下位机心跳

代码简洁,只供学习研究。

#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include <sys/wait.h>

#include <unistd.h>
#include <fcntl.h>
#include <pthread.h>
#include <semaphore.h>
#include <malloc.h>


#define MYPORT 4000 /* the port users will be sending to */
#define MAXBUFLEN 100

struct list {
 struct list *next;
 struct sockaddr_in addr;
 int flag;
};

struct header{
 struct list*next;
 int len;
};

void InitHeader(struct header *head)

 head->next = NULL;
 head->len = 0;
}

void InserList(struct header* head,struct sockaddr_in addr)
{
 struct list* list;
  
 list = (struct list*)malloc(sizeof(struct list));
 list->addr = addr;
 list->flag = 1;
 list->next = head->next;
 head->next = list;
 head->len++;
}

int DeleteList(struct header* head,struct list* list)
{
 struct list *p;
 p = head->next;
 while(p->next != list && p != NULL) {
  p = p->next;
 }
 if( p == NULL) {
  return 0;
 } else {
  p->next = (p->next)->next;
  free(p->next);
  head->len--;
  return 1;
 }
}

int SerchList(struct header* head,struct sockaddr_in  addr)
{
 struct list* p;
 
 p = head->next;
 while (p != NULL){
  if((strcmp(inet_ntoa(p->addr.sin_addr) , inet_ntoa(addr.sin_addr ) ) == 0)) {
   p->flag = 1;
   break;
  }
  p = p->next;
 }
 if( p == NULL)
  return 0;
 
 return 1;
}


void InitFlag(struct header* head)
{
 struct list* p;
 p = head->next;
 while(p != NULL) {
  p->flag = 0;
  p = p->next;
 }
}

void SearchSend(struct header* head,int sockfd)
{
 struct list* p;
 p = head->next;
 while(p != NULL) {
  if(p->flag == 0) {
   sendto(sockfd,"warnning",strlen("warnning"),0,&(p->addr),sizeof(p->addr));
   printf("lose ip %s/n",inet_ntoa(p->addr.sin_addr));
  } else {
   p->flag = 0;
  }
  p = p->next;
 }
}


main()
{
 int sockfd,client_sockfd;
 struct sockaddr_in my_addr; /* my address information */
 struct sockaddr_in their_addr; /* connector's address information */
 int addr_len, numbytes;
 int server_len, client_len;
 char buf[MAXBUFLEN];
 int i;
 struct timeval timeval;
 fd_set readfds,testfds;
 struct header head;
 
 if ((sockfd = socket(AF_INET, SOCK_DGRAM, 0)) == -1) {
  perror("socket");
  exit(1);
 }
 my_addr.sin_family = AF_INET; /* host byte order */
 my_addr.sin_port = htons(MYPORT); /* short, network byte order */
 my_addr.sin_addr.s_addr = INADDR_ANY; /* auto-fill with my IP */
 bzero(&(my_addr.sin_zero), 8); /* zero the rest of the struct */
 
 if (bind(sockfd, (struct sockaddr *)&my_addr, sizeof(struct sockaddr)) == -1) {
  perror("bind");
  exit(1);
 }
 addr_len = sizeof(struct sockaddr);  
 fcntl(sockfd,F_SETFL,O_NONBLOCK);

 InitHeader(&head);
 while(1) {  
  while((numbytes=recvfrom(sockfd, buf, MAXBUFLEN, 0, (struct sockaddr *)&their_addr, &addr_len)) != -1) {
   if(SerchList(&head, their_addr)) {
    printf("got packet from %s/n",inet_ntoa(their_addr.sin_addr));
    printf("packet is %d bytes long/n",numbytes);
    buf[numbytes] = '/0';   
    printf("packet contains /"%s/"/n",buf);
    continue;
   } else {
    InserList(&head,their_addr);
    printf("add ip %s/n",inet_ntoa(their_addr.sin_addr));
    printf("packet is %d bytes long/n",numbytes);
    buf[numbytes] = '/0';   
    printf("packet contains /"%s/"/n",buf);
   }
  }
  SearchSend(&head,sockfd);
  InitFlag(&head);
  sleep(4);
 }
 close(sockfd);
}

 

以上为主机代码。

自动获取连接到得IP地址,并在连接成功上位机可以定时测试下位机状况,如有僵死状况,发出警告信号。

该代码为考虑,自动退出功能。如需添加可以在代码中添加时间控制。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值