Socket address and port reuse (resulting in bind error)

Source: Internet
Author: User
Tags bind socket htons

v/:* {behavior:url (#default #vml);} o/:* {behavior:url (#default #vml);} w/:* {Behavior:url (#default #vml);. Shape { Behavior:url (#default #vml);} Normal 0 7.8 lbs 0 2 false false MicrosoftInternetExplorer4/* Style definitions */table. msonormaltable {mso-style-name: general form; mso-tstyle-rowband-size:0; mso-tstyle-colband-size:0; mso-style-noshow:yes; Mso-style-parent: ""; mso-padding-alt:0cm 5.4pt 0cm 5.4pt; mso-para-margin:0cm; mso-para-margin-bottom:.0001pt; Mso-pagination:widow-orphan; font-size:10.0pt; Font-family: "Times New Roman"; Mso-ansi-language: #0400; Mso-fareast-language: #0400; Mso-bidi-language: #0400;} Table. Msotablegrid {mso-style-name: grid type; mso-tstyle-rowband-size:0; mso-tstyle-colband-size:0; border:solid windowtext 1.0pt; Mso-border-alt:solid windowtext. 5pt; mso-padding-alt:0cm 5.4pt 0cm 5.4pt; Mso-border-insideh:.5pt solid windowtext; Mso-border-insidev:.5pt solid windowtext; mso-para-margin:0cm; mso-para-margin-bottom:.0001pt; text-align:justify; Text-justify:inter-ideogrAPh Mso-pagination:none; font-size:10.0pt; Font-family: "Times New Roman"; Mso-ansi-language: #0400; Mso-fareast-language: #0400; Mso-bidi-language: #0400;}

In fact, this question is in the details of Richard Stevens's "Unix Network Programming Guide" Volume One.
Answer (Chinese version p166-168 page). Here I just write a few basic examples to verify the problem.
First, declare a question: When the address of two sockets conflicts with port, and you want to reuse the ground
And port, the old socket and the new socket are already set to the SO_REUSEADDR feature, only
There is a problem with either of these features.
SO_REUSEADDR can be used in the following four cases.
(Excerpt from the Unix Network programming Volume One, UNPV1)
1, when there is a socket1 with the same local address and port in the TIME_WAIT state, and you start
The Socket2 of the program to occupy the address and port, your program will need to use this option.
2. SO_REUSEADDR allows multiple instances (multiple processes) of the same server to be started on the same port. But
The IP address of each instance binding cannot be the same. A machine with multiple NICs or IP Alias technology can
To test this situation.
3. SO_REUSEADDR allows a single process to bind the same port to multiple sockets, but each SOC
KET bindings have different IP addresses. This is similar to 2, please see UNPV1.
4. SO_REUSEADDR allows duplicate bindings for exactly the same address and port. But this is only for UDP
Multicast, not for TCP.

Example 1: Test the first case above.

#include <netinet/in .h>
#include <sys/socket.h>
#include <time.h>
#include <stdio.h>
#include < string .h>

#define MAXLINE 100

int main (int argc, char * * argv)
{
int LISTENFD,CONNFD;
struct sockaddr_in servaddr;
Char buff[maxline+1];
time_t ticks;
unsigned short port;
int flag=1,len= sizeof (int);

port=10013;
if ((Listenfd=socket (af_inet,sock_stream,0)) = =-1)
{
Perror ("socket");
Exit (1);
}
Bzero (&servaddr, sizeof (SERVADDR));
Servaddr.sin_family=af_inet;
Servaddr.sin_addr.s_addr=htonl (Inaddr_any);
Servaddr.sin_port=htons (port);
if (setsockopt (LISTENFD, Sol_socket, SO_REUSEADDR, &flag, len) = =-
1)
{
Perror ("setsockopt");
Exit (1);
}
if (bind (LISTENFD, struct sockaddr*) &servaddr, sizeof (servaddr)) = =
-1)
{
Perror ("bind");
Exit (1);
}
Else
printf ("Bind call ok!/n");
if (listen (listenfd,5) = =-1)
{
Perror ("Listen");
Exit (1);
}
for (;;)
{
if ((Connfd=accept (LISTENFD, (struct sockaddr*) null,null) = =-1)

{
Perror ("accept");
Exit (1);
}
if (fork () = = 0)/*child process*/
{
Close (LISTENFD); * * This sentence can not be less, the reason for everyone to think about it. */
Ticks=time (NULL);
snprintf (buff,100, "%.24s/r/n", CTime (&ticks));
Write (Connfd,buff,strlen (buff));
Close (CONNFD);
Sleep (1);
EXECLP ("f1-9d", NULL);
Perror ("EXECLP");
Exit (1);
}
Close (CONNFD);
Exit (0); /* End parent*/
}
}

Test: Compile into a f1-9d program and put it into a path in your own PATH environment variable, for example $HO
Me/bin, run f1-9d, and then telnet to localhost 10013 to see the results.

2, the second situation I do not have environmental testing, so do not give the test procedures, we have the conditions can write their own
One to test.

3. Test the third case procedure

#include <netinet/in .h>
#include <sys/socket.h>
#include <time.h>
#include <stdio.h>
#include < string .h>

#define MAXLINE 100

int main (int argc, char * * argv)
{
int fd1,fd2;
struct sockaddr_in servaddr1,servaddr2;
Char buff[maxline+1];
time_t ticks;
unsigned short port;
int flag=1,len= sizeof (int);

port=10013;
if ((Fd1=socket (af_inet,sock_stream,0)) = =-1)
{
Perror ("socket");
Exit (1);
}
if ((Fd2=socket (af_inet,sock_stream,0)) = =-1)
{
Perror ("socket");
Exit (1);
}
Bzero (&AMP;SERVADDR1, sizeof (SERVADDR1));
Bzero (&AMP;SERVADDR2, sizeof (SERVADDR2));
Servaddr1.sin_family=af_inet;
Servaddr2.sin_family=af_inet;

if (Inet_pton (af_inet, "127.0.0.1", &servaddr1.sin_addr) <= 0)
{
printf ("Inet_pton () call error:127.0.0.1/n");
Exit (1);
}
if (Inet_pton (af_inet, "128.160.1.230", &servaddr2.sin_addr) <= 0)

{
printf ("Inet_pton () call error:128.160.1.230/n");
Exit (1);
}
Servaddr1.sin_port=htons (port);
Servaddr2.sin_port=htons (port);
if (setsockopt (FD1, Sol_socket, SO_REUSEADDR, &flag, len) = =-1)
{
Perror ("setsockopt");
Exit (1);
}
if (setsockopt (FD2, Sol_socket, SO_REUSEADDR, &flag, len) = =-1)
{
Perror ("setsockopt");
Exit (1);
}
if (Bind (FD1, (struct sockaddr*) &servaddr1, sizeof (SERVADDR1)) = =-1)

{
Perror ("Bind fd1");
Exit (1);
}
if (Bind (FD2, (struct sockaddr*) &servaddr2, sizeof (SERVADDR2)) = =-1)

{
Perror ("Bind fd2");
Exit (1);
}
printf ("Bind fd1 and Fd2 ok!/n");
/*put Other Process here*/
GetChar ();
Exit (0); /* END */
}

4, because the fourth case is only used for UDP multicast, and the use of TCP is not much related, so do not write test
An example. I am interested in can write.

All of the above programs are compiled under Linux. It can also be run on other UNIX platforms.

Resources:
1, "Unix Network Programming" Volume One R. Stevens
2, "Socket programming FAQ" http://www.ibrado.com/sock-faq/

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.