Chinaunix

标题: 大家见过这样奇怪的现象吗?read函数不返回了 [打印本页]

作者: larace    时间: 2008-07-09 00:16
标题: 大家见过这样奇怪的现象吗?read函数不返回了
while (1)
        {
                int n,i,iret;
                n = -1;
                printf("------------------sock_fd is %d\n",sock_fd);
                if(sock_fd<0){
                        sock_fd=client_sock(ser_port,ser_ip);
                        printf("after client , sock_fd is %d\n",sock_fd);
                        if(sock_fd<0){
                        sleep(2);
                        continue;
                        }
                }
                memset(tmp_buf,0,sizeof(tmp_buf));
                if(mark ==1)
                {
                        printf("test n is %d\n",n);
                        printf("++++++++++++++++sock_fd is %d\n",sock_fd);
                        n = read(sock_fd,tmp_buf,sizeof(tmp_buf));
                        printf("after read, n is %d",n);
                }
                else if(mark ==0){
                        printf("mark not 1\n");
                        break;
                }
                printf("after mark , n is %d",n);

               ...
        }


程序输出的结果是:
------------------sock_fd is -1
sock_fd is -1
after client , sock_fd is 7
test n is -1
++++++++++++++++sock_fd is 7
------------------sock_fd is -1


也就是说read之后,没有任何返回, 直接进入下一循环,而且 sock_fd的值还没赋成-1了??
作者: larace    时间: 2008-07-09 00:18
sock_fd =-1 是在while循环之前就被赋值了

程序结果是重复这样
------------------sock_fd is -1
sock_fd is -1
after client , sock_fd is 100
test n is -1
++++++++++++++++sock_fd is 100
------------------sock_fd is -1
sock_fd is -1
after client , sock_fd is 101
test n is -1
++++++++++++++++sock_fd is 101
------------------sock_fd is -1
sock_fd is -1
after client , sock_fd is 102
test n is -1
++++++++++++++++sock_fd is 102
------------------sock_fd is -1
sock_fd is -1
after client , sock_fd is 103
test n is -1
++++++++++++++++sock_fd is 103
------------------sock_fd is -1
sock_fd is -1
after client , sock_fd is 104
作者: scutan    时间: 2008-07-09 00:32
tmp_buf是什么类型?
其实我觉得1. read函数肯定是有返回的,系统不会这么脆弱,不让它返回就跳转到其它的地址进行执行。 2. 系统调用也是需要将参数通过寄存器再传到内核态中去,所以你的sock_fd变量在read中只是一个参数,根本不会发生改变。
所以我觉得应该是其它地方出现了什么问题。另外,是多线程吗?
作者: larace    时间: 2008-07-09 00:44
不是多线程

我有两个循环
    while(1){
        printf("in while 1\n");
        int iDataType=0;
        int iHead =1;
        int mark =1;
        while (1)
        {
                int n,i,iret;
                n = -1;
                printf("------------------sock_fd is %d\n",sock_fd);
                if(sock_fd<0){
                        printf("sock_fd is %d\n",sock_fd);
                        sock_fd=client_sock(ser_port,ser_ip);
                        printf("after client , sock_fd is %d\n",sock_fd);
                        if(sock_fd<0){
                        sleep(2);
                        continue;
                        }
                }
                memset(tmp_buf,0,sizeof(tmp_buf));
                if(mark ==1)
                {
                        printf("test n is %d\n",n);
                        printf("++++++++++++++++sock_fd is %d\n",sock_fd);
                        n = read(sock_fd,tmp_buf,sizeof(tmp_buf));
                        printf("after re
作者: blizzard213    时间: 2008-07-09 00:46
原帖由 larace 于 2008-7-9 00:44 发表
不是多线程

我有两个循环
    while(1){
        printf("in while 1\n");
        int iDataType=0;
        int iHead =1;
        int mark =1;
        while (1)
        {
                in ...


有服务器吗? 是数据报套接口还是流套接口?
会不会是中断
作者: larace    时间: 2008-07-09 00:47
我发现 第一个 while 里的输出in while 1 总是输出,也就是说  第二个while 肯定退出了.

但是在哪里退出的呢?

我有写了一个小程序,ip 和端口 都不变

   while(1){
    printf("begin ,hihihi\n");
        while(1){
                n=read(sockfd,buf,20);
                printf("buf is %s\n",buf);
                printf("n is %d,sock_fd is %s\n",n,sockfd);
                sleep(1);
        }
   }

我发现,这次,是read一直阻塞,不返回. 程序输出hihihi之后,就不动了.  

但是为什么上面的程序虽然也是read之后的东西没有显示,但是循环就能执行了呢
作者: scutan    时间: 2008-07-09 00:48
gdb单步调试吧,这样可以更快地找到问题所在。
作者: larace    时间: 2008-07-09 00:49
不是流的.

但是是 将原来的x25 协议转换成tcp/ip了

telnet 那个映射的 ip和端口,会出现Escape character is '^]'.

也就是说对端没有任何数据吐出来,实际应该是有数据的.

但是话说回来,为什么两个小程序的结果不一样呢?
作者: scutan    时间: 2008-07-09 00:49
原帖由 larace 于 2008-7-9 00:47 发表
我发现 第一个 while 里的输出in while 1 总是输出,也就是说  第二个while 肯定退出了.

但是在哪里退出的呢?

我有写了一个小程序,ip 和端口 都不变

   while(1){
    printf("begin ,hihihi\n");
   ...

说明read肯定是有返回的,你程序中的错误应该不是在这个地方。gdb一下看看。
作者: larace    时间: 2008-07-09 00:52
机器没有调试的条件.
不能执行gdb,dbx什么的.

这个现象也太奇怪了!我的测试有什么漏洞吗
作者: larace    时间: 2008-07-09 00:53
但是那个小程序,很简单的
#include     <stdio.h>
#include     <sys/types.h>
#include     <sys/socket.h>
#include     <arpa/inet.h>
#include     <netinet/in.h>
#include     <netdb.h>
#include     <time.h>
#include     <errno.h>

int main(int argc, char **argv)
{
    int     sockfd, n;
    char    recvline[1024 + 1];
    char buf[1000];
    struct sockaddr_in servaddr;
    char *ip="10.1.125.1";

    if ( (sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0)
        printf("socket error");

    bzero(&servaddr, sizeof(servaddr));
    servaddr.sin_family = AF_INET;
    servaddr.sin_port = htons(103);  /* daytime server */
    if (inet_pton(AF_INET,ip, &servaddr.sin_addr) <= 0)
        printf("inet_pton error");

    if (connect(sockfd, (struct sockaddr *) &servaddr, sizeof(servaddr)) < 0)
        printf("connect error");
   while(1){
    printf("begin ,hihihi\n");
        while(1){
                n=read(sockfd,buf,20);
                printf("buf is %s\n",buf);
                printf("n is %d,sock_fd is %s\n",n,sockfd);
                sleep(1);
        }
   }
}

他可以说明,read阻塞了.
作者: larace    时间: 2008-07-09 00:54
这个小程序的结果是
begin ,hihihi

然后什么都没有了,一直停在那不动
作者: scutan    时间: 2008-07-09 00:55
标题: 回复 #11 larace 的帖子
这是平常很常见的,read一般情况下就是阻塞的。你先把你原来的代码简化一下,再找问题。
作者: blizzard213    时间: 2008-07-09 00:58
很像是continue的问题 但也没写错地方啊
作者: larace    时间: 2008-07-09 01:38
我再想想吧!
这么晚了大家都还没睡觉呢,谢谢各位!
兄弟先撤了
作者: flw    时间: 2008-07-09 08:45
不就是不会用  printf 嘛。
作者: rainysky    时间: 2008-07-09 09:13
请在printf的后面加上 \n




欢迎光临 Chinaunix (http://bbs.chinaunix.net/) Powered by Discuz! X3.2