我在通过套接字发送/接收文件时遇到问题。例如 - 在小视频 (440 Mb) 中,整个视频都存在干扰(绿色噪声)。
为什么会这样?
// server side
bool ClientThread::GetFile(char* path, LPDWORD fileSize, LPDWORD bytes)
{
FileInfo* fileInfo = new FileInfo();
if (!m_client->SyncGetRequest(fileInfo))
{
delete fileInfo;
return false;
}
*fileSize = fileInfo->parts * fileInfo->partSize + fileInfo->remain;
HANDLE hFile = CreateFile(path, GENERIC_READ | GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
if (hFile == INVALID_HANDLE_VALUE)
{
printf("Creating file is failed!\n");
delete fileInfo;
return false;
}
HANDLE hMap = CreateFileMapping(hFile, NULL, PAGE_READWRITE, 0, *fileSize, 0);
if (!hMap)
{
printf("Creating file mapping is failed!\n");
CloseHandle(hFile);
delete fileInfo;
return false;
}
void* ptr = MapViewOfFile(hMap, FILE_MAP_READ | FILE_MAP_WRITE, 0, 0, *fileSize);
if (!ptr)
{
printf("Map View of file has been failed!\n");
CloseHandle(hMap);
CloseHandle(hFile);
delete fileInfo;
return false;
}
for (int i = 0; i < fileInfo->parts; i++)
{
int received = m_client->SyncGetRequest((byte*)((DWORD)ptr + i * fileInfo->partSize), fileInfo->partSize);
if (received != fileInfo->partSize)
printf("WARNING! Byte mismatch detected! Part: %d\n", i + 1);
if (received <= 0)
{
printf("File receiving is failed! Part: %d\n", i + 1);
UnmapViewOfFile(ptr);
CloseHandle(hMap);
CloseHandle(hFile);
delete fileInfo;
return false;
}
*bytes += received;
}
if (fileInfo->remain > 0)
{
int received = m_client->SyncGetRequest((byte*)((DWORD)ptr + fileInfo->parts * fileInfo->partSize), fileInfo->remain);
*bytes += received;
}
UnmapViewOfFile(ptr);
CloseHandle(hMap);
CloseHandle(hFile);
delete fileInfo;
printf("File successfully received!\n");
return true;
}
// client side
void Client::SendFile(HANDLE hFile)
{
DWORD partSize = 4096;
DWORD size = GetFileSize(hFile, NULL);
int parts = size / partSize;
int remain = size - parts * partSize;
FileInfo fInfo = { parts, partSize, remain };
if (!SyncSendRequest(&fInfo))
return;
HANDLE hMap = CreateFileMapping(hFile, NULL, PAGE_READONLY, 0, 0, 0);
if (hMap)
{
void* ptr = MapViewOfFile(hMap, FILE_MAP_READ, 0, 0, 0);
if (ptr)
{
for (int i = 0; i < parts; i++)
{
if (!SyncSendRequest((byte*)((DWORD)ptr + i * partSize), partSize))
{
printf("Error sending file! Part: %d\n", i + 1);
break;
}
}
if (remain > 0)
{
SyncSendRequest((byte*)((DWORD)ptr + parts * partSize), remain);
}
UnmapViewOfFile(ptr);
}
CloseHandle(hMap);
}
printf("File is sent!\n");
}
SyncSendData 从/到服务器发送和接收响应(1 字节)
SyncGetData 进行接收并从/向服务器发送响应(1 字节)
并且接收文件的大小与原始文件的大小相匹配。