分别在linux和windows上设置socket为阻塞模式

在 Linux 和 Windows 系统中,都可以将 socket 设置为非阻塞模式。

Linux平台

  • 在 Linux 系统中,可以使用 fcntl 函数来设置 socket 为非阻塞模式。例如:
int flags = fcntl(socket_fd, F_GETFL, 0);
fcntl(socket_fd, F_SETFL, flags | O_NONBLOCK);
  • 此外,Linux 还提供了一个 accept4 函数,可以直接将返回的 socket 设置为非阻塞模式:
int client_fd = accept4(server_fd, (struct sockaddr *)&client_addr, &client_addr_len, SOCK_NONBLOCK);
  • 设置阻塞模式,默认阻塞模式不用单独设置
int flags = fcntl(socket_fd, F_GETFL, 0);
fcntl(socket_fd, F_SETFL, flags | ~O_NONBLOCK);

Windows平台

  • 在 Windows 系统中,可以使用 ioctlsocket 函数来设置 socket 为非阻塞模式。例如:
u_long mode = 1;
ioctlsocket(socket_fd, FIONBIO, &mode);
  • 设置阻塞模式
u_long mode = 0;
ioctlsocket(socket_fd, FIONBIO, &mode);

封窗两个函数方便调用

设置非阻塞模式
void SetNonBlocking(SOCKET sockfd)
设置阻塞模式
void SetBlocking(SOCKET sockfd)

#ifdef __GNUC__
#define SOCKET int
#elif defined(_WIN32)
#endif


void SetNonBlocking(SOCKET sockfd){
#ifdef _WIN32
    unsigned long flag = 1;
    if(ioctlsocket(sockfd, FIONBIO, &flag) == SOCKET_ERROR){
#else
    int cflags = fcntl(sockfd, F_GETFL, 0); 
    if(fcntl(sockfd, F_SETFL, cflags | O_NONBLOCK) == -1){
#endif
        std::cerr << "ioctlsocket or fcntl set non blocking error" << std::endl;
        exit(-1);
    }
}

void SetBlocking(SOCKET fd){
#ifdef _WIN32
    unsigned long flag = 0;
    if(ioctlsocket(sockfd, FIONBIO, &flag) == SOCKET_ERROR){
#else
    int cflags = fcntl(sockfd, F_GETFL, 0); 
    if(fcntl(sockfd, F_SETFL, cflags & ~O_NONBLOCK) == -1){
#endif
        std::cerr << "ioctlsocket or fcntl set non blocking error" << std::endl;
        exit(-1);
    }
}

猜你喜欢

转载自blog.csdn.net/CHNIM/article/details/132302720