LinuxTCP client specifies the port number to connect to the server
Source: Internet
Author: User
The linuxTCP 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 it...
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 of www.2cto.com # include # Include # Include # Include 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); dest. sin_family = AF_INET; dest. sin_port = htons (9999); inet_ton (AF_INET, "192.168.1.100", & dest. sin_addr); int B = bind (sock Fd, (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 # Include # Include # Include 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
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.
A Free Trial That Lets You Build Big!
Start building with 50+ products and up to 12 months usage for Elastic Compute Service