小编use*_*711的帖子

recv与非阻塞套接字

我试图实现socket的非阻塞,recv问题是当没有数据但我希望得到EAGAIN错误时我得到了错误-1 .

套接字肯定设置为非阻塞状态,我检查flags = fcntl(s, F_GETFL, 0)O_NONBLOCK标志.

非常感谢提前!

#include <arpa/inet.h>
#include <linux/if_packet.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <sys/ioctl.h>
#include <sys/socket.h>
#include <net/if.h>
#include <netinet/ether.h>
#include <unistd.h>
#include <fcntl.h>
#include <asm-generic/errno-base.h>
#include <assert.h>

#define ETH_FRAME_LEN_MY 1400

void print(void *buf, int length)
{
    int i;
        for (i = 0; i < length; ++i)
            putchar(((char *)buf)[i]);
    printf("\n");
}

int main(){

    int flags, s, r, err; 

    struct sockaddr_ll socket_addr;
        char ifName[IFNAMSIZ] = "eth0";

        struct …
Run Code Online (Sandbox Code Playgroud)

c sockets linux nonblocking recv

3
推荐指数
2
解决办法
2万
查看次数

标签 统计

c ×1

linux ×1

nonblocking ×1

recv ×1

sockets ×1