read() 在套接字编程中没有阻塞 [英] read() is not blocking in socket programming

查看:13
本文介绍了read() 在套接字编程中没有阻塞的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个服务器每 5 秒向客户端发送一次数据.我希望客户端阻止 read() 直到服务器发送一些数据然后打印它.我知道 read () 默认是阻塞的.我的问题是我的客户没有阻止 read().这很奇怪,这似乎不是一个正常的问题.

我的代码在无限循环中打印Nothing come back".我在一台linux机器上,用c编程.我的代码片段如下.请指教.

while(1){n = 读取(sockfd,recvline,MAXLINE);如果 (n > 0){recvline[n] = 0;if (fputs(recvline, stdout) == EOF)printf("fputs 错误");}否则如果(n == 0)printf("什么都没回来");否则,如果 (n <0)printf("读取错误");}返回;

解决方案

可能有多种原因,不同地方可能有几种异常:

  1. 检查您创建的套接字:

    <块引用>

    sockfd=socket(AF_INET,SOCK_STREAM,0);如果 (sockfd==-1) {perror("创建套接字");}

  2. 您还可以在使用之前明确启用阻塞模式:

    <块引用>

    //设置socket I/O模式:本例为FIONBIO//启用或禁用阻塞模式//基于 iMode 数值的套接字.//如果 iMode = 0,则启用阻塞;//如果 iMode != 0,则启用非阻塞模式.ioctl(sockfd, FIONBIO, &iMode);

    或者你可以使用 setsockopt 如下:

     struct timeval t;t.tv_sec = 0;tv_usec = 0;套索选择(sockfd,//套接字描述符SOL_SOCKET,//在套接字 API 级别操作选项SO_RCVTIMEO,//指定接收或发送超时const void *(&t),//选项值大小(t));

  3. 检查读取函数调用(错误原因)

    <块引用>

    n = read(sockfd, recvline, MAXLINE);如果(n <0){perror("读取错误:");}

  4. 另外检查服务器代码!:

    <块引用>

    1. 愿您的服务器发送一些空白(不可打印,空,输入)章程.而你对此一无所知.错误你的服务器代码.

    2. 或者您的服务器在您的客户端可以读取之前终止.

  5. 还有一件有趣的事,试着理解:

    <块引用>

    当您在服务器上调用 N write() 时,不需要在另一端调用 N read().

I have a server that sends data to a client every 5 seconds. I want the client to block on read() until the server sends some data and then print it. I know read () is blocking by default. My problem is that my client is not blocking on read(). This is very odd and this does not seem to be a normal issue.

My code prints "Nothing came back" in an infinite loop. I am on a linux machine, programming in c. My code snippet is below. Please advice.

while(1)
{
    n = read(sockfd, recvline, MAXLINE);
    if ( n > 0) 
    {
        recvline[n] = 0;    
        if (fputs(recvline, stdout) == EOF)
            printf("fputs error");
    }
    else if(n == 0)
        printf("Nothing came back");
    else if (n < 0)
        printf("read error");
}
return; 

解决方案

There may be several cause and several exceptions are possible at different place:

  1. check socket where you create:

    sockfd=socket(AF_INET,SOCK_STREAM,0);  
    if (sockfd==-1) {
        perror("Create socket");
    }
    

  2. You and also enable blocking mode explicitly before use it:

    // Set the socket I/O mode: In this case FIONBIO  
    // enables or disables the blocking mode for the   
    // socket based on the numerical value of iMode.  
    // If iMode = 0, blocking is enabled;   
    // If iMode != 0, non-blocking mode is enabled.
    ioctl(sockfd, FIONBIO, &iMode);  
    

    or you can use setsockopt as below:

     struct timeval t;    
     t.tv_sec = 0;
     tv_usec = 0;
     setsockopt(
          sockfd,     // Socket descriptor
          SOL_SOCKET, // To manipulate options at the sockets API level
          SO_RCVTIMEO,// Specify the receiving or sending timeouts 
          const void *(&t), // option values
          sizeof(t) 
      );   
    

  3. Check Read function call (Reason of bug)

    n = read(sockfd, recvline, MAXLINE);
    if(n < 0){  
        perror("Read Error:");
    }  
    

  4. Also check server code!:

    1. May your server send some blank(non-printable, null, enter) charter(s). And your are unaware of this. Bug you server code too.

    2. Or your server terminated before your client can read.

  5. One more interesting thing, Try to understand:

    When you call N write() at server its not necessary there should be N read() call at other side.

这篇关于read() 在套接字编程中没有阻塞的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆