0

I am trying to learn about socket programming, and I have the following function to set up a socket:

  29 int CreatePassiveSock(char *protocol, char *portstr, int qlen) {                         
  30   int s, port, type, saddrlen;                                                           
  31   char *endptr;                                                                          
  32   struct sockaddr_in saddr;                                                              
  33   port = (int) strtol(portstr, &endptr, 10);                                             
  34   if (*endptr) {                                                                         
  35     printf("\nPlease specify a positive integer for port\n");                            
  36     exit(1);                                                                             
  37   }                                                                                      
  38   saddrlen = sizeof(saddr);                                                              
  39   memset(&saddr, 0, saddrlen);                                                           
  40   saddr.sin_family = AF_INET;                                                            
  41   saddr.sin_addr.s_addr = INADDR_ANY;                                                    
  42   saddr.sin_port = htons(port);                                                          
  43   if (strcmp("tcp", protocol) == 0)                                                      
  44     type = SOCK_STREAM;                                                                  
  45   else if (strcmp("udp", protocol) == 0)                                                 
  46     type = SOCK_DGRAM;                                                                   
  47   else {                                                                                 
  48     printf("Unsupported protocol given");                                                
  49     exit(1);                                                                             
  50   }                                                                                      
  51   if ((s = socket(PF_INET, type, 0)) == -1){                                             
  52     perror("socket call failed");                                                        
  53     exit(1);                                                                             
  54   }                                                                                      
  55   if (bind(s, (struct sockaddr *)&saddr, saddrlen) == -1) {                              
  56     perror("Bind failed");                                                               
  57     exit(1);                                                                             
  58   }                                                                                      
  59   if (type == SOCK_STREAM) {                                                             
  60     if (listen(s, qlen) == -1) {                                                         
  61       perror("listen failed");                                                           
  62       exit(1);                                                                           
  63     }                                                                                    
  64   }                                                                                      
  65   return s;                                                                              
  66 }          

When I call this with 6001 as port number, this is the line that shows up with netstat -a | head

tcp        0      0 0.0.0.0:x11-1           0.0.0.0:*               LISTEN 

Why would it show up as 0.0.0.0:x11-1? Shouldn't I get 0.0.0.0:6001?

If it matters I am doing this on a laptop running PopOS.

1 Answer 1

2

TCP port 6001 is identified as x11-1 in /etc/services, so netstat shows it using that label: in a similar fashion to the way it looks up host names corresponding to the IP addresses it finds, it looks up service names corresponding to the port numbers it finds. See man 5 services for a description of /etc/services and the functions used to access it, How do X clients know that they will need to connect to TCP port 6000+<display number>? for an explanation of 6000+ range port number mapping.

You can examine /etc/services to see which services are known on your system; the official registry is maintained by the IANA, and you can find the X11 assignments there too.

You can force netstat to show you the port number using -n.

2
  • Do you know where I could read about which ports might have string identifiers? As in, how does netstat know to display x11-1 instead of just 6001? Is there an authoritative document that says it should do this or netstat just does this because x11 is common and doing so is convenient? Thank you for your answer! Commented Aug 17, 2023 at 20:51
  • 1
    netstat knows to display x11-1 because /etc/services tells it that TCP port 6001 maps to that service. If you look at that file on your system you’ll see what names are known. See the link above or man 5 services locally for details. Commented Aug 17, 2023 at 22:30

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.