Skip to content

Blocking when using read_all with socket stream #69

@thanhtungtvg95

Description

@thanhtungtvg95

I am using function read_all() but process is blocking.
I find this happen here:
int read_bytes = read(fd, buf + rbytes, read_upto - rbytes);

Solution:

      fd_set fds;
      struct timeval timeout;

      timeout.tv_sec = 3; /* timeout in secs */
      timeout.tv_usec = 10000;
      FD_ZERO(&fds);
      FD_SET(fd, &fds);
      if (select(fd + 1, &fds, NULL, NULL, &timeout) > 0) {
        read_bytes = read(fd, buf + rbytes, read_upto - rbytes);
      } else {
        std::cout << "Time out" << std::endl;
      }

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions