Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 9 months ago.
Improve this question
In C (or C++) is there a way to receive data from a socket, but instead of reading it into a buffer in memory, it "receives" it into a file. I know that the usual way to do this is to receive the data into a char buffer, then write the buffer into a file.
Is there a function like sendfile(), which transports the data directly between files and sockets, but instead of sending data from a file to a socket, it receives data from the socket into a file?
There is no equivalent to sendfile that works the other way around. But there is splice() that transfers between 2 file descriptors, one of them must refer to a file.
So what you can do is socket -> pipe, pipe -> file. Whether that is still a gain over a buffer you have to measure. Splice is limited by the pipe buffer size while a read/write can work in arbitrary units reducing the number of syscalls.
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 months ago.
Improve this question
I am using a client socket to make a HTTP call to retrieve an image. Even though the recv call receives 36791 bytes, the buffer only has 4 bytes in the response body (the BUFF_SIZE has been set to 50000 for testing purposes). I have tried to make subsequent calls to recv but 0 bytes are returned from the subsequent calls. Would appreciate any help to understand why the buffer does not contain the full response from the socket recv as expected.
Issue was due to null-terminated strings as pointed out by #dewaffled, used std::string(buffer, total) as a solution to create a string that allows embedded null characters inspired by this post
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
Just wondering, what would be the most efficient way to write same data to two files, on linux and C/C++.
For example, this is the most trivial way.
while(1) {
... getting data from somewhere ....
write(fd1, data, datalen);
write(fd2, data, datalen);
}
However, the disadvantage is that kernel needs to copy data twice even though the data is same.
Any thoughts?
what would be the most efficient way to write same data to two files
Write the data to one file only.
Copy that file to another. Use an OS call to do that efficiently (Copy a file in a sane, safe and efficient way).
Another way for step 2 would be to create a hard link (check link()).
However, please watch out of not becomning a victim of premature optimization. I this is not the bottleneck in your program, then just use the trivial, easy-it-read approach.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I was reading http://www.cplusplus.com/reference/cstdio/fflush/ and I was curious about what it means. According to the website it says:
If the given stream was open for writing (or if it was open for updating and the last i/o operation was an output operation) any unwritten data in its output buffer is written to the file.
What does the output buffer to file mean?
Some streams buffer output data and do not write to the device immediately. fflush forces the contents of the stream's buffer, if there is one, to be written to the device and the buffer cleared.
Generally, when data is intended to be written to a file, it is stored in a construct known as a "buffer". Once the buffer has reached a certain threshold, all the data stored in the buffer will be written out to the file at once.
Fflush empties the buffer and forces all changes to be written to the file. This is particularly useful when you are done writing to the file, since it is good practice to flush the buffer before closing the file (thereby making sure that all data has been successfully written to the file).
This goes for other types of filestreams too.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
i have some idea about file remote location.Can i split a file in to chunks and use thread control split and socket to implement split data and send meanwhile ?
Besides, local can prefetch remote chunk and read it by two thread?
If you have one source of data and one sink I would assume that chunking won't help.
The IP-stack itself uses this method, so as long as you can provide data fast enough to the TCP stack (write buffer) and are fast enough reading the data from the buffer (read buffer) I think that is as fast as it gets...
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 8 years ago.
Improve this question
I dont know which reactor to choose use ace_select_reactor or ace_dev_poll_reactor.
The reactor is used for listening of incoming open connection events on a server which uses unix domain socket
from: *Dev_Poll__Reactor.html#details">http://www.riverace.com/ACE/ace55/html/ace/classACE_Dev_Poll_Reactor.html#_details*
The ACE_Dev_Poll_Reactor uses the /dev/poll' or '/dev/epoll' character devices to demultiplex events on a given set of file descriptors. Unlike select(),/dev/poll' and `/dev/epoll' have no hard-coded limit on the number of file descriptors that may be handled at any given time. As such, the ACE_Dev_Poll_Reactor can generally handle a much larger number of file descriptors than select() -based reactors.
but I have to notice that's only available certain UNIX platforms