当前时区为 UTC + 8 小时



发表新帖 回复这个主题  [ 3 篇帖子 ] 
作者 内容
1 楼 
 文章标题 : 网络编程:第一次连接和第二次连接的不同
帖子发表于 : 2015-04-16 15:29 

注册: 2014-10-08 20:50
帖子: 95
系统: ubuntu 14.04
送出感谢: 53
接收感谢: 0 次
现在刚刚开始学网络编程,遇到了一个问题想要请教大家,程序可以从这里下载"ftp://www.studyandshare.info/server.c"
执行如下命令:
$ gcc server.c -o server
$ ./server
然后在另一个shell中输入并执行如下命令:
$ telnet localhost 3490
此时会输出一条语句,然后连接断开,如果你再次执行命令‘telnet localhost 3490’,然后用‘netstat -l’查看,会发现连接已经建立了,
这是怎么回事呢?为什么第一次建立连接会自动断开,而第二次建立连接会成功?


页首
 用户资料  
 
2 楼 
 文章标题 : Re: 网络编程:第一次连接和第二次连接的不同
帖子发表于 : 2015-04-16 16:12 
头像

注册: 2008-09-18 13:11
帖子: 2781
送出感谢: 1
接收感谢: 453
你把
代码:
   while(waitpid(-1,NULL,WNOHANG));  //waitpid() is defined in <sys/wait.h>

换成
代码:
   while(1) {
      int k = waitpid(-1,NULL,WNOHANG);  //waitpid() is defined in <sys/wait.h>
      printf("%d\n", k);
      if (!k) {
         break;
      }
   }

再运行就知道了

另外,fork之后也没做对。以下摘自fork(2) manpage
引用:
* The child inherits copies of the parent's set of open file descrip‐
tors. Each file descriptor in the child refers to the same open
file description (see open(2)) as the corresponding file descriptor
in the parent. This means that the two descriptors share open file
status flags, current file offset, and signal-driven I/O attributes
(see the description of F_SETOWN and F_SETSIG in fcntl(2)).

你理解透就知道是怎么回事了



_________________
评价: 3.7% ryt
 
页首
 用户资料  
 
3 楼 
 文章标题 : Re: 网络编程:第一次连接和第二次连接的不同
帖子发表于 : 2015-04-16 20:01 

注册: 2014-10-08 20:50
帖子: 95
系统: ubuntu 14.04
送出感谢: 53
接收感谢: 0 次
astolia 写道:
你把
代码:
   while(waitpid(-1,NULL,WNOHANG));  //waitpid() is defined in <sys/wait.h>

换成
代码:
   while(1) {
      int k = waitpid(-1,NULL,WNOHANG);  //waitpid() is defined in <sys/wait.h>
      printf("%d\n", k);
      if (!k) {
         break;
      }
   }

再运行就知道了

另外,fork之后也没做对。以下摘自fork(2) manpage
引用:
* The child inherits copies of the parent's set of open file descrip‐
tors. Each file descriptor in the child refers to the same open
file description (see open(2)) as the corresponding file descriptor
in the parent. This means that the two descriptors share open file
status flags, current file offset, and signal-driven I/O attributes
(see the description of F_SETOWN and F_SETSIG in fcntl(2)).

你理解透就知道是怎么回事了

非常感谢! :em01


页首
 用户资料  
 
显示帖子 :  排序  
发表新帖 回复这个主题  [ 3 篇帖子 ] 

当前时区为 UTC + 8 小时


在线用户

正在浏览此版面的用户:Google [Bot] 和 1 位游客


不能 在这个版面发表主题
不能 在这个版面回复主题
不能 在这个版面编辑帖子
不能 在这个版面删除帖子
不能 在这个版面提交附件

前往 :  
本站点为公益性站点,用于推广开源自由软件,由 DiaHosting VPSBudgetVM VPS 提供服务。
我们认为:软件应可免费取得,软件工具在各种语言环境下皆可使用,且不会有任何功能上的差异;
人们应有定制和修改软件的自由,且方式不受限制,只要他们自认为合适。

Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
简体中文语系由 王笑宇 翻译