netstat does not show listening port of udp server?

18,118

You may use the following command to specifically show the UDP bound ports:

netstat -n --udp --listen

-n is for numerical representation of the ports, you may omit this

--udp is to show only UDP protocol related information

--listen is to list only ports those have are bound to accept packets/connections

The short command is: netstat -nul

Share:
18,118
MOHAMED
Author by

MOHAMED

Contact me on LinkedIn.

Updated on June 05, 2022

Comments

  • MOHAMED
    MOHAMED almost 2 years

    I have the following udp server:

    /************* UDP SERVER CODE *******************/
    
    #include <stdio.h>
    #include <sys/socket.h>
    #include <netinet/in.h>
    #include <string.h>
    #include <stdlib.h>
    
    int main(){
      int udpSocket, nBytes;
      char buffer[1024];
      struct sockaddr_in serverAddr, clientAddr;
      struct sockaddr_storage serverStorage;
      socklen_t addr_size, client_addr_size;
      int i;
    
      /*Create UDP socket*/
      udpSocket = socket(PF_INET, SOCK_DGRAM, 0);
    
      /*Configure settings in address struct*/
      serverAddr.sin_family = AF_INET;
      serverAddr.sin_port = htons(20001);
      serverAddr.sin_addr.s_addr = inet_addr("127.0.0.1");
      memset(serverAddr.sin_zero, '\0', sizeof serverAddr.sin_zero);  
    
      /*Bind socket with address struct*/
      bind(udpSocket, (struct sockaddr *) &serverAddr, sizeof(serverAddr));
    
      /*Initialize size variable to be used later on*/
      addr_size = sizeof serverStorage;
    
      while(1){
        /* Try to receive any incoming UDP datagram. Address and port of 
          requesting client will be stored on serverStorage variable */
        nBytes = recvfrom(udpSocket,buffer,1024,0,(struct sockaddr *)&serverStorage, &addr_size);
    
        /*Convert message received to uppercase*/
        for(i=0;i<nBytes-1;i++)
          buffer[i] = toupper(buffer[i]);
    
        /*Send uppercase message back to client, using serverStorage as the address*/
        sendto(udpSocket,buffer,nBytes,0,(struct sockaddr *)&serverStorage,addr_size);
      }
    
      return 0;
    }
    

    I compiled and launched the udp server on my linux. and then I tried to see the listening UDP port with hetstat command but I did not see my server port listening:

    sudo netstat -nulp | grep LISTEN
    

    What I m missing?

  • Oshada
    Oshada about 4 years
    in windows netstat -an | find "UDP" | more worked for me