4 SBind(int sockfd
, const int port
, const int nTries
, const int reuseFlag
)
9 struct sockaddr_in localAddr
;
11 localAddr
.sin_family
= AF_INET
;
12 localAddr
.sin_addr
.s_addr
= htonl(INADDR_ANY
);
13 localAddr
.sin_port
= htons((unsigned short) port
);
15 if (reuseFlag
!= kReUseAddrNo
) {
16 /* This is mostly so you can quit the server and re-run it
17 * again right away. If you don't do this, the OS may complain
18 * that the address is still in use.
21 onsize
= (int) sizeof(on
);
22 (void) setsockopt(sockfd
, SOL_SOCKET
, SO_REUSEADDR
,
23 (char *) &on
, onsize
);
26 /* Tells kernel that it's okay to have more
27 * than one process originating from this
31 onsize
= (int) sizeof(on
);
32 (void) setsockopt(sockfd
, SOL_SOCKET
, SO_REUSEPORT
,
33 (char *) &on
, onsize
);
34 #endif /* SO_REUSEPORT */
38 /* Try binding a few times, in case we get Address in Use
41 if (bind(sockfd
, (struct sockaddr
*) &localAddr
, sizeof(struct sockaddr_in
)) == 0) {
44 if ((int) i
== nTries
) {
47 /* Give the OS time to clean up the old socket,
60 SListen(int sfd
, int backlog
)
62 return (listen(sfd
, (unsigned int) backlog
));