简体   繁体   中英

Best way of reading all available bytes from socket in blocking mode, C++

What is the best way of reading from blocking socket all available bytes? 'Available' means that server response is a bunch of text lines (each with '\\n') and EOF is a line with some prefix. Length of this response is always different, so I don't know it before reading. I assume using select() ( poll , epoll ) and then 'ioctl(FIONREAD)' is the best way, am I right? Or may be just reading all available at that moment, then checking if EOF is reached and if not, then repeating all again? Yes, it sounds more rational. Does it all make any sense? So, what is the most efficient way?

If you are doing blocking reads there is little point in using select / epoll .

ioctl(FIONREAD) is a pretty useless call because by the time it returns more data may have arrived into the kernel socket buffer.

Blocking reads are easy: just keep read ing into your user-space socket buffer until the message terminator is found.

Ideally, the networking components you use should not care whether the socket is in blocking or in non-blocking mode: there should be a function that is called when the socket is ready for reading. In blocking mode you would call this function regardless of whether the socket is ready, it just blocks on read if no data is available.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM