read() is not blocking in socket programming

前端 未结 3 1718
天命终不由人
天命终不由人 2021-01-02 12:51

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 block

3条回答
  •  太阳男子
    2021-01-02 13:05

    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.

提交回复
热议问题