免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
12下一页
最近访问板块 发新帖
查看: 2401 | 回复: 13
打印 上一主题 下一主题

关于网络编程的初级问题 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2003-03-01 12:44 |只看该作者 |倒序浏览
这是一个服务器端的程序,客户端连上后在客户端打印“Hello World”,然后关掉客户端,
但客户在第一次连接的时候没有相应。以后就正常了。

测试客户端程序 :telnet
测试主机:solaris

各位大侠能否指教一二



#include        <stdio.h>;
#include        <stdlib.h>;
#include        <string.h>;
#include        <memory.h>;
#include        <ctype.h>;
#include        <unistd.h>;
#include        <time.h>;
#include        <fcntl.h>;
#include        <signal.h>;
#include        <errno.h>;
#include        <stropts.h>;
#include        <poll.h>;
#include        <sys/types.h>;
#include        <sys/stat.h>;
#include        <sys/socket.h>;
#include        <netinet/in.h>;
#include        <netdb.h>;

main(int argc,char **argv)
{
        int        local_port;
        struct        sockaddr_in remote,local;
        unsigned long        rlen;
        int        sock,client_fd;
        int        status;
        struct        pollfd pd;
        char        buff[2048];
        int        i=0;
       
        local_port = atoi(argv[1]);
       
        bzero(&amp;local,sizeof(local));
        local.sin_addr.s_addr =INADDR_ANY/*inet_addr(local_addr)*/;
        local.sin_family = AF_INET;
        local.sin_port = htons(local_port);
       
        bzero(&amp;remote,sizeof(remote));
        remote.sin_family = AF_INET;
        remote.sin_addr.s_addr =INADDR_ANY /*inet_addr(remote_addr)*/;
        remote.sin_port = 0;
       
        sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
        status = bind(sock,(struct sockaddr *)(&amp;local),sizeof(local));
        if (status<0)
        {
                printf("bind %s\n",strerror(errno));
                return -1;
        }
       
        if (listen(sock,2)==-1)
        {
                printf("listen %s\n",strerror(errno));
                return -1;
        }
       
        pd.fd = sock;
        pd.events = POLLIN;
        pd.revents = 0;
       
        for(;
        {
                status = poll(&amp;pd,1,-1);
                if (status<=0)
                {
                        printf("poll %s\n",strerror(errno));
                        return -1;
                }
               
                if (pd.revents)
                {
                        pd.revents = 0;
                        rlen = sizeof(remote);
                        if (client_fd = accept(sock,(struct sockaddr *)&amp;remote,&amp;rlen)==-1)
                        {
                                printf("accept %s\n",strerror(errno));
                                close(client_fd);
                                continue;
                        }
                        printf("received a connection from %s \n",inet_ntoa(remote.sin_addr));
                        /*
                        status=recv(client_fd,&amp;buff,sizeof(buff),0);
                        printf("recv=%d\n",status);
                        printf("%s\n",buff);
                        if(status==0)
                        {
                                close(client_fd);
                                continue;
                        }
                       
                        memset(buff,sizeof(buff),0);
                        sprintf(buff,"have send %d pakages",i);
                        send(client_fd,&amp;buff,sizeof(buff),0);
                        */
                       
                        if (write(client_fd,"Hello,World\n",12)==-1)
                                perror("write error";
                       
                        i++;
                        close(client_fd);
                }
        }
}

论坛徽章:
0
2 [报告]
发表于 2003-03-01 12:51 |只看该作者

关于网络编程的初级问题

请用

  1. 代码.....
复制代码

论坛徽章:
0
3 [报告]
发表于 2003-03-01 12:59 |只看该作者

关于网络编程的初级问题

第一次提问,以后注意,请大侠继续指教

论坛徽章:
0
4 [报告]
发表于 2003-03-01 13:10 |只看该作者

关于网络编程的初级问题

那你再贴一次啊!

论坛徽章:
0
5 [报告]
发表于 2003-03-01 17:36 |只看该作者

关于网络编程的初级问题

代码:

  1. #include        <stdio.h>;
  2. #include        <stdlib.h>;
  3. #include        <string.h>;
  4. #include        <memory.h>;
  5. #include        <ctype.h>;
  6. #include        <unistd.h>;
  7. #include        <time.h>;
  8. #include        <fcntl.h>;
  9. #include        <signal.h>;
  10. #include        <errno.h>;
  11. #include        <stropts.h>;
  12. #include        <poll.h>;
  13. #include        <sys/types.h>;
  14. #include        <sys/stat.h>;
  15. #include        <sys/socket.h>;
  16. #include        <netinet/in.h>;
  17. #include        <netdb.h>;

  18. main(int argc,char **argv)
  19. {
  20.         int        local_port;
  21.         struct        sockaddr_in remote,local;
  22.         unsigned long        rlen;
  23.         int        sock,client_fd;
  24.         int        status;
  25.         struct        pollfd pd;
  26.         char        buff[2048];
  27.         int        i=0;
  28.        
  29.         local_port = atoi(argv[1]);
  30.        
  31.         bzero(&local,sizeof(local));
  32.         local.sin_addr.s_addr =INADDR_ANY/*inet_addr(local_addr)*/;
  33.         local.sin_family = AF_INET;
  34.         local.sin_port = htons(local_port);
  35.        
  36.         bzero(&remote,sizeof(remote));
  37.         remote.sin_family = AF_INET;
  38.         remote.sin_addr.s_addr =INADDR_ANY /*inet_addr(remote_addr)*/;
  39.         remote.sin_port = 0;
  40.        
  41.         sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
  42.         status = bind(sock,(struct sockaddr *)(&local),sizeof(local));
  43.         if (status<0)
  44.         {
  45.                 printf("bind %s\n",strerror(errno));
  46.                 return -1;
  47.         }
  48.        
  49.         if (listen(sock,2)==-1)
  50.         {
  51.                 printf("listen %s\n",strerror(errno));
  52.                 return -1;
  53.         }
  54.        
  55.         pd.fd = sock;
  56.         pd.events = POLLIN;
  57.         pd.revents = 0;
  58.        
  59.         for(;;)
  60.         {
  61.                 status = poll(&pd,1,-1);
  62.                 if (status<=0)
  63.                 {
  64.                         printf("poll %s\n",strerror(errno));
  65.                         return -1;
  66.                 }
  67.                
  68.                 if (pd.revents)
  69.                 {
  70.                         pd.revents = 0;
  71.                         rlen = sizeof(remote);
  72.                         if (client_fd = accept(sock,(struct sockaddr *)&remote,&rlen)==-1)
  73.                         {
  74.                                 printf("accept %s\n",strerror(errno));
  75.                                 close(client_fd);
  76.                                 continue;
  77.                         }
  78.                         printf("received a connection from %s \n",inet_ntoa(remote.sin_addr));
  79.                         /*
  80.                         status=recv(client_fd,&buff,sizeof(buff),0);
  81.                         printf("recv=%d\n",status);
  82.                         printf("%s\n",buff);
  83.                         if(status==0)
  84.                         {
  85.                                 close(client_fd);
  86.                                 continue;
  87.                         }
  88.                        
  89.                         memset(buff,sizeof(buff),0);
  90.                         sprintf(buff,"have send %d pakages",i);
  91.                         send(client_fd,&buff,sizeof(buff),0);
  92.                         */
  93.                        
  94.                         if (write(client_fd,"Hello,World\n",12)==-1)
  95.                                 perror("write error");
  96.                        
  97.                         i++;
  98.                         close(client_fd);
  99.                 }
  100.         }
  101. }

复制代码

论坛徽章:
0
6 [报告]
发表于 2003-03-01 17:39 |只看该作者

关于网络编程的初级问题

现在怎么样?

论坛徽章:
0
7 [报告]
发表于 2003-03-01 20:16 |只看该作者

关于网络编程的初级问题

我试了一下,结果如下:
$ a.out 2000
received a connection from 192.168.0.47
Hello,World
received a connection from 192.168.0.47
received a connection from 192.168.0.47
received a connection from 192.168.0.47
received a connection from 192.168.0.47
received a connection from 192.168.0.47
received a connection from 192.168.0.47
received a connection from 192.168.0.47
received a connection from 192.168.0.47
received a connection from 192.168.0.47
received a connection from 192.168.0.47

我发现你的问题出在运算符优先级上:

if (client_fd = accept(sock,(struct sockaddr *)&amp;remote,&amp;rlen)==-1)
这句话是将accept()的返回值与-1相比较,得0 (false),然后将0赋给了client_fd,而0表示stdout,所以你的第一个连接的输出是在终端上,而不是套接字上。
那么为什么以后可以了呢?是因为,第一次执行后,你close(client_fd),将stdout给关闭了,所以这之后,标识符0将成为第一个可用的标识符,以后的连接被accept后刚好就使用这个0来标识,而你那个错误的赋值刚好也是0,于是就瞎猫碰着死耗子,行了。应该这样写:

client_fd = accept(sock,(struct sockaddr *)&amp;remote,&amp;rlen);
if (client_fd < 0)
{
     /* error process */
}

or

if ((client_fd = accept(sock, (struct sockaddr *)&amp;remot, &amp;rlen)) < 0)
{
    ...
}

论坛徽章:
0
8 [报告]
发表于 2003-03-01 21:46 |只看该作者

关于网络编程的初级问题

但是,我将程序改了一下,还是一样的错

论坛徽章:
0
9 [报告]
发表于 2003-03-01 22:06 |只看该作者

关于网络编程的初级问题

不好意思,低级错误,我知道了再次感谢

论坛徽章:
0
10 [报告]
发表于 2003-03-02 11:48 |只看该作者

关于网络编程的初级问题

代码
[code]
for(;
fork();
[\code]
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP