How to move a file in C++ after reading it via send? - c++

Well, I've see this question about my problem and the solution seems to be good.
Send and Receive a file in socket programming in Linux with C/C++ (GCC/G++)
Well. My goal is to receive a txt file and after receiving it, I want to store it into a path.
I have now a question:
when I receive the bytes through the recv function, how can I merge all bytes into a txt file?

I think std::ofstream is what you are looking for.

Related

How to stream a file continuously with boost::beast

I have a local file which some process continuously appends to. I would like to serve that file with boost::beast.
So far I'm using boost::beast::http::response<boost::beast::http::file_body> and boost::beast::http::async_write to send the file to the client. That works very well and it is nice that boost::beast takes care of everything. However, when the end of the file is reached, it stops the asynchronous writing. I assume that is because is_done of the underlying serializer returns true at this point.
Is it possible to keep the asynchronous writing ongoing so new contents are written to the client as the local file grows (similar to how tail -f would keep writing the file's contents to stdout)?
I've figured that I might need to use boost::beast::http::response_serializer<boost::beast::http::file_body> for that kind of customization but I'm not sure how to use it correctly. And do I need to use chunked encoding for that purpose?
Note that keeping the HTTP connection open is not the problem, only writing further output as soon as the file grows.
After some research this problem seems not easily solvable, at least not under GNU/Linux which I'm currently focusing on.
It is possible to use chunked encoding as described in boost::beast's documentation. I've implemented serving chunks asynchronously from file contents which are also read asynchronously with the help of boost::asio::posix::stream_descriptor. That works quite well. However, it also stops with an end-of-file error as soon as the end of the file is reached. When using async_wait via the descriptor I'm getting the error "Operation not supported".
So it simply seems not possible to asynchronously wait for more bytes to be written to a file. That's strange considering tail -f does exactly that. So I've straceed tail -f and it turns out that it calls inotify_add_watch(4, "path_to_file", IN_MODIFY). Hence I assume one actually needs to use inotify to implement this.
For me it seems easier and more efficient to take control over the process which writes to the file so far to let it prints to stdout. Then I can stream the pipe (similarly to how I've attempted streaming the file) and write the file myself.
However, if one wanted to go down the road, I suppose using inotify and boost::asio::posix::stream_descriptor is the answer to the question, at least under GNU/Linux.

TCP File transfer with htonl & htons

I am doing a file transfer program in c++ right now. It works but the only problem is I am using getline instead of read(). So the program seems to only do text files. I read in the file and store in in a char*[]. So I was wondering when I change it to read() in binary and want to send it through the sockets, do I need to do a conversion(htons,htonl, etc) on the char*[] before I pass it over? I read somewhere that you don't need to do that if it's a char*. Am I mistaken in how I am storing the information from the file?
to sum up:
How do I store information read in from a binary opened file?
If it is stored in a char*[], do I need to use htons... on it, and how?
how many bytes would I send over the sockets at a time?
Thanks very much for your any help everyone!
No, you do not need to convert the file data, send/read it as-is.
What you should do, however, is assign the file size to a variable, convert it with htonl(), and send it before then sending the file data. Then the receiver can read the file size, convert it with ntohl(), and know how many bytes to read for the file data so it knows when to stop reading and whether the full file was received or not.

Way to check in C/C++ if a file is in use?

Is there any way to check if a file is in use in C/C++? Or do I have to ALWAYS implement a lock/semaphore to prevent simultaneous access of any file by multiple threads/processes?
If we consider Linux, and the following scenario: I want to transfer,in chunks, the contents of a file stored in device A to another device B through RS-232 communication, using a pre-defined communication framework. When the request for this transfer comes, I want to verify the file is NOT being used by any process in device A, before sending a "Ready to Transfer : OK" response, following which I will start reading and transmitting the data in chunks.
Is there a way to check file if is already in use without doing fopen/fclose on the said file?
actually
fopen();
is the best way to find this out.
Do fopen() on the receiving end, if it is successful, send the "OK to receive" message.

Sending the contents of a file to a client

I am writing a C++ server side application called quote of the day. I am using the winsock2 library. I want to send the contents of a file back to the client, including newlines by using the send function. The way i tried it doesn't work. How would i go about doing this?
Reading the file and writing to the socket are 2 distinct operations. Winsock does not have an API for sending a file directly.
As for reading the file, simply make sure you open it in read binary mode if using fopen, or simply use the CreateFile, and ReadFile Win32 API and it will be binary mode by default.
Usually you will read the file in chunks (for example 10KB at a time) and then send each of those chunks over the socket by using send or WSASend. Once you are done, you can close the socket.
On the receiving side, read whatever's available on the socket until the socket is closed. As you read data into a buffer, write the amount read to a file.
Hmm... I think Win32 should have something similar to "sendfile" in Linux.
If it doesn't you still can use memory-mapping (but, don't forgot to handle files with size larger than available virtual address space). You probably will need to use blocking sockets to avoid returning to application until all data is consumed. And I think there was something with "overlapped" operation to implement async IO.
I recommend dropping winsock and instead using something more modern such as Boost.Asio:
http://www.boost.org/doc/libs/1_37_0/doc/html/boost_asio/tutorial.html
There is also an example on transmitting a file:
http://www.boost.org/doc/libs/1_37_0/doc/html/boost_asio/examples.html

How do I convert an image into a buffer so that I can send it over using socket programming? C++

How do I convert an image into a buffer so that I can send it over using socket programming? I'm using C++ socket programming. I am also using QT for my Gui.
Also, after sending it over through the socket, how do I put that image back together so it can become a file again?
If someone could put up some sample code that would be great :)
I was looking around and found something about rfile but still not going through my head.
tyty
If you're using Qt, use a QImageWriter and a QImageReader and let them deal directly with the socket, thus avoiding the need for a temporary file.
Assuming that you want to send an image as a file,
here is pseudocode to send it through a TCP connection.
Client A
Set up a connection
Open a file in binary mode
Loop:
read/load N bytes in a buffer
send(buffer)
Close file
Close connection
Client B
Listen and accept a connection
Create a new file in binary mode
Loop:
n = read(buffer)
write n bytes in the file
Close file
Close connection
I figured this out as well and wrote down what I did. You can find the description here: http://hanckmann.net/?q=node/44. It is fine to download and use the examples there!