The so-called bind (BIND) refers to someone else connected I can only through the port I bind, equivalent to, I bought a mobile phone, others want to contact me, must know my mobile phone number, this time, I need to do? I need to plug in a phone card and fix a phone number so someone can contact me via this phone number. Phone card, fixed a phone number, similar to binding (BIND) process, binding (BIND) in order to fix a port number, the other network program can find the port number, find this port number to find the corresponding network application program.
In network programming, it is common to bind (BIND) ports in the server, which is not to say that the client cannot bind (BIND) ports, but it is important to note that a network application can only bind one port (one socket can only bind one port).
A socket cannot bind more than one port at a time, 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, char *argv[]) {char server_ip [+] = "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);//First bound port 8000int Err_log;err_log = Bind (SOCKFD, Struc T sockaddr*) &my_addr, sizeof (MY_ADDR)), if (err_log! = 0) {perror ("bind 8000"), close (SOCKFD); exit (-1);} Bind another port 9000 again, bind failed 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 compiles, the results are as follows:
If the client wants to bind the port number, be sure to call the Send information function before binding (BIND) port, because in the Send information function (sendto, or write), the system will automatically assign a random port number to the current network program, which is equivalent to randomly bound a port number, then we bind the port number, Will bind failed. If we bind before the Send Message function (sendto, or write), the program will send the message with the port number that we bound, and no more randomly assign 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, char *argv[]) {char server_ip [+] = "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));d est_addr.sin_family = Af_inet;dest_addr.sin_ Port = htons (8080);//server Ports Inet_pton (af_inet, server_ip, &dest_addr.sin_addr); char send_buf[512] = "This was for TES T ";//If there is no previous bound port, the sendto () system randomly assigns 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);//SendTo () After binding port, bind failed 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 compiles, the results are as follows:
Source code download please click here.
Linux Network Programming: Binding (BIND) ports need to be aware of issues