【问题标题】:bad file descriptor in raw socket原始套接字中的错误文件描述符
【发布时间】:2013-01-10 14:40:54
【问题描述】:

我开始使用原始套接字,我遇到了这个问题:

错误的文件描述符

代码是:

#include<stdio.h>
#include<sys/socket.h>
#include <netinet/in.h>
#include <errno.h>
#include <sys/ioctl.h>
#include <net/if.h>
#include <unistd.h>
int main(int argc, char **argv)
{
int fd; //file descriptor raw socket

int ioInt;

struct ifreq req; //struttura per la chiamata di ioctl

fd=socket(PF_INET, SOCK_RAW, IPPROTO_RAW);

if(fd==-1) {



if(errno==EPROTONOSUPPORT)
    perror("socket o protocollo non supportato dal dominio");
if(errno==EACCES)
    perror("mancano i privilegi per creare il socket");
if(errno==EINVAL)
    perror("protocollo sconosciuto o dominio non disponibile");
if(errno==ENOBUFS || errno==ENOMEM)
    perror("memoria non sufficente per la creazione del socket");

}

 strncpy (req.ifr_name, "eth0", sizeof(req.ifr_name) - 1);

 ioInt=ioctl(fd, SIOCGIFINDEX, &req);

 if(ioInt==-1) 
    perror("SIOCGIFINDEX");

return 0;   

}

我必须访问 eth0 并希望 send e 在其中接收数据包。我使用 linux mint 12

【问题讨论】:

    标签: c sockets file-descriptor


    【解决方案1】:

    当 fd==-1 且 errno 不是您要查找的值之一时,您无法打印错误。

    我运行了您的代码(作为非特权用户),fd 以 -1 的形式返回,错误号 == EPERM

    您很可能没有以 root 身份运行。您必须以 root 身份运行才能获得原始套接字。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-01-28
      • 2021-12-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-10-30
      • 2014-04-25
      • 1970-01-01
      相关资源
      最近更新 更多