Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8185701
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T01:56:26+00:00 2026-06-07T01:56:26+00:00

Is there a C/C++ way to read data from a socket using read() and

  • 0

Is there a C/C++ way to read data from a socket using read() and having the receiving buffer be a file (ofstream) or a similar self-extending object (vector e.g.)?

EDIT: The question arose while I contemplated how to read a stream socket that may receive the contents of a, say 10000+ byte file. I just never did like putting 20000 or 50000 bytes (large enough for now) on the stack as a buffer where the file could be stored temporarily till I could stick in into a file. Why not just stream it directly into the file to star with.

Much like you can get at the char* inside a std:string, I thought of something like

read( int fd, outFile.front(), std::npos );  // npos = INT_MAX

or something like that.

end edit

Thanks.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T01:56:28+00:00Added an answer on June 7, 2026 at 1:56 am

    This is simplistic, and off the top of my fingers, but I think something along these lines would work out:

    template <unsigned BUF_SIZE>
    struct Buffer {
        char buf_[BUF_SIZE];
        int len_;
        Buffer () : buf_(), len_(0) {}
        int read (int fd) {
            int r = read(fd, buf_ + len_, BUF_SIZE - len_);
            if (r > 0) len_ += r;
            return r;
        }
        int capacity () const { return BUF_SIZE - len_; }
    }
    
    template <unsigned BUF_SIZE>
    struct BufferStream {
        typedef std::unique_ptr< Buffer<BUF_SIZE> > BufferPtr;
        std::vector<BufferPtr> stream_;
        BufferStream () : stream_(1, BufferPtr(new Buffer<BUF_SIZE>)) {}
        int read (int fd) {
            if ((*stream_.rbegin())->capacity() == 0)
                stream_.push_back(BufferPtr(new Buffer<BUF_SIZE>));
            return (*stream_.rbegin())->read(fd);
        }
    };
    

    In a comment, you mentioned you wanted to avoid creating a big char buffer. When using the read system call, it is generally more efficient to perform a few large reads rather than many small ones. So most implementations will opt for large input buffers to gain that efficiency. You could implement something like:

    std::vector<char> input;
    char in;
    int r;
    while ((r = read(fd, &in, 1)) == 1) input.push_back(in);
    

    But that would involve a system call and at least one byte copied for every byte of input. In contrast, the code I put forth avoids extra data copies.

    I don’t really expect the code I put out to be the solution you would adopt. I just wanted to provide you with an illustration of how to create a self-extending object that was fairly space and time efficient. Depending on your purposes, you may want to extend it, or write your own. Off the top of my head, some improvements may be:

    • use std::list instead, to avoid vector resizing
    • allow API a parameter to specify how many bytes to read
    • use readv to always allow at least BUF_SIZE bytes (or more than BUF_SIZE bytes) to be read at a time
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I was wondering if there was a way to read data from a file
Is there any way to read whole lines from a socket in Erlang, or
Is there a way to read off from an [external] xml (an xml file
Is there any way to read a file using sharpsvn................
Is there any way to read a TIFF file using AIR ? Looks like
In C#/.NET (on Windows) is there a way to read a growing file using
I'm using a DataInputStream to read characters/data from a socket. I want to use
Is there a way to read a ByteBuffer with a BufferedReader without having to
Is there any way to read a file line by line in javascript, specifically
I have a TCP socket client receiving messages (data) from a server. messages are

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.