TCP文件传输,源码,请帮忙找下错 - C++ Builder / Windows SDK/API
/****************文件数据接收线程******************/
void Thread5 ( PVOID pvoid )
{
int iiGet,iiRecd;
static char recvBuff[RECV_BUFFER_SIZE*4];
do
{
iiGet =( ToReceive<(RECV_BUFFER_SIZE*4) )? ToReceive : (RECV_BUFFER_SIZE*4) ;
iiRecd=recv(tcp_client,recvBuff,iiGet,0);
recvBuff[iiRecd]='\0';
if (0!=iiRecd)
{
WriteFile(recvhFile,(LPVOID)recvBuff, iiGet,&dwBytesWrite,NULL);
if (0!=dwBytesWrite)
{
SendMessage(svProWnd1,PBM_STEPIT,(WPARAM)1,0);
}
iiRecd-=dwBytesWrite;
ToReceive -= dwBytesWrite;
}
else if (SOCKET_ERROR==iiRecd)
{
lstrcat (hisbuff,"\r\n");
lstrcat (hisbuff,"文件数据接收错误.....");
lstrcat (hisbuff,"\r\n");
SendMessage(showhwnd, EM_REPLACESEL, 0, (LPARAM) hisbuff) ;
SendMessage(hishwnd, EM_REPLACESEL, 0, (LPARAM) hisbuff) ;
iiRecd=0;
// setRecvZero();
SendMessage(recvDlg,WM_CLOSE,0,0);
}
}while(ToReceive>0);
if (ToReceive==0)
{
allFile--;
SendMessage(svProWnd2,PBM_STEPIT,(WPARAM)1,0);
CloseHandle(recvhFile);
}
if (allFile==0)
{
GetLocalTime(&sys);
sprintf_s(time,sizeof(time),"%2d:%2d:%2d",sys.wHour,sys.wMinute,sys.wSecond);
lstrcpy (hisbuff,time);
lstrcat (hisbuff,"\r\n");
lstrcat (hisbuff,"文件接收完毕......");
lstrcat (hisbuff,"\r\n");
SendMessage(showhwnd, EM_REPLACESEL, 0, (LPARAM) hisbuff) ;
SendMessage(hishwnd, EM_REPLACESEL, 0, (LPARAM) hisbuff) ;
process=TRUE;
SendMessage(recvDlg,WM_CLOSE,0,0);
}
closesocket(tcp_client);
_endthread () ;
}
/******************文件数据发送线程*****************/
void Thread6 ( PVOID pvoid )
{
int sendNum=0;
int k,l;
int sendthistime;
HANDLE sendhFile;
int buffOffset;
char errorCode[30];
static char lpBufPtr[SEND_BUFFER_SIZE*4]; //读入文件的缓冲区
toSendFileSize=0;
sendhFile=CreateFile(pathFile,GENERIC_READ,FILE_SHARE_READ,NULL,OPEN_EXISTING,FILE_ATTRIBUTE_READONLY,(HANDLE)NULL);
k=GetLastError();
toSendFileSize=GetFileSize(sendhFile,NULL);
if(sendhFile!=INVALID_HANDLE_VALUE)
{
do
{
//把文件内容读入缓冲区
memset(lpBufPtr,0,sizeof(lpBufPtr));
sendthistime=ReadFile(sendhFile,(LPVOID)lpBufPtr,SEND_BUFFER_SIZE*4,&dwBytesRead,NULL);
lpBufPtr[dwBytesRead]='\0';
buffOffset=0;
//把内容装入多行编辑控件中
if(dwBytesRead!=0)
{
do
{
sendda=send(tcp_client,(const char*)(lpBufPtr+buffOffset),dwBytesRead,0);
l=GetLastError();
sendNum+=sendda;
if(sendda==SOCKET_ERROR)
{
lstrcat (hisbuff,"\r\n");
sprintf_s(errorCode,sizeof(errorCode),"文件数据发送错误,请重新发送.错误代码:%d",l);
lstrcat (hisbuff,errorCode);
lstrcat (hisbuff,"\r\n");
SendMessage(showhwnd, EM_REPLACESEL, 0, (LPARAM) hisbuff) ;
SendMessage(hishwnd, EM_REPLACESEL, 0, (LPARAM) hisbuff) ;
nextFile=TRUE;
//setZero();
sendda=0;
SendMessage(hDialog,WM_CLOSE,0,0);
}
buffOffset+=(sendda); //判断下次发送数据的指针
(int)dwBytesRead-=(sendda); //判断有无将所读取的内容发送完毕
toSendFileSize-=(sendda); //判断文件是否发送完毕
}while(dwBytesRead>0);
SendMessage(sendWnd,PBM_STEPIT,(WPARAM)1,0);
}
else
{
MessageBox(hWnd,TEXT("读入字节数为0 "),NULL,MB_OK|MB_ICONEXCLAMATION);
}
}while(toSendFileSize>0);
if (toSendFileSize==0)
{
SendMessage(recvWnd,PBM_STEPIT,(WPARAM)1,0);
sendNumFile--;
nextFile=TRUE; //是否允许发送下一个文件 BOOL
}
if (sendNumFile==0)
{
GetLocalTime(&sys);
sprintf_s(time,sizeof(time),"%2d:%2d:%2d",sys.wHour,sys.wMinute,sys.wSecond);
lstrcpy (hisbuff,time);
lstrcat (hisbuff,"\r\n");
lstrcat (hisbuff,"文件发送完毕......");
lstrcat (hisbuff,"\r\n");
SendMessage(showhwnd, EM_REPLACESEL, 0, (LPARAM) hisbuff) ;
SendMessage(hishwnd, EM_REPLACESEL, 0, (LPARAM) hisbuff) ;
showDialog=TRUE;
sendcess=TRUE;
SendMessage(hDialog,WM_CLOSE,0,0);
}
}
else
{
MessageBox(hWnd,TEXT("发送文件:打开文件失败! "),NULL,MB_OK|MB_ICONEXCLAMATION);
SendMessage(hDialog,WM_CLOSE,0,0);
}
CloseHandle(sendhFile);
closesocket(tcp_client);
_endthread () ;
}
文件发送,接收各有一线程负责;
为什么在发送,接收时,有时能成功,有时失败;
文件大小都能保证,但是,有时文件却打不开,如一rar文件,不能解压;
或一rmvb不能正常播放
[解决办法]
//这里实际接收到的字节数为iiRecd
iiRecd=recv(tcp_client,recvBuff,iiGet,0);
recvBuff[iiRecd]='\0';
if (0!=iiRecd)
{
//写入文件时就应该也是iiRecd
WriteFile(recvhFile,(LPVOID)recvBuff, iiGet,&dwBytesWrite,NULL);