您的位置:首页 > 理论基础 > 计算机网络

tinyhttpd源码分析

2016-09-14 21:09 489 查看
一边学一边写,慢慢更新

TinyHttpd是一个轻量级HTTP服务器,主要部分只有500行

项目下载地址

点击打开链接

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);
printf("the port is %d\n",*port);
if (getsockname(httpd, (struct sockaddr *)&name, &namelen) == -1)
error_die("getsockname");
*port = ntohs(name.sin_port);
printf("the port is %d\n",*port);
}
修改httpd.c部分代码,再运行httpd,最终的输出结果:

the port is 51963

the port is 51963

httpd running on port 51963

也就是说getsockname()函数只在port等于0的时候才会返回本地端口号,当端口号本身不为0的时候不会再去修改端口号

getsockname()与getpeername()的作用可参考《Unix网络编程卷1》
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: