C ++ libraries for non-blocking, line-oriented I / O socket?

I'm going to write a Linux client that will make a socket connection to five (or so) servers. The client will send a "string" (a string followed by a "\ n") and receive a string from each of them.

I would like to use select () or epoll () in the client; however, this is TCP and streaming, I am not guaranteed to get a whole "string" in the system buffer when select () appears . I'm looking for a library that provides an API similar to epoll () and sysread () , but works on whole lines of data, buffered and ready to be read.

I'm surprised I couldn't find anything like this in my internet searches. I would think this is a fairly common problem. (I may have phrased the problem incorrectly.) It's not too hard to write, but I suspect an open source solution would be more bulletproof.

+3


source to share


3 answers


Take a look at boost asio . In particular, it has a function async_read_until

that does

Run an asynchronous operation to read data into streambuf until it contains a delimiter, matches a regular expression, or a function object specifies a match.



If you've used http, I also suggest cpp-netlib , which supports asynchronous clients .

+2


source


My MUSCLE networking library provides this (via PlainTextMessageIOGateway ); it works well in my projects and BSD is licensed, so you can use it if you like. The nested portableplaintextclient program in the tests subfolder is a very simple (94 line) example of how to use it. However, other respondents are correct that it is not too difficult to just write the necessary buffering logic; but if you want a pre-written / pre-debugged solution this is one.



+1


source


There is no such thing. You need a buffer for each connection and you need to read it as dictated by the results of select () / poll () / epoll () until a line appears. These APIs don't care about line terminators like you do.

-1


source







All Articles