我正在尝试用 C 语言编写一个 I/O 非阻塞的服务器,因为有时它会因洪水请求而崩溃。 环顾四周,我发现 I/O 非阻塞可以解决我的问题。 阅读 Beej 指南,我实现了 recvtimeout 函数,该函数设置超时来处理来自客户端的数据。 人们告诉我我必须使用 select 来避免这个问题,但我已经在函数 receivetimeout 中使用了它:
int Server::recvtimeout(int s, char *buf, int len, int timeout)
{
//Check if non-blocking
fcntl(s, F_SETFL, O_NONBLOCK);
int flags = fcntl(s, F_GETFD);
if ((flags & O_NONBLOCK) == O_NONBLOCK) {
fprintf(stderr, "nonblocking active");
}
else {
fprintf(stderr, "nonblocking not active");
}
//End check
fd_set fds;
int n;
struct timeval tv;
// set up the file descriptor set
FD_ZERO(&fds);
FD_SET(s, &fds);
// set up the struct timeval for the timeout
tv.tv_sec = timeout;
tv.tv_usec = 0;
// wait until timeout or data received
n = select(s+1, &fds, NULL, NULL, &tv);
if (n == 0){
return -2; // timeout!
}
if (n == -1){
return -1; // error
}
// data must be here, so do a normal recv()
return recv(s, buf, len, 0);
}
因此,我添加了一段代码来显示 NONBLOCK 是否已设置,但我从未读过“非阻塞事件”,因此在我的代码中,非阻塞未处于事件状态。 如何修改我的代码以启用此功能?
问题是当我从客户端读取字符串并有如下代码时:
char headerstring[512];
memset(headerstring,0,512);
if(this->recvtimeout(client_fd,headerstring,sizeof(headerstring),10) < 0){
close(client_fd);
}
一切工作正常,但是如果洪水泛滥者在事务期间关闭连接,服务器就会崩溃。 我尝试过 try-catch 和其他任何东西......但什么也没有。
最佳答案
将套接字设置为非阻塞的正常方法是
int x;
x=fcntl(s,F_GETFL,0);
fcntl(s,F_SETFL,x | O_NONBLOCK);
在您的代码中,您将使用
获取标志int flags = fcntl(s, F_GETFD);
而你应该这样做
x=fcntl(s,F_GETFL,0);
因此,您的套接字实际上可能启用了非阻塞。
关于c - 如何修改recv来实现IO/非阻塞?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10803079/