简单的UDP服务器无法接受数据包

时间:2021-06-30 14:01:00

I have a 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 compile it

我编译它

  gcc -m32 -o udp_server udp_server.c   

and run it(./udp_server) on several linux machines, it works fine and I use a udp client client to send packets to the UDP server on these machines, they are accepted

在几台linux机器上运行它(./ udp_server),它工作正常,我使用udp客户端客户端将数据包发送到这些机器上的UDP服务器,它们被接受

but I have a new machine(let me call it A), it is relatively strange compared to other linux machines, as shown in https://superuser.com/questions/581442/ifconfig-command-not-found anyway it has no "eth0" and the interfaces are:

但是我有一台新机器(让我称之为A),与其他linux机器相比它相对比较奇怪,如https://superuser.com/questions/581442/ifconfig-command-not-found所示,无论如何它没有“eth0”和接口是:

    [root@kitch proxy]# ip link show
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN mode DEFAULT
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
2: em1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP mode DEFAULT qlen 1000
    link/ether 00:1a:a0:23:86:6c brd ff:ff:ff:ff:ff:ff

First, I run the 32-bit-version of the program on it and I got

首先,我运行32位版本的程序,我得到了

-bash: ./udp_server: /lib/ld-linux.so.2: bad ELF interpreter: No such file or directory

Then, second, I compile the UDP program without '-m32' and run it on the machine A, it runs normally Then I use UDP client to send packets to the UDP server on A I can capture the sent packets on this machine, but the UDP server doesn't accept those packets,

然后,第二,我编译没有'-m32'的UDP程序并在机器A上运行它,它正常运行然后我使用UDP客户端将数据包发送到UDP服务器上AI可以捕获这台机器上发送的数据包,但是UDP服务器不接受这些数据包,

are there any potential reasons for this? maybe the binding doesn't work here because this machine is special? thanks!

这有什么潜在的原因吗?也许绑定在这里不起作用,因为这台机器很特别?谢谢!

1 个解决方案

#1


0  

Some recent Linux distros (e.g. Fedora) have changed the name of the interface from ethX to emX, so, nothing wrong with it.

一些最近的Linux发行版(例如Fedora)已经将接口的名称从ethX更改为emX,因此,没有任何问题。

You got problem with that machine because the server is not running at all as this line claims:

你遇到了该机器的问题,因为该线路声称服务器根本没有运行:

-bash: ./udp_server: /lib/ld-linux.so.2: bad ELF interpreter: No such file or directory

I guess you've compiled the program in a 32bit mode ( look at the -m32 gcc parameter ) in a 64bit machine with no 32bit library support installed.

我猜你已经在一个没有安装32位库支持的64位机器上以32位模式编译程序(查看-m32 gcc参数)。

Recompile it without -m32 options.

不使用-m32选项重新编译它。

#1


0  

Some recent Linux distros (e.g. Fedora) have changed the name of the interface from ethX to emX, so, nothing wrong with it.

一些最近的Linux发行版(例如Fedora)已经将接口的名称从ethX更改为emX,因此,没有任何问题。

You got problem with that machine because the server is not running at all as this line claims:

你遇到了该机器的问题,因为该线路声称服务器根本没有运行:

-bash: ./udp_server: /lib/ld-linux.so.2: bad ELF interpreter: No such file or directory

I guess you've compiled the program in a 32bit mode ( look at the -m32 gcc parameter ) in a 64bit machine with no 32bit library support installed.

我猜你已经在一个没有安装32位库支持的64位机器上以32位模式编译程序(查看-m32 gcc参数)。

Recompile it without -m32 options.

不使用-m32选项重新编译它。