我在 C 中有以下代码。
void setNonBlocking(SOCKET fd){
int flags;
if (-1 == (flags = fcntl(fd, F_GETFL, 0)))
flags = 0;
fcntl(fd, F_SETFL, flags | O_NONBLOCK);
}
int main(){
int sock;
connect(sock, .....);
setNonBlocking(sock);
....
close(sock);
//we will do something here but the application exits in/after the close operation
}
我在非阻塞模式下使用带有 setNonBlocking 功能的套接字。当我关闭套接字时,应用程序立即退出而没有段错误或其他任何东西。如果我不使用 setNonBlocking 函数,我看不到这个问题。
如何在没有此问题的情况下关闭非阻塞套接字?