简体   繁体   English

netstat不显示udp服务器的侦听端口?

[英]netstat does not show listening port of udp server?

I have the following udp server: 我有以下udp服务器:

/************* 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. 我在Linux上编译并启动了udp服务器。 and then I tried to see the listening UDP port with hetstat command but I did not see my server port listening: 然后我尝试使用hetstat命令查看侦听的UDP端口,但没有看到服务器侦听的端口:

sudo netstat -nulp | grep LISTEN

What I m missing? 我想念什么?

You may use the following command to specifically show the UDP bound ports: 您可以使用以下命令专门显示UDP绑定端口:

netstat -n --udp --listen

-n is for numerical representation of the ports, you may omit this -n用于端口的数字表示,您可以省略

--udp is to show only UDP protocol related information --udp仅显示UDP协议相关信息

--listen is to list only ports those have are bound to accept packets/connections --listen仅列出那些已经绑定接受数据包/连接的端口

The short command is: netstat -nul 简短的命令是: netstat -nul

暂无
暂无

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

相关问题 套接字绑定在一个端口上,但诸如netstat和lsof之类的工具显示它在其他端口上侦听 - Socket bound on one port, but tools like netstat and lsof show it listening on some other port 使用netstat -lntp检测侦听端口的进程是否关闭时,netstat是否异常? - An exception of netstat when using netstat -lntp to detect a process listening on a port is down or not? 尽管服务器正在侦听端口,为什么主机总是响应“ RST”? - why does the host always response `RST` though the server is listening on the port? 为什么netstat显示错误的外部地址? - Why does netstat show the wrong foreign address? 我有一个在 Debian 10 中运行的服务器套接字程序,但 netstat 没有显示它 - I have a server socket program runnig in Debian 10, but netstat does not show it UDP:为两个不同的多播流侦听同一端口 - UDP: Listening to the same port for two different multicast streams 如何阻止客户端在服务器上监听或客户端正在监听的端口 - How to block client listening on a server or the port the client is listening to UDP单播:两个进程侦听同一个udp端口; 只有一个接收数据包 - UDP unicast: two processes listening on same udp port; only one receiving packets 无法与服务器正在侦听的端口通信 - Unable to communicate to a port in which server is listening 获取服务器正在侦听的端口号? - Obtain port number to which a server is listening to?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM