阅读()的socket编程不堵 [英] read() is not blocking in socket programming

查看:196
本文介绍了阅读()的socket编程不堵的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有数据发送到客户端,每5秒的服务器。我想在客户端上阅读()阻塞,直到服务器发送一些数据,然后打印。我知道,阅读()默认情况下阻止。我的问题是我的客户不阻塞的read()。这是很奇怪的,这似乎并没有成为一个正常的问题。

我的code打印在一个无限循环没有回来。我是一个Linux机器上,C编程。我的code片断如下。请指点。

 而(1)
{
    N =读取(的sockfd,recvline,MAXLINE);
    如果(N 0)
    {
        recvline [η] = 0;
        如果(的fputs(recvline,标准输出)== EOF)
            输出(的fputs错误);
    }
    否则,如果(N == 0)
        的printf(没什么回来了);
    否则如果(正℃,)
        的printf(读取错误);
}
返回;


解决方案

有可能有几个原因,几个例外是可以在不同的地方:


  1. 检查插座在其中创建:


      =的sockfd插座(AF_INET,SOCK_STREAM,0);
    如果(的sockfd == - 1){
        PERROR(创建插座);
    }



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


      //设置套接字I / O模式:在这种情况下FIONBIO
    //启用或禁用阻塞模式为
    //基于的iMode的数值插座。
    //如果iMode平台= 0,阻止已启用;
    //如果iMode平台!= 0,非阻塞模式已启用。
    的ioctl(的sockfd,FIONBIO,&安培; iMode平台);


    您也可以使用 的setsockopt 如下:

      timeval结构吨;
     t.tv_sec = 0;
     tv_usec = 0;
     setsockopt的(
          的sockfd // socket描述符
          SOL_SOCKET,//要操纵在套接字API级别选项
          SO_RCVTIMEO,//指定接收或发送超时
          常量无效*(& T公司),//选项值
          的sizeof(T)
      );


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


      N =读取(的sockfd,recvline,MAXLINE);
    如果(正℃,){
        PERROR(读错误:);
    }



  4. 此外检查服务器code


      

        
    1. 愿你的服务器发送一些空白(非打印,空,进入)章程(S)。而你不知道这一点。打扰你的服务器code了。


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


    4.   


  5. 还有一个有趣的事情,试着去了解:


      

    当你调用ñ的write()在服务器上的没有必要,应该为N 阅读()在呼叫另一边。



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.

这篇关于阅读()的socket编程不堵的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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