读取功能不返回任何值

时间:2015-01-13 06:59:06

标签: c sockets

来自n的{​​{1}}的值,sockfd = 3并且因为它调用了读取函数,它不会从读取函数中出来。打印读取开始行后,它仅在那里停止:

while(1)
{
        if (n < 0)  
            {
                printf("ERROR writing to socket");              
            }   
        else if(n == 0)
            {
                printf("Nothing");

            }
            printf(" read start n=%d\n ,sockfd = %d\n",n,sockfd);
            n = read(sockfd,buffer,1024);

        if (n < 0)
            {   error("ERROR reading from socket");
            }
            printf("Reply= %s",buffer);
            write_buf_to_file("/root/abc_regr/receive.txt",buffer);  
}
return;

1 个答案:

答案 0 :(得分:1)

默认情况下,

read()阻止调用,因此它会等到获取数据。如果您想要非阻塞函数,请阅读this问题的答案。