Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

startup函数中,getsockname检查、请求分配端口为何要在bind后? #23

Open
juliehy opened this issue Dec 25, 2017 · 1 comment

Comments

@juliehy
Copy link

juliehy commented Dec 25, 2017

if (bind(httpd, (struct sockaddr *)&name, sizeof(name)) < 0)
error_die("bind");
if (port == 0) / if dynamically allocating a port */
{
socklen_t namelen = sizeof(name);
if (getsockname(httpd, (struct sockaddr *)&name, &namelen) == -1)
error_die("getsockname");
*port = ntohs(name.sin_port);
}

为什么不在bind前判断端口是否为0,为0就调用getsockname让系统自动分配。
现在在bind后,如果bind的端口为0,bind失败、分配了也没用,代码里也没有重新bind。

@linzewei1991
Copy link

bind的过程才自动分配端口(如果bind时相应的端口为0)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants