信号驱动IO是由操作系统回调到用户态的一种网络IO模型:
#include<stdio.h>
#include<sys/socket.h>
#include<sys/types.h>
#include<netinet/in.h>
#include<fcntl.h>
#include<signal.h>
#include<string.h>
#include<unistd.h>
int sockfd = 0;
void do_sigio(int sig) {
//从内核里面回调出来的
char buffer[256]={0};
struct sockaddr_in clientaddr;
int len = sizeof(struct sockaddr_in);
int rlen = recvfrom(sockfd,buffer,256,0,(struct sockaddr*)&clientaddr,(socklen_t*)&len);
int slen = sendto(sockfd,buffer,rlen, 0 ,(struct sockaddr*)&clientaddr,len);
}
int main() {
struct sigaction sigio_action;
sigio_action.sa_flags = 0;
sigio_action.sa_handler = do_sigio; //信号处理函数
sigaction(SIGIO,&sigio_action,NULL);//绑定信号和信号处理函数
sockfd = socket(AF_INET,SOCK_DGRAM,0);
struct sockaddr_in serv_addr;
memset(&serv_addr,0,sizeof(serv_addr));
serv_addr.sin_family = AF_INET;
serv_addr.sin_port = htons(6016);
serv_addr.sin_addr.s_addr = INADDR_ANY;
bind(sockfd,(struct sockaddr*)&serv_addr,sizeof(serv_addr));
//设置这个fd属于哪个进程
fcntl(sockfd,F_SETOWN,getpid());
//获取这个fd的状态
int flags = fcntl(sockfd,F_GETFL,0);
flags |= O_ASYNC | O_NONBLOCK;
//再设置这个FD非阻塞异步
flags = fcntl(sockfd,F_SETFL,flags);
while(1) sleep(1);
return 0;
}