简体   繁体   English

linux child recvfrom无法正常工作

[英]linux child recvfrom not working as it should

I'm making a bunch of child processes and every child must make an arp package with a different IP for every child,send the package and receive a package from the machine with the corresponding IP if that machine is on.The problem is that i send the packages correctly(i check this with wireshark) but I don't know why,all my child processes receive the same package. 我正在做一堆子进程,每个孩子都必须为每个子进程制作一个具有不同IP的arp程序包,发送该程序包,并从具有相应IP的计算机接收程序包(如果该计算机已打开)。问题是我正确发送软件包(我用wireshark进行了检查),但我不知道为什么,我的所有子进程都收到相同的软件包。

For example I have 192.167.0.1,88.4.3.2 and 100.20.3.20.Only the first IP is good.I have 3 child processes that each send a request and wait 8000000 nanoseconds for a response.The only response comes for the child with the first IP(because is is correct) but I don't know why all the children receive this package.Any suggestions ? 例如我有192.167.0.1、88.4.3.2和100.20.3.20。只有第一个IP是好的。我有3个子进程,每个子进程发送一个请求并等待8000000纳秒以获取响应。唯一的响应来自具有第一个IP(因为是正确的),但我不知道为什么所有孩子都会收到此软件包。有什么建议吗? Here is a piece of code.I'm sure that it sends the packages correctly because I tested it with wireshark. 这是一段代码,我确定它正确发送了软件包,因为我是用Wireshark测试的。

    if (sendto(sock,&req,sizeof(req),0,(struct sockaddr*)&addr,sizeof(addr))==-1) {
      printf("%s",strerror(errno));
    }
    struct sockaddr_ll linkLayerAddr;
    char buf[32];
    int sockaddr_len=sizeof(linkLayerAddr);
    memset(&linkLayerAddr,0,sizeof(linkLayerAddr));
    fcntl(sock,F_SETFL,O_NONBLOCK);
    nanosleep(&time1,NULL);

     if(recvfrom(sock,buf,sizeof(buf),0,(struct sockaddr*)&linkLayerAddr,&sockaddr_len)==-1){

    info.status=0; 
    }
    else{
    info.status=1;

} }

Thant thing with the info structure I use just to check something. 我使用的只是信息结构,只是用来检查某些东西。

ARP packets does not contain port number or similar id for describing the destination application. ARP数据包不包含用于描述目标应用程序的端口号或类似ID。 So all received ARP packages are delivered to all ARP sockets. 因此,所有收到的ARP数据包都将传递到所有ARP套接字。

In your case: Threads should only pick wanted packet and ignore others. 在您的情况下:线程应仅选择所需的数据包,而忽略其他数据包。

EDIT: You could loop until the wanted ARP-respone packet is received. 编辑:您可以循环直到收到想要的ARP重发数据包。 This way: 这条路:

while ( !time_out )
{
    <receive next ARP packet>

    if ( arph->ar_tip == wanted_ip )
    {
        // This is correct one. Handle it and break out from the loop.
        <process the package>
        break;
    }
    else
    {
        // This is not for me, ignore it silently.
    }    
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM