Precautions for binding ports to Linux Network Programming and port reuse (1)

Source: Internet
Author: User

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:

 
 
  1. # Include <stdio. h>
  2. # Include <stdlib. h>
  3. # Include <string. h>
  4. # Include <unistd. h>
  5. # Include <sys/socket. h>
  6. # Include <netinet/in. h>
  7. # Include <arpa/inet. h>
  8. Int main (int argc, charchar * argv [])
  9. {
  10. Char server_ip [30] = "10.221.20.12 ";
  11. Int sockfd;
  12. Sockfd = socket (AF_INET, SOCK_DGRAM, 0); // create a UDP socket
  13. If (sockfd <0)
  14. {
  15. Perror ("socket ");
  16. Exit (-1 );
  17. }
  18. // Initialize local network information
  19. Struct sockaddr_in my_addr;
  20. Bzero (& my_addr, sizeof (my_addr ));
  21. My_addr.sin_family = AF_INET;
  22. My_addr.sin_port = htons (8000 );
  23. My_addr.sin_addr.s_addr = htonl (INADDR_ANY );
  24. // Bind port 8000 for the first time
  25. Int err_log;
  26. Err_log = bind (sockfd, (struct sockaddr *) & my_addr, sizeof (my_addr ));
  27. If (err_log! = 0)
  28. {
  29. Perror ("bind 8000 ");
  30. Close (sockfd );
  31. Exit (-1 );
  32. }
  33. // Binding another port 9000 will fail.
  34. My_addr.sin_port = htons (9000 );
  35. Err_log = bind (sockfd, (struct sockaddr *) & my_addr, sizeof (my_addr ));
  36. If (err_log! = 0)
  37. {
  38. Perror ("bind 9000 ");
  39. Close (sockfd );
  40. Exit (-1 );
  41. }
  42. Close (sockfd );
  43. Return 0;
  44. }

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:

 
 
  1. # Include <stdio. h>
  2. # Include <stdlib. h>
  3. # Include <string. h>
  4. # Include <unistd. h>
  5. # Include <sys/socket. h>
  6. # Include <netinet/in. h>
  7. # Include <arpa/inet. h>
  8. Int main (int argc, charchar * argv [])
  9. {
  10. Char server_ip [30] = "10.221.20.12 ";
  11. Int sockfd;
  12. Sockfd = socket (AF_INET, SOCK_DGRAM, 0); // create a UDP socket
  13. If (sockfd <0)
  14. {
  15. Perror ("socket ");
  16. Exit (-1 );
  17. }
  18. Struct sockaddr_in dest_addr;
  19. Bzero (& dest_addr, sizeof (dest_addr ));
  20. Dest_addr.sin_family = AF_INET;
  21. Dest_addr.sin_port = htons (8080); // The server port
  22. Inet_ton (AF_INET, server_ip, & dest_addr.sin_addr );
  23. Char send_buf [512] = "this is for test ";
  24. // If no port is bound, the sendto () system will randomly allocate a port.
  25. Sendto (sockfd, send_buf, strlen (send_buf), 0, (struct sockaddr *) & dest_addr, sizeof (dest_addr); // send data
  26. // Initialize local network information
  27. Struct sockaddr_in my_addr;
  28. Bzero (& my_addr, sizeof (my_addr ));
  29. My_addr.sin_family = AF_INET;
  30. My_addr.sin_port = htons (8000 );
  31. My_addr.sin_addr.s_addr = htonl (INADDR_ANY );
  32. // The port bound to sendto () fails to be bound.
  33. Int err_log;
  34. Err_log = bind (sockfd, (struct sockaddr *) & my_addr, sizeof (my_addr ));
  35. If (err_log! = 0)
  36. {
  37. Perror ("bind 8000 ");
  38. Close (sockfd );
  39. Exit (-1 );
  40. }
  41. Close (sockfd );
  42. Return 0;
  43. }

After the program is compiled and run, the result is as follows:


Contact Us

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

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.