linux下:
nt flags = fcntl(sockfd, F_GETFL, 0); fcntl(sockfd, F_SETFL, flags | O_NONBLOCK);
windows下:
unsigned long param = 1; ioctlsocket(sockfd, FIONBIO, (unsigned long *)¶m);
上面 为不同平台下设置非阻塞Socket函数
#pragma once
#include <iostream>
#ifndef WIN32
#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <sys/types.h>
#include <sys/socket.h>
#define SOCKET int
#define INVALID_SOCKET -1
#define _sock_init( )
#define _sock_nonblock( sockfd ) { i