streambuf for socket

Mar 27, 2014 at 8:18pm
Hello, I am trying to make a streambuf for a socket, which will use either WinSock or POSIX sockets depending on OS. I understand about how to send and receive data via sockets, but I don't really understand how to put that into a 'streambuf'. What functions do I need to override? I can't seem to find any information about that anywhere...
Mar 27, 2014 at 9:23pm
Assuming that you know what you're talking about, you're aware of the difference between how things like networking protocols and streams differ in their operation. You could just constantly receive the data from the other host and stash everything in a buffer but that's about as close as you're going to get to something like a stream unless you care to clarify what it is you're trying to do.
Mar 27, 2014 at 9:30pm
It is a TCP/IP stream socket, created with socket(AF_INET, SOCK_STREAM, 0), so an iostream could be used for it (but obviously you need a streambuf). What I need to know specifically is what member functions I need to override to read that socket. I tried overriding underflow(), but when I read from the stream it just loops forever, and if I interrupt it, it causes a segmentation fault.
Last edited on Mar 27, 2014 at 9:35pm
Mar 27, 2014 at 9:38pm
I don't think you want to "override" anything, just create your own object and overload the operators you want. As far as what kind of buffer I would suggest, I'd say a 'filebuf', but that's because I'm lazy and I could see that being the easiest solution even if it isn't the most elegant. Things coming in get written to the file buffer which writes to a file, that file is also opened with a 'filebuf' object that you can then use for your stream.
Mar 27, 2014 at 9:42pm
The problem is I actually need to be able to have an "iostream" with a socket streambuf, as there is some stuff I'll be sending over the Internet, as well as saving to a file. I can't use a filebuf as that would not work on Windows due to the WinSock implementation. I need to make a derivative of streambuf and override some functions in order to make it read/write the socket. Is it possible?
Mar 27, 2014 at 10:01pm
Mar 27, 2014 at 10:12pm
Thank you, I'll try that.
Mar 28, 2014 at 12:42am
Computergeek01 wrote:
Assuming that you know what you're talking about, you're aware of the difference between how things like networking protocols and streams differ in their operation. You could just constantly receive the data from the other host and stash everything in a buffer but that's about as close as you're going to get to something like a stream unless you care to clarify what it is you're trying to do.
Stream buffers can be implemented such that they only have a 1-byte internal buffer - in that case it is trivial to map them to socket interfaces.
Topic archived. No new replies allowed.