1、阻塞/非阻塞

/*
函数名 :int fcntl(int fd, int cmd); 
参  数 :int fd    -- 文件描述符
参  数 :int cmd   -- 控制值
参  数 :flock *lock  -- (参数)
返回值 :【成功】返回依赖于cmd的值 【错误】返回-1,错误原因存于errno.
说  明 :fcntl针对文件描述符提供控制。
*/
int fcntl(int fd, int cmd); 
int fcntl(int fd, int cmd, long arg); 
int fcntl(int fd, int cmd, struct flock *lock);

头文件
#include <sys/types.h>
#include <unistd.h>
#include <fcntl.h>

/*获取 flag 值*/
int flags = fcntl(socket, F_GETFL, 0);
/* 设置为非阻塞*/
fcntl(socket, F_SETFL, flags | O_NONBLOCK)
/* 设置为阻塞 */
fcntl(socket, F_SETFL, 0)

/*非阻塞模式的消息发送*/
recv(sockfd, buff, buff_size,MSG_DONTWAIT);
/*非阻塞模式的消息接受*/
send(scokfd, buff, buff_size, MSG_DONTWAIT);

猜你喜欢

转载自www.cnblogs.com/osbreak/p/9961101.html