tinyhttpd源码分析

来源:互联网 发布:双色球旋转矩阵出6保6 编辑:程序博客网 时间:2024/05/18 00:16

一边学一边写,慢慢更新

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》





0 0
原创粉丝点击