我试图实现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)