简体   繁体   English

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

[英]Undefined port number as output - UDP/C/Linux

I have defined the port number in both client as well as server program. 我已经在客户端和服务器程序中定义了端口号。 I start the simple udp server program which receives packet from client. 我启动简单的udp服务器程序,该程序从客户端接收数据包。 The server gets the packet But when i print the client information, Port number is know as Random number (51958) How to get the correct port number . 服务器获取数据包,但是当我打印客户信息时,端口号称为随机数(51958)如何获取正确的端口号。 ie the number i have defined. 即我定义的数字。

  #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);

/// client ///客户端

   #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);

Update If we have data sent on N number of sockets and On the server side we are in a while(1) loop receiving the data , How do we identify the port the client has sent ? 更新如果我们在N个套接字上发送了数据,并且在服务器端,我们处于while(1)循环中接收数据,那么如何识别客户端已发送的端口?

I think it actually is the correct port number, since you're printing the source port of the client (the client host uses a random free one if not else specified) and not the destination port which is the one the server is listening too 我认为这实际上是正确的端口号,因为您正在打印客户端的源端口(如果未指定,则客户端主机使用随机的空闲端口),而不是服务器也在监听的目标端口。

If you have multiple sockets you can get the port which is bound to with getsockname 如果您有多个套接字,则可以获取与getsockname绑定的端口

if (getsockname(sock, (struct sockaddr *)&sin, &len) == -1)
    perror("getsockname");
else
    printf("port number of the listening socket %d\n", ntohs(sin.sin_port));

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

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

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