简体   繁体   English

OpenWRT C程序未接收到数据包

[英]OpenWRT C program not receiving packets

I compiled a simple C UDP server and client for the OpenWrt router. 我为OpenWrt路由器编译了一个简单的C UDP服务器和客户端。 The codes work as expected when both are on my pc. 当两个都在我的电脑上时,代码按预期工作。 The client, if on the router, seems to successfully send the packets (port 6115) since my pc acknowledges them if I send them to the pc (with the same server code). 如果在路由器上,客户端似乎成功发送了数据包(端口6115),因为如果我将数据包发送到计算机(使用相同的服务器代码),则我的计算机会确认它们。 But the server does not receive them if on the router, whether the client is run on the router (using local loopback) or on the pc. 但是,无论客户端是在路由器上运行(使用本地环回)还是在PC上,服务器都不会在路由器上接收到它们。

On the router, no other programs were using port 6115. 在路由器上,没有其他程序使用端口6115。

I've checked the firewall configuration in /etc/config/firewall and it seems to allow packets from port 6115: 我已经检查了/etc/config/firewall的防火墙配置,它似乎允许来自端口6115的数据包:

config rule

  option input 'ACCEPT'

  option output 'ACCEPT'

  option forward 'ACCEPT'

  option target 'ACCEPT'

  option proto 'tcp udp'

  option src_port '6115'

  option dest_port '6115'

  option name 'Allow-myudp'

  option src '*'

  option family 'ipv4'

I've tried to disable the firewall but nothing changes. 我试图禁用防火墙,但是没有任何变化。

Question: Can the firewall even interfere if I send the packets through local loopback (127.0.0.1) or should I be trying something other than messing with the firewall? 问:如果我通过本地环回(127.0.0.1)发送数据包,防火墙甚至会干扰,还是我应该尝试除弄乱防火墙以外的其他方法?

I've heard the problem can be caused because my router device is big-endian, if that can cause the problem what can I do about it? 我听说问题可能是由于我的路由器设备是高位优先端引起的,如果这可能会导致问题,我该怎么办?

If relevant, here are the client and server codes (for local loopback): 如果相关,以下是客户端和服务器代码(用于本地环回):

Server: 服务器:

int udpSocket, ndat;
struct sockaddr_in serverAddr;
struct sockaddr_storage serverStorage;
socklen_t addr_size;
char buf[1024];

udpSocket=socket(PF_INET,SOCK_DGRAM,0);

serverAddr.sin_family=AF_INET;
serverAddr.sin_port=htons(6115);
memset(serverAddr.sin_zero,'\0',sizeof serverAddr.sin_zero);

bind(udpSocket,(struct sockaddr*)&serverAddr,sizeof(serverAddr));

addr_size=sizeof serverStorage;

while (1) {

    ndat=recvfrom(udpSocket,buf,1024,0,(struct sockaddr*)&serverStorage,&addr_size);
    printf("DATA RECEIVED WITH %u BYTES\n",ndat);

}

Client: 客户:

int udpSocket;
char buffer[1024]="Hello [home]";
struct sockaddr_in serverAddr;
socklen_t addr_size=sizeof serverAddr;

udpSocket=socket(PF_INET,SOCK_DGRAM,0);

serverAddr.sin_family=AF_INET;
serverAddr.sin_port=htons(6115);
serverAddr.sin_addr.s_addr=inet_addr("127.0.0.1");
memset(serverAddr.sin_zero,'\0',sizeof serverAddr.sin_zero);

bind(udpSocket,(struct sockaddr*)&serverAddr,sizeof(serverAddr));

sendto(udpSocket,buffer,1024,0,(struct sockaddr*)&serverAddr,addr_size);

printf("Sent...\n");

Please give me suggestions on how to solve the problem if you have any. 如果有的话,请给我有关如何解决问题的建议。 Thanks in advance. 提前致谢。

My bad, I had ignored the server code because for some reason it was working on the PC. 不好的是,我忽略了服务器代码,因为由于某种原因它在PC上正常工作。 Anyway, I had to add serverAddr.sin_addr.s_addr=htonl(INADDR_ANY); 无论如何,我必须添加serverAddr.sin_addr.s_addr = htonl(INADDR_ANY); before the call to bind(); 在调用bind()之前; .

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

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