Precautions for binding ports to Linux Network Programming and port reuse (1)
The so-called binding (bind) means that when someone connects to me, I can only use the port I bind, which is equivalent to buying a mobile phone. If someone else wants to contact me, he must know my mobile phone number, what should I do at this time? I need to plug in a phone card to fix a phone number so that someone else can contact me through this phone number. The mobile phone is inserted into the phone card and a fixed phone number is similar to the bind process. To fix a port number, other network programs can find the port number, find the network application corresponding to the port number.
In network programming, the port (bind) is usually bound to the server. This does not mean that the port (bind) cannot be bound to the client, but note that, A network application can only bind one port (one socket can only bind one port ).
A socket cannot be bound to multiple ports at the same time, as shown below:
- # Include <stdio. h>
- # Include <stdlib. h>
- # Include <string. h>
- # Include <unistd. h>
- # Include <sys/socket. h>
- # Include <netinet/in. h>
- # Include <arpa/inet. h>
- Int main (int argc, charchar * argv [])
- {
- Char server_ip [30] = "10.221.20.12 ";
- Int sockfd;
- Sockfd = socket (AF_INET, SOCK_DGRAM, 0); // create a UDP socket
- If (sockfd <0)
- {
- Perror ("socket ");
- Exit (-1 );
- }
- // Initialize local network information
- Struct sockaddr_in my_addr;
- Bzero (& my_addr, sizeof (my_addr ));
- My_addr.sin_family = AF_INET;
- My_addr.sin_port = htons (8000 );
- My_addr.sin_addr.s_addr = htonl (INADDR_ANY );
- // Bind port 8000 for the first time
- Int err_log;
- Err_log = bind (sockfd, (struct sockaddr *) & my_addr, sizeof (my_addr ));
- If (err_log! = 0)
- {
- Perror ("bind 8000 ");
- Close (sockfd );
- Exit (-1 );
- }
- // Binding another port 9000 will fail.
- My_addr.sin_port = htons (9000 );
- Err_log = bind (sockfd, (struct sockaddr *) & my_addr, sizeof (my_addr ));
- If (err_log! = 0)
- {
- Perror ("bind 9000 ");
- Close (sockfd );
- Exit (-1 );
- }
- Close (sockfd );
- Return 0;
- }
After the program is compiled and run, the result is as follows:
If the client wants to bind the port number, you must bind the port before calling the sending information function, because in the sending information function (sendto, or write ), the system will automatically assign a random port number to the current network program. This is equivalent to a random port number bound, which will only be allocated once. Later communications will communicate with this random port, if we bind the port number, the binding will fail. If we bind it before the sending information function (sendto, or write), then the program will send the information using the port number we bind and will not randomly allocate a port number.
The binding failure example (UDP) is as follows:
- # Include <stdio. h>
- # Include <stdlib. h>
- # Include <string. h>
- # Include <unistd. h>
- # Include <sys/socket. h>
- # Include <netinet/in. h>
- # Include <arpa/inet. h>
- Int main (int argc, charchar * argv [])
- {
- Char server_ip [30] = "10.221.20.12 ";
- Int sockfd;
- Sockfd = socket (AF_INET, SOCK_DGRAM, 0); // create a UDP socket
- If (sockfd <0)
- {
- Perror ("socket ");
- Exit (-1 );
- }
- Struct sockaddr_in dest_addr;
- Bzero (& dest_addr, sizeof (dest_addr ));
- Dest_addr.sin_family = AF_INET;
- Dest_addr.sin_port = htons (8080); // The server port
- Inet_ton (AF_INET, server_ip, & dest_addr.sin_addr );
- Char send_buf [512] = "this is for test ";
- // If no port is bound, the sendto () system will randomly allocate a port.
- Sendto (sockfd, send_buf, strlen (send_buf), 0, (struct sockaddr *) & dest_addr, sizeof (dest_addr); // send data
- // Initialize local network information
- Struct sockaddr_in my_addr;
- Bzero (& my_addr, sizeof (my_addr ));
- My_addr.sin_family = AF_INET;
- My_addr.sin_port = htons (8000 );
- My_addr.sin_addr.s_addr = htonl (INADDR_ANY );
- // The port bound to sendto () fails to be bound.
- Int err_log;
- Err_log = bind (sockfd, (struct sockaddr *) & my_addr, sizeof (my_addr ));
- If (err_log! = 0)
- {
- Perror ("bind 8000 ");
- Close (sockfd );
- Exit (-1 );
- }
- Close (sockfd );
- Return 0;
- }
After the program is compiled and run, the result is as follows: