Search code examples
c++winsockwinsock2winsockets

Sending files over TCP sockets C++ | Windows


I want to send files over TCP sockets in C++ on Windows, all is working absolutely fine, however I can't send big files like this, I understand that TCP as any protocol has it's limitations, like I can't send more than 64KB per packet, my method works for small file sizes(tested all up to 12KB), but I would like to send LARGE files, like iso image of ubuntu or windows, which are surely bigger than 12 fully packed packets and etc.

Server

int filesize = 0;
int err = recv(conn, (char*)&filesize, sizeof(filesize), 0);
if (err <= 0)
{
    printf("recv: %d\n", WSAGetLastError());
    clean(conn);
}
printf("recv %d bytes [OK]\n", err);

char* buffer = new char[filesize];
ZeroMemory(buffer, filesize);
err = recv(conn, buffer, filesize, MSG_WAITALL);
if (err <= 0)
{
    printf("recv: %d\n", WSAGetLastError());
    clean(conn);
}
printf("recv %d bytes [OK]\n", err);

ofstream file("a.txt", ios::binary);
file.write(buffer, filesize);
delete[] buffer;
file.close();

Client

ifstream file("a.txt", ios::binary);
file.seekg(0, ios::end);
int size = file.tellg();
file.seekg(0, ios::beg);
char* buffer = new char[size];
file.read(buffer, size);
file.close();

int* fsize = &size;
int err = send(client, (char*)fsize, sizeof(int), 0);
if (err <= 0)
{
    printf("send: %d\n", WSAGetLastError());
}
printf("send %d bytes [OK]\n", err);

err = send(client, buffer, size, 0);
if (err <= 0)
{
    printf("send: %d\n", WSAGetLastError());
}
printf("send %d bytes [OK]\n", err);
delete[] buffer;

All values for both sides are initialised, and error handling is done well, and if I had problem then I would have said about that. I decided to use MSG_WAITALL because I guess that is suitable for this case, please correct my code for recieving/sending and if possible refactor it, it would be nicer if it would be with explainations, so that evrybody could learn to code better, thanks)))


Solution

  • The one main point that should be taken away from the comments below your question is that send and recv are fickle. Just because you write send(buffer with 100 bytes) doesn't mean it's going to send 100 bytes. It could send 25 bytes, or 99 bytes, or fail out completely. It's up to you to take the return value and compute what needs to still be sent.

    Same goes with recv. If you write recv(buffer with 100 bytes) because you are expecting 100 bytes, it could only grab 25 bytes, or 99 bytes, or fail out completely. Again, it's up to you to use that return value and compute what still needs to be received.

    File I/O is completely different. If you want to write 100 bytes to a file, those 100 bytes are guaranteed to be written if the method doesn't fail. So, when folks who have worked with file I/O move to socket I/O usually end up confused why things aren't sending or receiving correctly.

    One of the trickier parts to socket programming is knowing how much data you will need to receive. You covered that by sending the length of the file first. The server will know to read in that value, then continue reading until that value is satisfied.

    Some protocols, like HTTP, will use delimiters (in HTTP's case \r\n\r\n) to signal when a packet of data has ended. So, as a socket programmer, you would recv on a loop until those 4 bytes are read.

    I put together an example on how you could accomplish sending and receiving a large file (this will handle files up to 9,223,372,036,854,775,807 in length). This isn't pure C++, I cheated in places because of lack of time. I used some Windows-only constructs for the same reason.

    So let's take a look at it:

    int64_t GetFileSize(const std::string& fileName) {
        // no idea how to get filesizes > 2.1 GB in a C++ kind-of way.
        // I will cheat and use Microsoft's C-style file API
        FILE* f;
        if (fopen_s(&f, fileName.c_str(), "rb") != 0) {
            return -1;
        }
        _fseeki64(f, 0, SEEK_END);
        const int64_t len = _ftelli64(f);
        fclose(f);
        return len;
    }
    
    ///
    /// Recieves data in to buffer until bufferSize value is met
    ///
    int RecvBuffer(SOCKET s, char* buffer, int bufferSize, int chunkSize = 4 * 1024) {
        int i = 0;
        while (i < bufferSize) {
            const int l = recv(s, &buffer[i], __min(chunkSize, bufferSize - i), 0);
            if (l < 0) { return l; } // this is an error
            i += l;
        }
        return i;
    }
    
    ///
    /// Sends data in buffer until bufferSize value is met
    ///
    int SendBuffer(SOCKET s, const char* buffer, int bufferSize, int chunkSize = 4 * 1024) {
    
        int i = 0;
        while (i < bufferSize) {
            const int l = send(s, &buffer[i], __min(chunkSize, bufferSize - i), 0);
            if (l < 0) { return l; } // this is an error
            i += l;
        }
        return i;
    }
    
    //
    // Sends a file
    // returns size of file if success
    // returns -1 if file couldn't be opened for input
    // returns -2 if couldn't send file length properly
    // returns -3 if file couldn't be sent properly
    //
    int64_t SendFile(SOCKET s, const std::string& fileName, int chunkSize = 64 * 1024) {
    
        const int64_t fileSize = GetFileSize(fileName);
        if (fileSize < 0) { return -1; }
    
        std::ifstream file(fileName, std::ifstream::binary);
        if (file.fail()) { return -1; }
    
        if (SendBuffer(s, reinterpret_cast<const char*>(&fileSize),
            sizeof(fileSize)) != sizeof(fileSize)) {
            return -2;
        }
    
        char* buffer = new char[chunkSize];
        bool errored = false;
        int64_t i = fileSize;
        while (i != 0) {
            const int64_t ssize = __min(i, (int64_t)chunkSize);
            if (!file.read(buffer, ssize)) { errored = true; break; }
            const int l = SendBuffer(s, buffer, (int)ssize);
            if (l < 0) { errored = true; break; }
            i -= l;
        }
        delete[] buffer;
    
        file.close();
    
        return errored ? -3 : fileSize;
    }
    
    //
    // Receives a file
    // returns size of file if success
    // returns -1 if file couldn't be opened for output
    // returns -2 if couldn't receive file length properly
    // returns -3 if couldn't receive file properly
    //
    int64_t RecvFile(SOCKET s, const std::string& fileName, int chunkSize = 64 * 1024) {
        std::ofstream file(fileName, std::ofstream::binary);
        if (file.fail()) { return -1; }
    
        int64_t fileSize;
        if (RecvBuffer(s, reinterpret_cast<char*>(&fileSize),
                sizeof(fileSize)) != sizeof(fileSize)) {
            return -2;
        }
    
        char* buffer = new char[chunkSize];
        bool errored = false;
        int64_t i = fileSize;
        while (i != 0) {
            const int r = RecvBuffer(s, buffer, (int)__min(i, (int64_t)chunkSize));
            if ((r < 0) || !file.write(buffer, r)) { errored = true; break; }
            i -= r;
        }
        delete[] buffer;
    
        file.close();
    
        return errored ? -3 : fileSize;
    }
    

    Sending and Receiving Buffers

    At the top we have two methods that works with buffers in memory. You can send it any buffer at any size (stay reasonable here), and those methods will send and receive until all the bytes passed in have been transmitted.

    This does what I was talking about above. It takes the buffer and loops until all the bytes have been successfully sent or received. After these methods complete, you are guaranteed that all data is transmitted (as long as the return value is zero or positive).

    You can define a "chunk size" which is the default size of the chunks of data the methods will use to send or receive data. I am sure these can be optimized by using more suitable values than what they are currently set at, but I don't know what those values are. It's safe to leave them at the default. I don't think that with the speed of today's computers you will notice too much of a difference if you change it to something else.

    Sending and Receiving Files

    The code for doing files is almost identical in nature to the buffer code. Same idea, except now we can assume that if the return value is greater than zero from the buffer methods then it was successful. So the code is a little simpler. I use a chunk size of 64KB... for no special reason. This time the chunk size determines how much data is read from the file I/O operations, not the sockets I/O.

    Test Server and Client

    Just to be complete, I used this code below to test this with a 5.3 GB file I have on disk. I basically just re-wrote Microsoft's client/server examples in a very slimmed down way.

    #pragma comment(lib, "Ws2_32.lib")
    #include <iostream>
    #include <winsock2.h>
    #include <ws2tcpip.h>
    #include <fstream>
    
    DWORD __stdcall ClientProc(LPVOID param) {
    
        struct addrinfo hints = { 0 }, * result, * ptr;
        hints.ai_family = AF_UNSPEC;
        hints.ai_socktype = SOCK_STREAM;
        hints.ai_protocol = IPPROTO_TCP;
    
        if (getaddrinfo("127.0.0.1", "9001", &hints, &result) != 0) {
            return ~0;
        }
    
        SOCKET client = INVALID_SOCKET;
        for (ptr = result; ptr != NULL; ptr = ptr->ai_next) {
            client = socket(ptr->ai_family, ptr->ai_socktype, ptr->ai_protocol);
            if (client == SOCKET_ERROR) {
                // TODO: failed (don't just return, cleanup)
            }
            if (connect(client, ptr->ai_addr, (int)ptr->ai_addrlen) == SOCKET_ERROR) {
                closesocket(client);
                client = INVALID_SOCKET;
                continue;
            }
            break;
        }
        freeaddrinfo(result);
    
        if (client == SOCKET_ERROR) {
            std::cout << "Couldn't create client socket" << std::endl;
            return ~1;
        }
    
        int64_t rc = SendFile(client, "D:\\hugefiletosend.bin");
        if (rc < 0) {
            std::cout << "Failed to send file: " << rc << std::endl;
        }
    
        closesocket(client);
    
        return 0;
    }
    
    int main()
    {
        WSADATA wsaData;
        WSAStartup(MAKEWORD(2, 2), &wsaData);
    
        {
            struct addrinfo hints = { 0 };
            hints.ai_family = AF_INET;
            hints.ai_socktype = SOCK_STREAM;
            hints.ai_protocol = IPPROTO_TCP;
            hints.ai_flags = AI_PASSIVE;
    
            struct addrinfo* result = NULL;
            if (0 != getaddrinfo(NULL, "9001", &hints, &result)) {
                // TODO: failed (don't just return, clean up)
            }
    
            SOCKET server = socket(result->ai_family, result->ai_socktype, result->ai_protocol);
            if (server == INVALID_SOCKET) {
                // TODO: failed (don't just return, clean up)
            }
    
            if (bind(server, result->ai_addr, (int)result->ai_addrlen) == INVALID_SOCKET) {
                // TODO: failed (don't just return, clean up)
            }
            freeaddrinfo(result);
    
            if (listen(server, SOMAXCONN) == SOCKET_ERROR) {
                // TODO: failed (don't just return, clean up)
            }
    
            // start a client on another thread
            HANDLE hClientThread = CreateThread(NULL, 0, ClientProc, NULL, 0, 0);
    
            SOCKET client = accept(server, NULL, NULL);
    
            const int64_t rc = RecvFile(client, "D:\\thetransmittedfile.bin");
            if (rc < 0) {
                std::cout << "Failed to recv file: " << rc << std::endl;
            }
    
            closesocket(client);
            closesocket(server);
    
            WaitForSingleObject(hClientThread, INFINITE);
            CloseHandle(hClientThread);
        }
        WSACleanup();
        return 0;
    }