forked from DingHe/unpv13e
-
Notifications
You must be signed in to change notification settings - Fork 2
/
tcp_listen.c
61 lines (48 loc) · 1.75 KB
/
tcp_listen.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
/* include tcp_listen */
#include "unp.h"
int
tcp_listen(const char *host, const char *serv, socklen_t *addrlenp)
{
int listenfd, n;
const int on = 1;
struct addrinfo hints, *res, *ressave;
bzero(&hints, sizeof(struct addrinfo));
//AI_PASSIVE和AF_UNSPEC两个暗示信息将会返回2个套接字地址结构:
// 1个IPv4的,1个IPv6的
hints.ai_flags = AI_PASSIVE;//因为该函数供服务器使用
hints.ai_family = AF_UNSPEC;
hints.ai_socktype = SOCK_STREAM;
if ( (n = getaddrinfo(host, serv, &hints, &res)) != 0)
err_quit("tcp_listen error for %s, %s: %s",
host, serv, gai_strerror(n));
ressave = res;
do {
listenfd = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
if (listenfd < 0)
continue; /* error, try next one */
Setsockopt(listenfd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
if (bind(listenfd, res->ai_addr, res->ai_addrlen) == 0)
break; /* success */
Close(listenfd); /* bind error, close and try next one */
} while ( (res = res->ai_next) != NULL);
if (res == NULL) /* errno from final socket() or bind() */
err_sys("tcp_listen error for %s, %s", host, serv);
Listen(listenfd, LISTENQ);
//通过指针addrlenp返回协议地址的带下,这个大小允许调用者在通过accept获取
//客户的协议地址时分配一个套接字地址结构的内存空间
if (addrlenp)
*addrlenp = res->ai_addrlen; /* return size of protocol address */
freeaddrinfo(ressave);
return(listenfd);
}
/* end tcp_listen */
/*
* We place the wrapper function here, not in wraplib.c, because some
* XTI programs need to include wraplib.c, and it also defines
* a Tcp_listen() function.
*/
int
Tcp_listen(const char *host, const char *serv, socklen_t *addrlenp)
{
return(tcp_listen(host, serv, addrlenp));
}