我正在使用TransmitFile函数,它所做的事情与我预期的有所不同。

最小示例:

#include <mswsock.h>

int main(void)
{
    WSADATA wsa;
    SOCKET ListenSocket, ClientSocket;
    struct sockaddr_in server;

    if (WSAStartup(MAKEWORD(2,2),&wsa) != 0)
        return 1;

    if ((ListenSocket = socket(AF_INET , SOCK_STREAM , 0 )) == INVALID_SOCKET)
        return 2;

    server.sin_family = AF_INET;
    server.sin_addr.s_addr = INADDR_ANY;
    server.sin_port = htons(1234);

    if (bind(ListenSocket, (struct sockaddr *)&server, sizeof(server)) == SOCKET_ERROR)
        return 3;

    listen(ListenSocket, 1);

    if ((ClientSocket = accept(ListenSocket, NULL, NULL)) == INVALID_SOCKET)
        return 4;

    HANDLE hFile = CreateFile("alphabet.txt", GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);

    do {
        if (!TransmitFile(ClientSocket, hFile, 3, 0, NULL, NULL, 0))
            return 5;
        Sleep(1000);
    } while (1);
}

使用MinGW构建:g++ main.cpp -lws2_32 -lmswsock
文件alphabet.txt必须存在并且包含:
abcdefghijklmnopqrstuvwxyz

当我运行程序并连接(例如使用PuTTY或netcat)到端口1234时,我得到:



我期望:

最佳答案

使用您的确切代码,我能够重现该问题。在每个循环迭代中使用SetFilePointer(),我可以看到在第一次发送后文件位置按预期的那样前进到偏移量3,但是在以后的发送中永远不会超过偏移量3,因此相同的3个字节一遍又一遍地重传。为什么这样工作,我不知道。

通过按documentationTransmitFile()结构传递给TransmitFile()(是否在OVERLAPPED上指定TransmitFile(),这没有区别),我能够通过传递FILE_FLAG_OVERLAPPED来发送正确的文件块(无论传递给CreateFile()的标志如何)(没有区别):



尝试这样的事情:

#include <mswsock.h>

int main(void)
{
    WSADATA wsa;
    SOCKET ListenSocket, ClientSocket;
    struct sockaddr_in server = {};
    HANDLE hFile;

    if (WSAStartup(MAKEWORD(2,2), &wsa) != 0)
        return 1;

    if ((ListenSocket = socket(AF_INET, SOCK_STREAM, 0)) == INVALID_SOCKET)
        return 2;

    server.sin_family = AF_INET;
    server.sin_addr.s_addr = INADDR_ANY;
    server.sin_port = htons(1234);

    if (bind(ListenSocket, (struct sockaddr *)&server, sizeof(server)) == SOCKET_ERROR)
        return 3;

    if (listen(ListenSocket, 1) == SOCKET_ERROR)
        return 4;

    if ((ClientSocket = accept(ListenSocket, NULL, NULL)) == INVALID_SOCKET)
        return 5;

    if ((hFile = CreateFileA("alphabet.txt", GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL)) == INVALID_HANDLE_VALUE)
        return 6;

    ULARGE_INTEGER ul;
    ul.LowPart = GetFileSize(hFile, &ul.HighPart);

    if ((ul.LowPart == INVALID_FILE_SIZE) && (GetLastError() != 0))
        return 7;

    OVERLAPPED ov = {};
    ov.hEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
    if (!ov.hEvent)
        return 8;

    unsigned __int64 uiPos = 0;
    unsigned __int64 uiRemaining = ul.QuadPart;

    while (uiRemaining > 0)
    {
        ul.QuadPart = uiPos;

        ov.Offset = ul.LowPart;
        ov.OffsetHigh = ul.HighPart;

        DWORD dwNumToSend = (uiRemaining >= 3) ? 3 : (DWORD)uiRemaining;

        if (!TransmitFile(ClientSocket, hFile, dwNumToSend, 0, &ov, NULL, 0))
        {
            if ((GetLastError() != ERROR_IO_PENDING) && (WSAGetLastError() != WSA_IO_PENDING))
                break;

            WaitForSingleObject(ov.hEvent, INFINITE);
        }

        uiPos += dwNumToSend;
        uiRemaining -= dwNumToSend;

        Sleep(1000);
    }

    closesocket(ClientSocket);
    CloseHandle(ov.hEvent);
    CloseHandle(hFile);

    return 0;
}

另外,除了使用OVERLAPPED之外,我还通过在每次发送后手动按文件再次手动推进文件位置来使代码正确运行:


#include <mswsock.h>

int main(void)
{
    WSADATA wsa;
    SOCKET ListenSocket, ClientSocket;
    struct sockaddr_in server = {};
    HANDLE hFile;

    if (WSAStartup(MAKEWORD(2,2), &wsa) != 0)
        return 1;

    if ((ListenSocket = socket(AF_INET, SOCK_STREAM, 0)) == INVALID_SOCKET)
        return 2;

    server.sin_family = AF_INET;
    server.sin_addr.s_addr = INADDR_ANY;
    server.sin_port = htons(1234);

    if (bind(ListenSocket, (struct sockaddr *)&server, sizeof(server)) == SOCKET_ERROR)
        return 3;

    if (listen(ListenSocket, 1) == SOCKET_ERROR)
        return 4;

    if ((ClientSocket = accept(ListenSocket, NULL, NULL)) == INVALID_SOCKET)
        return 5;

    if ((hFile = CreateFileA("alphabet.txt", GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL)) == INVALID_HANDLE_VALUE)
        return 6;

    ULARGE_INTEGER ul;
    ul.LowPart = GetFileSize(hFile, &ul.HighPart);

    if ((ul.LowPart == INVALID_FILE_SIZE) && (GetLastError() != 0))
        return 7;

    unsigned __int64 uiPos = 0;
    unsigned __int64 uiRemaining = ul.QuadPart;

    while (uiRemaining > 0)
    {
        ul.QuadPart = uiPos;
        if ((SetFilePointer(hFile, (LONG)ul.LowPart, (PLONG)&ul.HighPart, FILE_BEGIN) == INVALID_SET_FILE_POINTER) && (GetLastError() != 0))
            break;

        DWORD dwNumToSend = (uiRemaining >= 3) ? 3 : (DWORD)uiRemaining;

        if (!TransmitFile(ClientSocket, hFile, dwNumToSend, 0, NULL, NULL, 0))
            break;

        uiPos += dwNumToSend;
        uiRemaining -= dwNumToSend;

        Sleep(1000);
    }

    closesocket(ClientSocket);
    CloseHandle(hFile);

    return 0;
}

10-04 14:12