UDP服务器进程无法接受来自内核的数据包

时间:2021-07-26 14:02:13

I have a very simple UDP server program

我有一个非常简单的UDP服务器程序

    #include <sys/socket.h>
    #include <netinet/in.h>
    #include <stdio.h>
    #include <string.h>


    int main(int argc, char**argv)
    {
       int sockfd,n;
       struct sockaddr_in servaddr,cliaddr;
       socklen_t len;
       char mesg[1000];

       sockfd=socket(AF_INET,SOCK_DGRAM,0);

       bzero(&servaddr,sizeof(servaddr));
       servaddr.sin_family = AF_INET;
       servaddr.sin_addr.s_addr=htonl(INADDR_ANY);
       servaddr.sin_port=htons(54000);
       bind(sockfd,(struct sockaddr *)&servaddr,sizeof(servaddr));

       for (;;)
       {
          len = sizeof(cliaddr);
          n = recvfrom(sockfd,mesg,1000,0,(struct sockaddr *)&cliaddr,&len);
          sendto(sockfd,mesg,n,0,(struct sockaddr *)&cliaddr,sizeof(cliaddr));
          printf("-------------------------------------------------------\n");
          mesg[n] = 0;
          printf("Received the following:\n");
          printf("%s",mesg);
          printf("-------------------------------------------------------\n");
       }
    }

I put it on several machines and let a udp client to send packets to it it can accept incoming udp packets successfully

我把它放在几台机器上,让一个udp客户端向它发送数据包,它可以成功接受传入的udp数据包

then I place it in a machine with fedora 18 I compile the program and run it and then I let a udp client to send packets to it(the same as on the other machines) but the program can't accept incoming UDP packets I used tcpdump for capturing and I can see the incoming udp packets why the server program doesn't accept the incoming UDP packet on this machine?

然后我把它放在一台装有fedora 18的机器上我编译程序并运行它然后我让一个udp客户端向它发送数据包(与其他机器上相同)但程序不能接受我使用的传入UDP数据包tcpdump用于捕获,我可以看到传入的udp数据包为什么服务器程序不接受这台机器上的传入UDP数据包?

I checked the iptables rule iptables -L and the results are in

我检查了iptables规则iptables -L,结果是在

https://docs.google.com/file/d/0B09y_TWqTtwlNHp1eTJkTFNuY0k/edit?usp=sharing

are there potential reasons for this? thanks!

有这个潜在的原因吗?谢谢!

1 个解决方案

#1


1  

The code looks OK at first glance.

乍一看,代码看起来还不错。

The most obvious explanation might simply be that the Fedora 18 machine has been installed with iptables firewalling configured by default...

最明显的解释可能只是Fedora 18机器安装了默认配置的iptables防火墙......

Try running lsmod to look for loaded iptables modules, and/or iptables -L to list the current ruleset.

尝试运行lsmod来查找加载的iptables模块,和/或iptables -L列出当前的规则集。

#1


1  

The code looks OK at first glance.

乍一看,代码看起来还不错。

The most obvious explanation might simply be that the Fedora 18 machine has been installed with iptables firewalling configured by default...

最明显的解释可能只是Fedora 18机器安装了默认配置的iptables防火墙......

Try running lsmod to look for loaded iptables modules, and/or iptables -L to list the current ruleset.

尝试运行lsmod来查找加载的iptables模块,和/或iptables -L列出当前的规则集。