http://www.codeproject.com/KB/IP/SocketFileTransfer.aspx?artkw=socket%20send%20a%20file 我並不清楚地瞭解這一行: //獲取文件的大小第一指針在C++ - 需要解釋它是如何工作
cbLeftToReceive = sizeof(dataLength);
do
{
BYTE* bp = (BYTE*)(&dataLength) + sizeof(dataLength) - cbLeftToReceive;
cbBytesRet = sockClient.Receive(bp, cbLeftToReceive);
// test for errors and get out if they occurred
if (cbBytesRet == SOCKET_ERROR || cbBytesRet == 0)
{
int iErr = ::GetLastError();
TRACE("GetFileFromRemoteSite returned
a socket error while getting file length\n"
"\tNumber of Bytes received (zero means connection was closed) = %d\n"
"\tGetLastError = %d\n", cbBytesRet, iErr);
/* you should handle the error here */
bRet = FALSE;
goto PreReturnCleanup;
}
// good data was retrieved, so accumulate
// it with already-received data
cbLeftToReceive -= cbBytesRet;
}
while (cbLeftToReceive > 0);
我想知道如何得到它得到的文件大小到dataLength :)
此行:BYTE* bp = (BYTE*)(&dataLength) + sizeof(dataLength) - cbLeftToReceive;
這是正確的,bp是dataLength地址的一個字節指針,但+ sizeof(dataLength) - cbLeftToReceive
是什麼意思?
我不認爲該文件是那麼小:4個字節,只是onc接收他們如何接收dataLength和數據?它先發送dataLength,然後發送數據嗎?
你引用的代碼中最令人擔憂的部分是這個發送者和接收者都假定這個長度可以適合int。 (我仍然記得4GB的文件是無法想象的巨大的,現在我不認爲它是這樣的。) – 2010-10-23 08:43:33