Sending file contents to 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 the file back to the client, including newlines, using the send function. The way I tried it does not work. How should I do it?

+3
source share
3 answers

Reading a file and writing to a socket are two different operations. Winsock does not have an API to send a file directly.

As for reading a file, just make sure you open it in binary read mode if you use fopen, or just use the CreateFile and ReadFile Win32 API, and by default it will be binary.

You will usually read the file in chunks (for example, 10 KB at a time), and then send each of these fragments through a socket using send or WSASend . Once you are done, you can close the socket.

On the receiving side, read everything that is available on the socket until the socket is closed. When you read the data into the buffer, write the amount read into the file.

+3
source

... , Win32 - "sendfile" Linux. , memory-mapping ( , , ). , , , . , - "" async IO.

0

Source: https://habr.com/ru/post/1738906/


All Articles