By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,144 Members | 1,250 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,144 IT Pros & Developers. It's quick & easy.

Stream from file descriptor?

P: n/a
Given a file descriptor (from a network socket, for instance), I want to
have an iostream that reads/writes to it, so I can push and read data in
the traditional way :

sockStream << "<some stuff>" < "\r\n"

How is this possible with the standard header files?
I need this because my objects have xml'ed << operators, but now I can't
figure out how to use them on the actual network stream.

I suppose I'll have to use a temporary file then? Just thought it'd be
possible in one go..
Thanks,
--
- gipsy boy
Jul 22 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
"gipsy boy" <x@x.pi> wrote...
Given a file descriptor (from a network socket, for instance), I want to
have an iostream that reads/writes to it, so I can push and read data in
the traditional way :

sockStream << "<some stuff>" < "\r\n"

How is this possible with the standard header files?
Nope.
I need this because my objects have xml'ed << operators, but now I can't
figure out how to use them on the actual network stream.

I suppose I'll have to use a temporary file then? Just thought it'd be
possible in one go..


You can write your own streambuf-derived class with implementation which
will redirect the output to the platform-specific functions that work
with your sockets, etc. Search the web for examples of that.

V
Jul 22 '05 #2

P: n/a

"gipsy boy" <x@x.pi> wrote in message
news:Ep*********************@phobos.telenet-ops.be...
Given a file descriptor (from a network socket, for instance), I want to
have an iostream that reads/writes to it, so I can push and read data in
the traditional way :

sockStream << "<some stuff>" < "\r\n"

How is this possible with the standard header files?
I need this because my objects have xml'ed << operators, but now I can't
figure out how to use them on the actual network stream.

I suppose I'll have to use a temporary file then? Just thought it'd be
possible in one go..
Thanks,


If the file descriptor is opened in blocking mode, you can use the file
descriptor resources from the Boost Iostreams Library:

http://home.comcast.net/~jturkanis/i.../?path=5.9.1.3.

Sample usage:

using namespace boost::io;
typedef stream_facade<file_descriptor_sink> fdostream;
int fd = ...;
fdostream out(fd);
out << "Hello, World!"; // writes to file descriptor fd.

This library will appear (slightly modified) in the next release of Boost
(1.33). It is available for download here

http://home.comcast.net/~jturkanis/iostreams/

Best Regards,
Jonathan
Jul 22 '05 #3

P: n/a
gipsy boy wrote:
How is this possible with the standard header files?
I need this because my objects have xml'ed << operators, but now I can't
figure out how to use them on the actual network stream.


It's not. There is no such thing in standard C++ as a "file descriptor".

What you can do is write a streambuffer class that does whatever system
specific read and write calls are required. There are open source
implementations out there of this, look up socket++ or something along
those lines.
Jul 22 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.