我read()
来自连接到服务器的客户端,同时,我select()
注意到来自 FIFO 的数据。现在,当数据写入 FIFO 时,select 会将所有数据写入客户端,但会一直返回,就好像它“准备好读取”一样。所以下一次读取设置为-1
and errno == EAGAIN
。它这样做直到达到 fdmax。虽然它工作正常。
但是,为什么我一直得到EAGAIN
?有没有更好的方法来处理这个?还是这是正确的方法?
注意:我正在传递O_RDWR|O_NONBLOCK
,因此它还可以保留read()
客户端发送的数据,而不仅仅是 FIFO。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <fcntl.h>
#include <errno.h>
#define PORT "9034"
int main(void) {
fd_set master, read_fds;
int fdmax, listener, newfd, sbytes, yes=1, i, j, rv;
struct sockaddr_storage remoteaddr; // client address
socklen_t addrlen;
char buf[256] = {0}, remoteIP[INET6_ADDRSTRLEN];
struct addrinfo hints, *ai;
FD_ZERO(&master);
FD_ZERO(&read_fds);
int fifo;
if ((mkfifo("/tmp/fifo", 0666)) < 0)
perror(strerror(errno));
if ((fifo = open("/tmp/fifo", O_RDWR|O_NONBLOCK)) < 0)
perror(strerror(errno));
// get us a socket and bind it
memset(&hints, 0, sizeof hints);
hints.ai_family = AF_UNSPEC;
hints.ai_socktype = SOCK_STREAM;
hints.ai_flags = AI_PASSIVE;
rv = getaddrinfo(NULL, PORT, &hints, &ai);
listener = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol);
setsockopt(listener, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(int));
bind(listener, ai->ai_addr, ai->ai_addrlen);
freeaddrinfo(ai);
listen(listener, 10);
FD_SET (fifo, &master);
FD_SET(listener, &master);
fdmax = listener;
for (;;) {
read_fds = master;
if (select(fdmax + 1, &read_fds, NULL, NULL, NULL) == -1) exit(4);
for (i = 0; i <= fdmax; i++) {
if (FD_ISSET(i, &read_fds)) {
if (i == listener) {
addrlen = sizeof remoteaddr;
newfd = accept(listener, (struct sockaddr * ) & remoteaddr, & addrlen);
FD_SET(newfd, & master);
if (newfd > fdmax)
fdmax = newfd;
} else if (i != fifo) {
recv(i, buf, sizeof buf, 0);
}
}
if (FD_ISSET(fifo, &read_fds)) {
sbytes = read (fifo, buf, sizeof (buf));
if (sbytes == -1 && errno == EAGAIN)
continue;
for(j = 0; j <= fdmax; j++) {
// send to everyone!
if (FD_ISSET(j, &master)) {
if (j != listener && j != i && j != fifo) {
if (send(j, buf, sbytes, 0) == -1) {
perror("send");
}
}
}
}
}
}
}
return 0;
}