c sendto函数设置linux2.6.29中的“网络不可到达”errno

时间:2023-01-02 12:23:56

I have a problem similar to that in sendto function setting "network is unreachable" errno and (less like) UDP Broadcast sendto failed:“network is unreachable” on linux 2.6.30 but as this problems are not answered and are pretty old i tried restating them here with more clarifications in hope for an answer.

我有一个问题类似于sendto函数设置“网络访问”errno,(不像)UDP广播sendto失败:“网络是遥不可及的“linux上来自2.6.30但不回答这个问题是非常旧的我试着重申他们更多的澄清在希望的答案。

I have a UDP server and client where clients broadcasts alive messages and server catches them (code is as follows). When i run these on several different desktop linux like ubuntu14.04, 16.04, fedora6 and... they work fine. But when i try running them from an ltib made embedded linux on an mpc8308 board, the server works(recieving messages from other machines) but client sets errno to "network is unreachable" as it calls sendto.
I tried wireshark and there is no packet on board interfaces from the client and i tested the system with tcpdump on board and there is no generated packet there.

我有一个UDP服务器和客户机,客户机在其中广播活动消息,服务器捕获它们(代码如下)。当我在几个不同的桌面linux上运行它们时,比如ubuntu14.04、16.04、fedora6和……他们的工作很好。但是,当我尝试在mpc8308板上运行ltib制造的嵌入式linux时,服务器可以工作(从其他机器接收消息),但是客户端将errno设置为它调用sendto时“网络是不可访问的”。我尝试了wireshark,客户端的接口上没有包,我在系统上测试了tcpdump,那里没有生成的包。

Here is the code for client:

这是客户的代码:

  int clientSocket, portNum, nBytes;
  char buffer[1024];
  struct sockaddr_in serverAddr;
  socklen_t addr_size;

  /*Create UDP socket*/
  clientSocket = socket(AF_INET, SOCK_DGRAM, 0);
  int broadcastEnable=1;
  int retsockopts=setsockopt(clientSocket, SOL_SOCKET, SO_BROADCAST, &broadcastEnable, sizeof(broadcastEnable));
  /*Configure settings in address struct*/
  serverAddr.sin_family = AF_INET;
  serverAddr.sin_port = htons(12021);
  serverAddr.sin_addr.s_addr = htonl(INADDR_BROADCAST);
  memset(serverAddr.sin_zero, '\0', sizeof serverAddr.sin_zero);  

  /*Initialize size variable to be used later on*/
  addr_size = sizeof serverAddr;

  while(1){
    sprintf(buffer,"I AM ALIVE");

    nBytes = strlen(buffer) + 1;

    int err;
    err = sendto(clientSocket,buffer,nBytes,0,(struct sockaddr *)&serverAddr,addr_size);
    if(err==-1) //error occured
    {
        printf("error sending alive message: %s\n",strerror(errno));
    }

    sleep(5);
  }

EDIT:
I checked sending broadcast messages to subnet broadcast ip address and it worked. But global broadcast address is unreachable... I think the problem is that os cannot identify which network interface it should use for sending this packet but i don't know how to fix it. EDIT2:
I added some options to my code so that the socket binds to an specific interface and the problem i mentioned above can be solved and this worked:

编辑:我检查发送广播消息到子网广播ip地址,它工作。但是全球广播地址是无法到达的……我认为问题是操作系统不能识别它应该使用哪个网络接口来发送这个数据包,但是我不知道如何修复它。EDIT2:我在代码中添加了一些选项,以便套接字绑定到一个特定的接口,我上面提到的问题可以得到解决,这是可行的:

int rc = setsockopts(clientSocket, SOL_SOCKET, SO_BINDTODEVICE, "eth0", 4);

I borrowed this code snippet from Bind UDP listening socket to specific interface (or find out the interface a datagram came in from).

我从Bind UDP监听套接字(或查找数据报来自的接口)中借用了这段代码片段。

1 个解决方案

#1


3  

Your code is fine. The problem is in your network administration.

你的代码很好。问题在于您的网络管理。

You are missing a default route. The default route might be provided by your DHCP server, you might have to specify it in your network config files, or you might have to issue a command like route add default gw 192.0.2.1.

您丢失了一个默认路由。默认路由可能由您的DHCP服务器提供,您可能需要在您的网络配置文件中指定它,或者您可能需要发出一个命令,如route add default gw 192.0.2.1。

#1


3  

Your code is fine. The problem is in your network administration.

你的代码很好。问题在于您的网络管理。

You are missing a default route. The default route might be provided by your DHCP server, you might have to specify it in your network config files, or you might have to issue a command like route add default gw 192.0.2.1.

您丢失了一个默认路由。默认路由可能由您的DHCP服务器提供,您可能需要在您的网络配置文件中指定它,或者您可能需要发出一个命令,如route add default gw 192.0.2.1。