Reputation: 725
I wish to make a file transfer using sockets from client to server using C++ language...
The code I have only transfers strings to client and server.
How can I transfer files? Any help or reference materials would also help.
Upvotes: 0
Views: 3217
Reputation:
You can also look at CSocketFile As per MSDN
Class CSocketFile derives from CFile, but it does not support CFile member functions such as the positioning functions (Seek, GetLength, SetLength, and so on), the locking functions (LockRange, UnlockRange), or the GetPosition function. All the CSocketFile object must do is write or read sequences of bytes to or from the associated CSocket object.
Upvotes: 0
Reputation: 3481
If a Winsock-specific solution is okay with you, take a look at the TransmitFile()
function. Linux and Solaris both have a sendfile()
function that perform in a similar way, although I believe Linux and Solaris have a slightly different sendfile()
API. These functions provide the added benefit of not having to copy contents of the file into your address space.
Otherwise there are several options including but not limited to the following:
sendfile()
and TransmitFile()
functions would still be faster, however. As always, profile your code.Another thing you might want to think about is whether or not you want the socket write operation to be blocking or non-blocking, and similarly on the receiving end. Non-blocking IO will require you to use your platform's event demultiplexing mechanism (e.g. select()
on POSIX platforms).
Boost.Asio would likely greatly simplify your task, as well. I'd recommend using it over the native APIs if at all possible.
HTH!
Upvotes: 2
Reputation: 2065
Turn the file to a byte stream, and send this across the socket and read it as a bytestream on the server.
Upvotes: 2