Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/64.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
作为输出的未定义端口号-UDP/C/Linux_C_Linux_Network Programming_Udp - Fatal编程技术网

作为输出的未定义端口号-UDP/C/Linux

作为输出的未定义端口号-UDP/C/Linux,c,linux,network-programming,udp,C,Linux,Network Programming,Udp,我已经在客户端和服务器程序中定义了端口号。我启动简单的udp服务器程序,它从客户端接收数据包。服务器获取数据包,但当我打印客户端信息时,端口号被称为随机数(51958) 如何获得正确的端口号。i、 e.我定义的数字 #define PORT XYZ ... if((s = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) == -1) diep("socket"); memset((char *) &am

我已经在客户端和服务器程序中定义了端口号。我启动简单的udp服务器程序,它从客户端接收数据包。服务器获取数据包,但当我打印客户端信息时,端口号被称为随机数(51958) 如何获得正确的端口号。i、 e.我定义的数字

  #define PORT XYZ
       ...
     if((s = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) == -1)
        diep("socket");

      memset((char *) &si_me, 0, sizeof(si_me));

      si_me.sin_family = AF_INET;
      si_me.sin_port = htons(PORT);
      si_me.sin_addr.s_addr = htonl(INADDR_ANY);

      if(bind(s, (struct sockaddr *) &si_me, sizeof(si_me)) == -1)
        diep("bind");

      for(i = 0; i < NPACK; i += 1) {
        if(recvfrom(s, buf, BUFLEN, 0, (struct sockaddr *) &si_other, &slen) == -1)
          diep("recvfrom()");

        printf("Recieved packet from %s: %d\nData: %s\n\n", inet_ntoa(si_other.sin_addr), ntohs(si_other.sin_port), buf);
      }
      close(s);
#定义端口XYZ
...
if((s=套接字(AF_INET,SOCK_DGRAM,IPPROTO_UDP))=-1)
diep(“插座”);
memset((char*)&si_-me,0,sizeof(si_-me));
si_me.sin_family=AF_INET;
si_me.sin_port=htons(port);
si_me.sin_addr.s_addr=htonl(在任何情况下);
if(bind(s,(struct sockaddr*)&si_me,sizeof(si_me))=-1)
diep(“绑定”);
对于(i=0;i
///客户

   #define PORT XYZ

   if((s = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) == -1)
                diep("socket");

        memset((char *) &si_other, 0, sizeof(si_other));

        si_other.sin_family = AF_INET;
        si_other.sin_port = htons(PORT);
        if(inet_aton(SRV_IP, &si_other.sin_addr) == 0) {
                fprintf(stderr, "inet_aton() failed\n");
                exit(1);
        }

        {
                for(i = 0; i < NPACK; i += 1) {
                        printf("Sending packet %d\n", index);
                        sprintf(buf, "This is packet%d\n", index);
                     ;
                        if(sendto(s, buf, BUFLEN, 0, (struct sockaddr *) &si_other, slen) == -1)
                                diep("sendto()");
                        index++;
                }

        }
        close(s);
#定义端口XYZ
if((s=套接字(AF_INET,SOCK_DGRAM,IPPROTO_UDP))=-1)
diep(“插座”);
memset((char*)&si_other,0,sizeof(si_other));
si_other.sin_family=AF_INET;
si_other.sin_port=htons(port);
if(inet_aton(SRV_IP,&si_other.sin_addr)==0){
fprintf(stderr,“inet_aton()失败\n”);
出口(1);
}
{
对于(i=0;i
更新 如果我们在N个套接字上发送数据,并且在服务器端,我们在一个while(1)循环中接收数据,那么我们如何识别客户端发送的端口?

我认为它实际上是正确的端口号,因为您正在打印客户端的源端口(如果没有其他指定,客户端主机将使用随机空闲端口)而不是服务器正在侦听的目标端口

如果您有多个套接字,则可以获取绑定到的端口


您没有将客户端套接字绑定到特定端口,因此它会获得系统分配的端口。

我认为您还需要发布客户端代码。我已经在客户端和服务器si_me.sinu port=htons(端口)中指定了端口号;在客户机代码中,您设置的是消息的目标端口,而不是源端口。我的印象是recvfrom()的第五个参数获取客户机详细信息。它确实如此!它包含所接收数据包的源ip和端口。不需要指定目标端口,因为它绑定到您刚刚从中读取数据的套接字(打印ntohs(如果需要打印,请打印si_me.sinu port))如果我从多个套接字接收数据,如何识别我接收数据的端口。
if (getsockname(sock, (struct sockaddr *)&sin, &len) == -1)
    perror("getsockname");
else
    printf("port number of the listening socket %d\n", ntohs(sin.sin_port));