目录
- 一、select函数
- 二、poll函数
select函数
int select(int nfds, fd_set *readfds, fd_set *writefds,
fd_set *exceptfds, struct timeval *timeout);
poll函数
int poll(struct pollfd *fds, nfds_t nfds, int timeout);
epoll API
epoll_create
epoll_wait
epoll_ctl
一、select函数
select函数
int select(int nfds, fd_set *readfds, fd_set *writefds,
fd_set *exceptfds, struct timeval *timeout);
//nfds: 是三个集合中编号最高的文件描述符,加上 1
//readfds/writefds/exceptfds:
可读集合/可写集合/异常集合
//timeout:
NULL:永久阻塞
0:非阻塞模式
struct timeval {
long tv_sec; /* 秒 */
long tv_usec; /* 微秒 */
};
fd_sed函数结构体:
/*将文件描述符从集合中删除*/
void FD_CLR(int fd, fd_set *set);
/*查看文件描述符是否存在于集合当中*/
int FD_ISSET(int fd, fd_set *set);
/*添加文件描述符*/
void FD_SET(int fd, fd_set *set);
/*初始化集合*/
void FD_ZERO(fd_set *set);
实现代码:
net.h
#ifndef _NET_H_
#define _NET_H_
#include <stdio.h>
#include <stdlib.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netinet/tcp.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <strings.h>
#include <errno.h>
typedef struct sockaddr Addr;
typedef struct sockaddr_in Addr_in;
#define BACKLOG 5
#define ErrExit(msg) do { perror(msg); exit(EXIT_FAILURE); } while(0)
void Argment(int argc, char *argv[]);
int CreateSocket(char *argv[]);
int DataHandle(int fd);
#endif
server.c
#include "net.h"
#include <sys/select.h>
#define MAX_SOCK_FD 1024
int main(int argc, char *argv[])
{
int i, ret, fd, newfd;
fd_set set, tmpset;
Addr_in clientaddr;
socklen_t clientlen = sizeof(Addr_in);
/*检查参数,小于3个 直接退出进程*/
Argment(argc, argv);
/*创建已设置监听模式的套接字*/
fd = CreateSocket(argv);
FD_ZERO(&set);
FD_ZERO(&tmpset);
FD_SET(fd, &set);
while(1){
tmpset = set;
if( (ret = select(MAX_SOCK_FD, &tmpset, NULL, NULL, NULL)) < 0)
ErrExit("select");
if(FD_ISSET(fd, &tmpset) ){
/*接收客户端连接,并生成新的文件描述符*/
if( (newfd = accept(fd, (Addr *)&clientaddr, &clientlen) ) < 0)
perror("accept");
printf("[%s:%d]已建立连接\n",
inet_ntoa(clientaddr.sin_addr), ntohs(clientaddr.sin_port));
FD_SET(newfd, &set);
}else{ //处理客户端数据
for(i = fd + 1; i < MAX_SOCK_FD; i++){
if(FD_ISSET(i, &tmpset)){
if( DataHandle(i) <= 0){
if( getpeername(i, (Addr *)&clientaddr, &clientlen) )
perror("getpeername");
printf("[%s:%d]断开连接\n",
inet_ntoa(clientaddr.sin_addr), ntohs(clientaddr.sin_port));
FD_CLR(i, &set);
}
}
}
}
}
return 0;
}
socket.c
#include "net.h"
void Argment(int argc, char *argv[]){
if(argc < 3){
fprintf(stderr, "%s<addr><port>\n", argv[0]);
exit(0);
}
}
int CreateSocket(char *argv[]){
/*创建套接字*/
int fd = socket(AF_INET, SOCK_STREAM, 0);
if(fd < 0)
ErrExit("socket");
/*允许地址快速重用*/
int flag = 1;
if( setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &flag, sizeof(flag) ) )
perror("setsockopt");
/*设置通信结构体*/
Addr_in addr;
bzero(&addr, sizeof(addr) );
addr.sin_family = AF_INET;
addr.sin_port = htons( atoi(argv[2]) );
/*绑定通信结构体*/
if( bind(fd, (Addr *)&addr, sizeof(Addr_in) ) )
ErrExit("bind");
/*设置套接字为监听模式*/
if( listen(fd, BACKLOG) )
ErrExit("listen");
return fd;
}
int DataHandle(int fd){
char buf[BUFSIZ] = {};
Addr_in peeraddr;
socklen_t peerlen = sizeof(Addr_in);
if( getpeername(fd, (Addr *)&peeraddr, &peerlen) )
perror("getpeername");
int ret = recv(fd, buf, BUFSIZ, 0);
if(ret < 0)
perror("recv");
if(ret > 0){
printf("[%s:%d]data: %s\n",
inet_ntoa(peeraddr.sin_addr), ntohs(peeraddr.sin_port), buf);
}
return ret;
}
运行结果:
二、poll函数
int poll(struct pollfd *fds, nfds_t nfds, int timeout);
fds:
struct pollfd {
int fd; /* 文件描述符 */
short events; /* 请求的事件 */
short revents; /* 返回的事件 */
};
poll函数事件类型:
events:
POLLIN:有数据可读
POLLPRI:有紧急数据需要读取
POLLOUT: 文件可写
.....
"添加多个事件类型: events = POLLIN | POLLPRT"
nfds:
fds的个数
timeout:
设置阻塞的时间(毫秒)
0为非阻塞
负数表示永久阻塞
实现代码:
net.h
#ifndef _NET_H_
#define _NET_H_
#include <stdio.h>
#include <stdlib.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netinet/tcp.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <strings.h>
#include <errno.h>
typedef struct sockaddr Addr;
typedef struct sockaddr_in Addr_in;
#define BACKLOG 5
#define ErrExit(msg) do { perror(msg); exit(EXIT_FAILURE); } while(0)
void Argment(int argc, char *argv[]);
int CreateSocket(char *argv[]);
int DataHandle(int fd);
#endif
server.c
#include "net.h"
#include <poll.h>
#define MAX_SOCK_FD 1024
int main(int argc, char *argv[])
{
int i, j, fd, newfd;
nfds_t nfds = 1;
struct pollfd fds[MAX_SOCK_FD] = {};
Addr_in addr;
socklen_t addrlen = sizeof(Addr_in);
/*检查参数,小于3个 直接退出进程*/
Argment(argc, argv);
/*创建已设置监听模式的套接字*/
fd = CreateSocket(argv);
fds[0].fd = fd;
fds[0].events = POLLIN;
while(1){
if( poll(fds, nfds, -1) < 0)
ErrExit("poll");
for(i = 0; i < nfds; i++){
/*接收客户端连接,并生成新的文件描述符*/
if(fds[i].fd == fd && fds[i].revents & POLLIN){
if( (newfd = accept(fd, (Addr *)&addr, &addrlen) ) < 0)
perror("accept");
fds[nfds].fd = newfd;
fds[nfds++].events = POLLIN;
printf("[%s:%d][nfds=%lu] connection successful.\n",
inet_ntoa(addr.sin_addr), ntohs(addr.sin_port), nfds);
}
/*处理客户端数据*/
if(i > 0 && fds[i].revents & POLLIN){
if(DataHandle(fds[i].fd) <= 0){
if( getpeername(fds[i].fd, (Addr *)&addr, &addrlen) < 0)
perror("getpeername");
printf("[%s:%d][fd=%d] exited.\n",
inet_ntoa(addr.sin_addr), ntohs(addr.sin_port), fds[i].fd);
close(fds[i].fd);
for(j=i; j<nfds-1; j++)
fds[j] = fds[j+1];
nfds--;
i--;
}
}
}
}
close(fd);
return 0;
}
socket.c
#include "net.h"
void Argment(int argc, char *argv[]){
if(argc < 3){
fprintf(stderr, "%s<addr><port>\n", argv[0]);
exit(0);
}
}
int CreateSocket(char *argv[]){
/*创建套接字*/
int fd = socket(AF_INET, SOCK_STREAM, 0);
if(fd < 0)
ErrExit("socket");
/*允许地址快速重用*/
int flag = 1;
if( setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &flag, sizeof(flag) ) )
perror("setsockopt");
/*设置通信结构体*/
Addr_in addr;
bzero(&addr, sizeof(addr) );
addr.sin_family = AF_INET;
addr.sin_port = htons( atoi(argv[2]) );
/*绑定通信结构体*/
if( bind(fd, (Addr *)&addr, sizeof(Addr_in) ) )
ErrExit("bind");
/*设置套接字为监听模式*/
if( listen(fd, BACKLOG) )
ErrExit("listen");
return fd;
}
int DataHandle(int fd){
char buf[BUFSIZ] = {};
Addr_in peeraddr;
socklen_t peerlen = sizeof(Addr_in);
if( getpeername(fd, (Addr *)&peeraddr, &peerlen) )
perror("getpeername");
int ret = recv(fd, buf, BUFSIZ, 0);
if(ret < 0)
perror("recv");
if(ret > 0){
printf("[%s:%d]data: %s\n",
inet_ntoa(peeraddr.sin_addr), ntohs(peeraddr.sin_port), buf);
}
return ret;
}
运行结果: