代码简洁,只供学习研究。
#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地址,并在连接成功上位机可以定时测试下位机状况,如有僵死状况,发出警告信号。
该代码为考虑,自动退出功能。如需添加可以在代码中添加时间控制。