As I mentioned yesterday, I'm working on wrapper around Unix sockets. Well, just in case somebody else also wants to use the
__gnu_cxx::stdio_filebuf GNU extension for marrying file descriptors or C FILE*s with C++ iostreams, one word of warning: don't try to create one iostream for both directions. Instead, create everything separated, from the FILE* to the stdio_filebuf to the istream and ostream, otherwise one of the two channels (in my case the out channel) gets messed up. Here's a little code snippet to demonstrate that:
FILE * ifh = fdopen(fd,"r");
FILE * ofh = fdopen(fd,"w");
__gnu_cxx::stdio_filebuf * ibuf = new __gnu_cxx::stdio_filebuf(ifh,std::ios_base::in,1);
__gnu_cxx::stdio_filebuf * obuf = new __gnu_cxx::stdio_filebuf(ofh,std::ios_base::out,1);
std::istream * conn_istream = new std::istream(ibuf);
std::ostream * conn_ostream = new std::ostream(obuf);
Why the extra wrapping with FILE *, even while stdio_filebuf provides a constructor where file descriptors can be put in? Because I made the experience that buffering doesn't quite work as you expect. You need to provide a buffer size, and it only seems to return data to e.g. getline() when the buffer is completely filled. This leads to weird behaviour that getline(istream,strobj) leads to strobj == "ASDF" when the buffer size was 4 and the input was actually "ASDFQWERT\n". So, that's why I'm putting FILE * in between, as it gives me the behaviour that I expect (see
principle of least surprise).