Udp心跳包代码
//1.client.c
#include<stdio.h>
#include <sys/socket.h>#include<sys/types.h>
#include<stdlib.h>
#include<errno.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include<string.h>
#include<pthread.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/time.h>
#include <unistd.h>
#include <sys/select.h>
#include<signal.h>
#define IP "127.0.0.1"
#define PORT 5678
char szBuf[64]={'\0'};
void livesndhandler(void *arg)
{//专门发送5秒心跳
int sfd=*(int *)arg;
struct sockaddr_in saddr;
saddr.sin_family=AF_INET;
saddr.sin_port=htons(PORT);
saddr.sin_addr.s_addr=inet_addr(IP);//本机合法IP
bzero(saddr.sin_zero,8);
memset(szBuf,0,64);
while(1)
{//先传living后接收
strcpy(szBuf,"want OK!");
sendto(sfd,(void*)szBuf,sizeof(szBuf),0,(struct sockaddr *)&saddr,sizeof(struct sockaddr));
sleep(5);
}
}
void timeout(int signo)
{//超时退出5秒
if(signo==SIGALRM)
printf("you are 超时 !\n");
exit(-1);
}
void livercvhandler(void *arg)
{//专门接收心跳回复
int sfd=*(int *)arg;
struct sockaddr_in saddr;
saddr.sin_family=AF_INET;
saddr.sin_port=htons(PORT);
saddr.sin_addr.s_addr=inet_addr(IP);//本机合法IP
bzero(saddr.sin_zero,8);
socklen_t len=sizeof(struct sockaddr);
while(1)
{
memset(szBuf,0,64);
signal(SIGALRM,timeout); //信号
int remain=alarm(10);//定时10s
recvfrom(sfd,(void *)szBuf,sizeof(szBuf),0,(struct sockaddr *)&saddr,&len);//接受OK
while(0!=strncasecmp(szBuf,"OK",2))
{
memset(szBuf,0,64);
recvfrom(sfd,(void *)szBuf,sizeof(szBuf),0,(struct sockaddr *)&saddr,&len);
}
}
return ;
}
int main(int argc,char** argv)
{
int sfd=socket(AF_INET,SOCK_DGRAM,0);
if(-1==sfd)
{
perror("socket");
exit(-1);
}
signal(SIGALRM, timeout);//timeout信号处理函数
pthread_t live,livercv;
pthread_create(&live,NULL,(void*)livesndhandler,(void*)&sfd); //专门发送living
pthread_create(&livercv,NULL,(void*)livercvhandler,(void*)&sfd);
pthread_join(live,NULL);
pthread_join(livercv,NULL);
close(sfd);
return 0;
}
//2.server.c
#include<stdio.h>
#include <sys/socket.h>
#include<sys/types.h>
#include<stdlib.h>
#include<errno.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include<string.h>
#define IP "127.0.0.1"
#define PORT 5678
int Udp_Socket_Init()
{
//1.socket() udp协议
int sfd=socket(AF_INET,SOCK_DGRAM,0);
if(-1==sfd)
{
perror("socket");
exit(-1);
}
// 2.setsockopt
int optval=1;
socklen_t len=sizeof(optval);
int ret=setsockopt(sfd, SOL_SOCKET, SO_REUSEADDR,
(void*)&optval, sizeof(optval));
if(-1==ret)
{
perror("setsockopt");
exit(-1);
}
//3.bind()
struct sockaddr_in saddr;
saddr.sin_family=AF_INET;
saddr.sin_port=htons(PORT);
saddr.sin_addr.s_addr=inet_addr(IP);
bzero(saddr.sin_zero,8);//预留成员
ret=bind(sfd,(struct sockaddr*)&saddr,sizeof(struct sockaddr));
if(-1==ret)
{
perror("bind");
exit(-1);
}
return sfd;
}
void deal_client(int sfd)
{
struct sockaddr_in src_addr;
socklen_t addrlen=sizeof(struct sockaddr_in);
char szBuf[64]={'\0'};
while(1)
{
recvfrom(sfd,(void *)szBuf,sizeof(szBuf),0,(struct sockaddr *)&src_addr,&addrlen);
printf("Get a beat:%s\n",szBuf);
if(0==strncmp("quit",szBuf,4)) { close(sfd); break; }
memset(szBuf,0,64);
printf("服务器写(OK):");
scanf("%s",szBuf);
sendto(sfd,(void*)szBuf,sizeof(szBuf),0,(struct sockaddr *)&src_addr,addrlen);
memset(szBuf,0,64);
}
return ;
}
int main(int argc,char** argv)
{
int sfd=Udp_Socket_Init();
while(1) deal_client(sfd);
close(sfd);
return 0;
}