我用ACE写了个简单的网络文件传输模块,发送端用WINDOWS API "TransmitFile()"发送文件,接受端用ACE_Sock_Stream::recv_n接受文件,大体代码如下:
// 发送端
//..............
OVERLAPPED ov;
memset(&ov,0,sizeof(OVERLAPPED));
HANDLE hEvent = CreateEvent(0,FALSE,FALSE,0);
ov.hEvent = hEvent;
TransmitFile((SOCKET)pRemoteStream->get_handle(),hFile,0,everySendBytes,&ov,0,TF_USE_KERNEL_APC);
int waitRet = WaitForSingleObject(hEvent,maxWaitTime);
CloseHandle(hEvent);
if(waitRet == WAIT_TIMEOUT)
{
CloseHandle(hFile);
return FALSE;
}
//..............
// 接受端
//..............
BOOL bRet;
BYTE * pBuffer = new BYTE[fileSize]; // fileSize问发送端发送的文件的大小
ACE_OS::last_error(0);
int recvBytes = pRemoteStream->recv_n(pBuffer,fileSize,0,&ACE_Time_Value(maxWaitSeconds));
int lastError = ACE_OS::last_error();
if(recvBytes == fileSize)
{
bRet = TRUE;
}
else
{
if(lastError == ETIME)
{
// 这个错误经常会发生,不知道是怎么回事
TRACE(_T("data is not received all for out of time : %dsc, %dbytes,%dbytes"),maxWaitSeconds,recvBytes,fileSize);
}
else
{
TRACE(_T("data is not received all for other reason,errorCode : %d"),lastError);
}
bRet = FALSE;
}
//..............
但我写的程序在接受端经常会超时而没有接受到完成的文件数据,我也不知道怎么回事,现在有谁能告诉我ACE有无设计好的端对端文件传输框架? |