TransmitFile一次又一次地发送相同的字节

时间:2018-06-05 18:02:07

标签: c++ winsock winsock2

我使用了TransmitFile功能,并且做了与我预期不同的事情。

最小例子:

NgModule

使用MinGW构建:#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); }

文件g++ main.cpp -lws2_32 -lmswsock必须存在且包含:

alphabet.txt

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

abcdefghijklmnopqrstuvwxyz

我希望:

abcdefdefdefdefdef

1 个答案:

答案 0 :(得分:1)

使用您的确切代码,我能够重现该问题。在每次循环迭代中使用SetFilePointer(),我可以看到文件位置在第一次发送后按预期进入偏移量3,但在后续发送时从不超过偏移量3,因此相同的3个字节将重新传输过度。为什么它以这种方式工作,我不知道。

通过将TransmitFile()结构传递给TransmitFile(),我能够让OVERLAPPED发送正确的文件块(不管传递给TransmitFile()的标志)(指定{ FILE_FLAG_OVERLAPPED CreateFile()上的{1}},documentation符合symfony routing

  

您可以使用lpOverlapped参数通过设置{OffsetOffsetHigh OVERLAPPED成员来指定文件中开始文件数据传输的64位偏移量。 1}}结构。

尝试这样的事情:

#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,而是在每次发送后手动提前调整文件位置,而不是使用文档:

  

如果lpOverlapped是NULL指针,则数据传输始终从文件中的当前字节偏移量开始。

#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;
}