ソケットを介したファイルの送受信に問題があります。例 - 小さなビデオ (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 バイト)
recv を実行し、サーバーとの間で応答を送信する SyncGetData (1 バイト)
また、受信したファイルのサイズは元のファイルのサイズと一致しました。