create fstream from raw file descriptor

This is a discussion on create fstream from raw file descriptor within the C++ Programming forums, part of the General Programming Boards category; if I have a raw file descriptor, for example, a socket descriptor, is there any way to take that descriptor ...

  1. #1
    Registered User
    Join Date
    Oct 2006
    Posts
    2,421

    create fstream from raw file descriptor

    if I have a raw file descriptor, for example, a socket descriptor, is there any way to take that descriptor and pass it to any type of iostream object to create an iostream that reads and writes that file descriptor? file descriptors are a very common and portable part of most APIs, and if there is no way to do this, it seems like a huge oversight in the design of the iostreams library.

  2. #2
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    I have written one for this purpose before, actually. I can try to dig it up for you if you would like.

  3. #3
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    I am not certain I understand what you mean. I believe you mean using a file descriptor in order to read and write from the file described by the file descriptor. If that is the case I don't see why not. That is what fstream does. This is done by the constructor, as noted here
    http://publib.boulder.ibm.com/infoce...y/ref/i196.htm (don't know if this is standard or whatever)
    I ll test it and see...

    EDIT: Does NOT work, should pay more attention on the above side. Sorry
    Last edited by C_ntua; 09-18-2008 at 02:04 PM.

  4. #4
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    A lot of the problem with that, C_ntua is that a socket is not always considered the same as a file descriptor. In Linux these are treated as being the same animal however Windows does not appreciate you trying to fool it into using a socket as a file descriptor.

  5. #5
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    This is what you are looking for http://publib.boulder.ibm.com/infoce...y/ref/i196.htm

    Note that the site says that boost has better functions for this. So use those. They do exactly what you want. Google for examples...

    EDIT: (Seeing the above post) Well, then I believe you have no choice but writing it yourself. Or modifying the existing ones to match your specific data. Or be "clever" and find the actual type of the socket handler (lets say int) and cast it. Or cast it and test it (bad idea). Or... create it and learn in the process. Personally... I would do the third one (shame shame on me)
    Last edited by C_ntua; 09-18-2008 at 02:25 PM.

  6. #6
    Registered User
    Join Date
    Oct 2006
    Posts
    2,421
    unfortunately, it appears that the GNU standard C++ library does not support the iostream::iostream(int fd) overload, as it says "invalid conversion from int to const char*" when I try to call it that way.

    here is what I have tried apart from the above:
    Code:
    FILE* i = fdopen(socket, "r");
    FILE* o = fdopen(socket, "w");
    std::filebuf ibuf(i, std::ios_base::in);
    std::filebuf obuf(o, std::ios_base::out);
    my_ifstream.rdbuf(ibuf);
    my_ofstream.rdbuf(obuf);
    but this doesn't seem to be supported either.

    I'm running OpenSuse linux with kernel 2.6.x and gcc 4.2.1, and there are about 6 libstdc++ files in my /usr/lib directory, so I don't even know which one is getting linked in.

  7. #7
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    What the hell... your code is entirely plausable on OpenSuSE... I have an OpenSuSE box. The one I wrote was to accomodate windows. So it wouldn't be of as much use. Hmmmmmmm I am a little stumped. Why not just use the C functions?

  8. #8
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    Any particular reason you can't use this?
    http://www.boost.org/doc/libs/1_36_0...escriptor.html

    There is absolutely no way to get a standard iostream for a file descriptor, except for a few standard library implementations which provide this functionality as an extension.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  9. #9
    Registered User
    Join Date
    Oct 2006
    Posts
    2,421
    Quote Originally Posted by CornedBee View Post
    Any particular reason you can't use this?
    http://www.boost.org/doc/libs/1_36_0...escriptor.html
    Not sure exactly how I would use it. it doesn't exactly give any examples, and there isn't any immediately apparent way to read from or write to an instance of any of the classes described in that link. I really don't know anything about boost, so I'm sort of lost here.

  10. #10
    Registered User
    Join Date
    Oct 2006
    Posts
    2,421
    while we're on the subject, can you recommend any good books dealing with boost?

  11. #11
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    There's three books that are partially or in whole about Boost, AFAIK:
    "Beyond the C++ Standard Library" introduces a few of the oldest and most important Boost libraries.
    "C++ Template Metaprogramming" is about template metaprogramming, but also has a section on the MPL and a short section on the Preprocessor library.
    "The Boost Graph Library" is exactly what the name says.

    Here's the link to the whole documentation of the IOStreams library:
    http://www.boost.org/doc/libs/1_36_0...doc/index.html

    An example of how to use an arbitrary device with the generic streams (and thus gain standard iostreams syntax) is at the bottom of this page:
    http://www.boost.org/doc/libs/1_36_0...c_streams.html
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  12. #12
    Registered User
    Join Date
    Oct 2006
    Posts
    2,421
    I checked out the boost iostreams and file_descriptor_sink/source, and I was able to make it compile/link with the following code:

    Code:
    typedef stream<file_descriptor_sink> ofdstream;
    typedef stream<file_descriptor_source> ifdstream;
    
    //in my socket class declaration:
    int m_sock; // the actual socket descriptor
    ofdstream* ofs;
    ifdstream* ifs;
    ofdstream& GetOstream(void) { return *ofs; }
    ifdstream& GetIstream(void) { return *ifs; }
    
    //in my socket class constructor:
    ofs = new ofdstream(m_sock);
    ifs = new ifdstream(m_sock);
    
    //in my function that uses the socket, in which the socket object is called sock:
    ifdstream& ifs = sock.GetIstream();
    ofdstream& ofs = sock.GetOstream();
    std::string text_in;
    ifs >> text_in;
    but this crashes when I try to read this way. Any ideas on why this might be happening?

  13. #13
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    No. Can you get a backtrace?
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  14. #14
    Registered User
    Join Date
    Oct 2006
    Posts
    2,421
    it doesn't generate a backtrace. my SIGSEGV handler catches it and exits. I could try debugging, but I'm pretty sure it's crashing either in the boost_iostreams lib or inside std::istream, so I'd have basically no options for fixing it.

  15. #15
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    Both Boost and GCC are open source, so you have the option of fixing it. More to the point, since many people use these libraries, knowing about a segfault and fixing it would be important to us. Please use a debugger to get a backtrace.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Formatting the contents of a text file
    By dagorsul in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2008, 12:36 PM
  2. gcc link external library
    By spank in forum C Programming
    Replies: 6
    Last Post: 08-08-2007, 03:44 PM
  3. Inventory records
    By jsbeckton in forum C Programming
    Replies: 23
    Last Post: 06-28-2007, 04:14 AM
  4. C++ std routines
    By siavoshkc in forum C++ Programming
    Replies: 33
    Last Post: 07-28-2006, 12:13 AM
  5. Simple File encryption
    By caroundw5h in forum C Programming
    Replies: 2
    Last Post: 10-13-2004, 10:51 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21