The linux TCP client specifies the port number to connect to the Server Linux. There seems to be no command to directly enable or disable the port, because if you only enable the port and do not associate it with the process, enabling and disabling a port makes no sense (when a port is opened, no program processes the incoming data ). In other words, the port activity in Linux is closely linked to the process. If you want to disable a port, you only need to kill the corresponding process. Client code # include <stdio. h> # include <stdlib. h> # include <strings. h> # include <arpa/inet. h> # include <sys/socket. h> int main () {int sockfd = socket (AF_INET, SOCK_STREAM, 0); if (sockfd =-1) perror ("sock"); struct sockaddr_in mine, dest; bzero (& mine, sizeof (mine); mine. sin_family = AF_INET; mine. sin_port = htons (3334); inet_ton (AF_INET, "192.168.1.100", & mine. sin_addr); bzero (& dest, sizeof (dest); d Est. sin_family = AF_INET; dest. sin_port = htons (9999); inet_ton (AF_INET, "192.168.1.100", & dest. sin_addr); int B = bind (sockfd, (struct sockaddr *) & mine, sizeof (mine); if (B =-1) perror (""); int c = connect (sockfd, (struct sockaddr *) & dest, sizeof (dest); if (c! = 0) perror (""); close (sockfd);} server code # include <stdio. h> # include <stdlib. h> # include <sys/socket. h> # include <arpa/inet. h> # include <strings. h> int main () {int sockfd = socket (AF_INET, SOCK_STREAM, 0); struct sockaddr_in addr; bzero (& addr, sizeof (addr); addr. sin_family = AF_INET; addr. sin_port = htons (9999); inet_ton (AF_INET, "192.168.1.100", & addr. sin_addr); bind (sockfd, (struct sockaddr *) & addr, sizeof (addr); listen (sockfd, 5); while (1) {struct sockaddr_in c_addr; int c_len = sizeof (c_addr); bzero (& c_addr, c_len); int c = accept (sockfd, (struct sockaddr *) & addr, & c_len ); if (c =-1) perror (""); char ip [40] = {0}; int port; inet_ntop (AF_INET, & addr. sin_addr, ip, 40); port = ntohs (addr. sin_port); printf ("from % s: % d \ n", ip, port); close (c );}} after the connection is established, use netstat-anp | grep port number to view the port status and process number. If you want to disable the port connection, use kill-9 process number to close-a to display all active TCP connections, and the TCP and UDP ports being monitored.
-N represents the address and port number in a numerical form. N-p lists the processes related to port listening or connection. netstat-tuln lists the port listening statuses of all inet address classes. netstat-tn lists all TCP Protocols. connection status