Skip to content

Commit 7417f97

Browse files
leidwangYanVugenfirer
authored andcommitted
viosock: Enhance file transfer support in viosocklib-test
Improve viosocklib-test to handle large file transfers efficiently: - Increase buffer size from 4KB to 64KB for better performance - Replace single recv() call with streaming loop until connection close - Add proper file creation with CREATE_ALWAYS flag - Remove automatic "OK" response for ncat --vsock compatibility - Add progress reporting during file transfer - Enhance error handling and user feedback This enables reliable file transfers from Linux hosts using ncat --vsock without size limitations imposed by the previous 4KB buffer constraint. Signed-off-by: Leidong Wang <leidwang@redhat.com>
1 parent 3bdd826 commit 7417f97

1 file changed

Lines changed: 50 additions & 10 deletions

File tree

viosock/viosocklib-test/viosocklib-test.c

Lines changed: 50 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -429,20 +429,60 @@ int SocketListenTest(PSOCKADDR_VM addr, PTCHAR sFileName)
429429
}
430430
else
431431
{
432-
BYTE Buffer[0x1000];
433-
DWORD BufferLen = sizeof(Buffer);
434-
435-
_tprintf(_T("accepted from: %d:%d\n"), rAddr.svm_cid, rAddr.svm_port);
436-
437-
if (Recv(aSock, Buffer, &BufferLen) && BufferLen)
432+
BYTE Buffer[65536]; // Increase buffer size to 64KB
433+
DWORD totalBytes = 0;
434+
int bytesReceived;
435+
436+
_tprintf(_T("accepted from: %d:%d, receiving file...\n"), rAddr.svm_cid, rAddr.svm_port);
437+
438+
// Create or overwrite target file
439+
HANDLE hFile = CreateFile(sFileName,
440+
GENERIC_WRITE,
441+
0,
442+
NULL,
443+
CREATE_ALWAYS, // Overwrite existing file
444+
FILE_ATTRIBUTE_NORMAL,
445+
NULL);
446+
447+
if (hFile != INVALID_HANDLE_VALUE)
438448
{
439-
if (!AddBufferToFile(sFileName, Buffer, BufferLen))
449+
// Loop to receive data until connection closes
450+
while ((bytesReceived = recv(aSock, Buffer, sizeof(Buffer), 0)) > 0)
440451
{
441-
_tprintf(_T("AddBufferToFile failed: %d\n"), WSAGetLastError());
452+
DWORD bytesWritten;
453+
if (WriteFile(hFile, Buffer, bytesReceived, &bytesWritten, NULL))
454+
{
455+
totalBytes += bytesWritten;
456+
_tprintf(_T("Received %d bytes, total: %d bytes\r"), bytesWritten, totalBytes);
457+
458+
if (bytesWritten < (DWORD)bytesReceived)
459+
{
460+
_tprintf(_T("\nError: partial write to file. Disk may be full.\n"));
461+
break;
462+
}
463+
}
464+
else
465+
{
466+
_tprintf(_T("\nWriteFile failed: %d\n"), GetLastError());
467+
break;
468+
}
442469
}
443470

444-
BufferLen = sizeof("OK") - 1;
445-
Send(aSock, "OK", &BufferLen);
471+
CloseHandle(hFile);
472+
473+
if (bytesReceived == 0)
474+
{
475+
_tprintf(_T("\nFile transfer completed successfully!\n"));
476+
_tprintf(_T("Total bytes received: %d\n"), totalBytes);
477+
}
478+
else
479+
{
480+
_tprintf(_T("\nConnection error: %d\n"), WSAGetLastError());
481+
}
482+
}
483+
else
484+
{
485+
_tprintf(_T("CreateFile failed: %d\n"), GetLastError());
446486
}
447487

448488
shutdown(aSock, SD_BOTH);

0 commit comments

Comments
 (0)