如果socket服务端程序未调用bind

unp的习题4.5:如果socket在服务端程序中,移除对bind的调用但继续调用listen,会发生什么?

结果是:在未调用bind的情况下,调用listen会为监听套接字分配临时端口号(ephemeral port)。

用以下代码进行验证:

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
#include	"unp.h"
#include <time.h>

int
main(int argc, char **argv)
{
int listenfd, connfd;
socklen_t len;
struct sockaddr_in servaddr, cliaddr;
char buff[MAXLINE];
time_t ticks;
char buf[MAXLINE];

listenfd = Socket(AF_INET, SOCK_STREAM, 0);

len = sizeof(servaddr);
getsockname(listenfd, (SA *)&servaddr, &len);
printf("Local addr: %s\n", Sock_ntop((SA *) &servaddr, len));

// bzero(&servaddr, sizeof(servaddr));
// servaddr.sin_family = AF_INET;
// servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
// servaddr.sin_port = htons(9999); /* daytime server */

// Bind(listenfd, (SA *) &servaddr, sizeof(servaddr));

Listen(listenfd, LISTENQ);

len = sizeof(servaddr);
getsockname(listenfd, (SA *)&servaddr, &len);
printf("Local addr: %s\n", Sock_ntop((SA *) &servaddr, len));

for ( ; ; ) {
len = sizeof(cliaddr);
connfd = Accept(listenfd, (SA *) &cliaddr, &len);
printf("connection from %s, port %d\n",
Inet_ntop(AF_INET, &cliaddr.sin_addr, buff, sizeof(buff)),
ntohs(cliaddr.sin_port));

ticks = time(NULL);
snprintf(buff, sizeof(buff), "%.24s\r\n", ctime(&ticks));
Write(connfd, buff, strlen(buff));

Close(connfd);
}
}

运行结果为:

1
2
3
4
5
6
7
8
9
▶ ./daytimetcpsrv
Local addr: 0.0.0.0:44833
connection from 127.0.0.1, port 9999
connection from 127.0.0.1, port 9999

▶ ./daytimetcpsrv
Local addr: 0.0.0.0:43641
connection from 127.0.0.1, port 9999
connection from 127.0.0.1, port 9999

本博客所有文章除特别声明外,均采用 CC BY-SA 4.0 协议 ,转载请注明出处!