gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 05:00:35 28 4
gpt4 key购买 nike

我正在使用 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 时,我得到:

abcdefdefdefdefdef

我希望:

abcdefghijklmnopqr

最佳答案

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

我能够得到 TransmitFile()通过传递 TransmitFile() 来发送正确的文件 block (无论传递给 OVERLAPPED 的标志如何)结构为 TransmitFile() (在 FILE_FLAG_OVERLAPPED 上指定 CreateFile() 与否,没有区别),根据 documentation :

You can use the lpOverlapped parameter to specify a 64-bit offset within the file at which to start the file data transfer by setting the Offset and OffsetHigh member of the OVERLAPPED structure.

尝试这样的事情:

#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 ,我也是为了让代码正常工作,只需在每次发送后手动推进文件位置,再次根据文档:

If lpOverlapped is a NULL pointer, the transmission of data always starts at the current byte offset in the file.

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

关于c++ - TransmitFile 一次又一次地发送相同的字节,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50706315/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com