如何将UDP的recvfrom函数设置为非阻塞或阻塞超时

本文先介绍我查看了的2篇文章,然后介绍linux 和windows 下的非阻塞设置。最后是阻塞超时的设置。

博文一:其实UDP的非阻塞和阻塞超时可以理解成和TCP是一样的,都是通过socket的属性去做。

方法一:通过fcntl函数将套接字设置为非阻塞模式。

方法二:通过套接字选项SO_RECVTIMEO设置阻塞超时。

只是linux 下的情况

论坛二:有关recvfrom超时设置的问题。

“非阻塞”和“超时”是矛盾的。非阻塞式套接字表示如果没有数据就立即返回,不会有等待超时的机制。

如果使用了“超时”,应该是“阻塞+超时”模式。

一、非阻塞设置

1.在linux 下用fcntl 函数

//设置非阻塞
static void setnonblocking(int sockfd) {
    int flag = fcntl(sockfd, F_GETFL, 0);  //取标志
    if (flag < 0) {
        Perror("fcntl F_GETFL fail");
        return;
    }
    if (fcntl(sockfd, F_SETFL, flag | O_NONBLOCK) < 0) {  //设置标志
        Perror("fcntl F_SETFL fail");
    }
}

2.在windows 下用ioctlsocket

//设置非阻塞
static void setnonblocking(int sockfd) {
    unsigned long on = 1;  //此值为0,会将套接字设置为阻塞(默认的);为1设置为非阻塞
    if (0 != ioctlsocket(sockfd, FIONBIO, &on))
    {
        /* Handle failure. */
    }
}

二、阻塞超时设置

linux下的:

#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <errno.h>
#include <strings.h>
#include <stdlib.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/time.h>
 
void Perror(const char *s)
{
    perror(s);
    exit(EXIT_FAILURE);
}
 
int main()
{
    int sockfd;
    int port = 9527;
    struct sockaddr_in servaddr, cliaddr;
 
    sockfd = socket(AF_INET, SOCK_DGRAM, 0);
    if (sockfd == -1) {
        Perror("socket failed:");
    }
 
    // 设置超时
    struct timeval timeout;
    timeout.tv_sec = 1;//秒
    timeout.tv_usec = 0;//微秒
    if (setsockopt(sockfd, SOL_SOCKET, SO_RCVTIMEO, &timeout, sizeof(timeout)) == -1) {
        Perror("setsockopt failed:");
    }
 
    bzero(&servaddr, sizeof(servaddr));
    servaddr.sin_family = AF_INET;
    servaddr.sin_addr.s_addr = inet_addr("127.0.0.1");
    servaddr.sin_port = htons(port);
 
    if (bind(sockfd, (sockaddr *)&servaddr, sizeof(servaddr)) == -1) {
        Perror("bind failed:");
    }
    
    socklen_t len;
    for ( ; ; ) {
        char mesg[1024] = {};
        int n = recvfrom(sockfd, mesg, 1024, 0, (sockaddr *)&cliaddr, &len);
        perror("recvfrom fail: ");
 
    }
 
    return 0;
}
经验分享 程序员 微信小程序 职场和发展