|
客户端发送文件的函数:
void CPclientDlg::OnFileSend()
{
// TODO: Add your control notification handler code here
CString str;
sendFile.Abort();
sendFile.Open(sendFileName,CFile::modeRead | CFile::typeBinary);
sendFileLen = sendFile.GetLength();
CPclientDlg *dlg = (CPclientDlg*)AfxGetApp()->GetMainWnd();
str.Format("%d",sendFileLen);
dlg->MessageBox(str);
int size=8100;
while(sendFileLen>size)
{
char *membuf=new char[size+2] ;
sendFile.Read(membuf,size);
membuf[size] = '%';
membuf[size+1] = '\0';
HA_Proactive_Service *curSvr = HA_Proactive_Service::GetObj();
curSvr->WriteDate(membuf,strlen(membuf));
sendFileLen = sendFileLen-size;
LocalFree(membuf);
}
char *membuf = new char[sendFileLen+2];
sendFile.Read(membuf,sendFileLen);
sendFile.Close();
membuf[sendFileLen] = '^';
membuf[sendFileLen+1] = '\0';
HA_Proactive_Service *curSvr = HA_Proactive_Service::GetObj();
curSvr->WriteDate(membuf,strlen(membuf));
LocalFree(membuf);
MessageBox("文件已发送");
}
int WriteDate(char *szSendBuf, int nSendCnt)
{
ACE_Message_Block *smb = new ACE_Message_Block(nSendCnt+1);
smb->copy(szSendBuf);
CPclientDlg *dlg = (CPclientDlg*)AfxGetApp()->GetMainWnd();
CString str;
str.Format("%d",smb->length());
dlg->MessageBox(str);
int nResult = this->writer_.write(*smb,smb->length());
if ( nResult != 0)
{
ACE_TRACE("Write data failed!");
}
return nResult;
}
服务器在open函数中初始了
ACE_Message_Block *mb;
ACE_NEW_NORETURN(mb,ACE_Message_Block(8102));
服务器接受文件的函数:
virtual void handle_read_stream
(const ACE_Asynch_Read_Stream::Result &result)
{
ACE_Message_Block &mb = result.message_block ();
if (!result.success () || result.bytes_transferred () == 0)
{
mb.release ();
delete this;
return;
}
CPserverDlg *dlg=(CPserverDlg*)AfxGetApp()->GetMainWnd();
// mb.copy("\0"); //为字符串添加结束标记'\0'
char *buffer = mb.rd_ptr();
int length= mb.length()-1;
CString str1;
str1.Format("%d",length);
dlg->MessageBox(str1);
if(buffer[length-1] == [email=]'@'[/email])
{//文字信息
buffer[length-1] = '\0';
CString str;
str.Format("rev:\t%s\n",buffer);
m_lbx->AddString(str);
mb.release();
}
else
{
if(buffer[length-1] == '$')
{//文件名
buffer[length-1] = '\0';
recvFileName = buffer;
dlg->MessageBox(recvFileName);
}
else
{
if(flag)
{
if (dlg->MessageBox("是否接收数据?","提示",MB_YESNO)==IDYES)
{
CString returnPath;
TCHAR szPath[MAX_PATH];
BROWSEINFO bInfo;
bInfo.hwndOwner = NULL;
bInfo.pidlRoot = NULL;
bInfo.pszDisplayName = szPath;
bInfo.lpszTitle ="请选择目的路径";
bInfo.ulFlags = BIF_BROWSEINCLUDEFILES|BIF_RETURNFSANCESTORS|BIF_RETURNONLYFSDIRS|BIF_BROWSEFORCOMPUTER;
bInfo.lParam =NULL;
bInfo.lpfn =NULL;
LPITEMIDLIST lpList = SHBrowseForFolder(&bInfo);
SHGetPathFromIDList(lpList,szPath);
filePath = szPath ;
filePath += "\\";
//用来区分哪个客户端发来的文件,把其IP地址放在文件名的前一级目录。
// filePath += peer_name
// filePath += "\\";
filePath += recvFileName;
dlg->MessageBox(filePath);
//往文件里写内容
//保存文件对话框的问题
flag=false;
}
else
{
if( buffer[length-1] =='^')
flag=true;
return;
}
}
if(buffer[length-1] == '%')
{
buffer[length-1] = '\0';
if (file_connector.connect(file,
ACE_FILE_Addr (filePath),
0,
ACE_Addr::sap_any,
0,
O_RDWR|O_CREAT|O_APPEND |O_BINARY
) == -1)
{
printf("error");
}
int len=length-1;
if (file.send (buffer, len) != len)
printf("send error");
//所传文件的信息
if (file.close () == -1)
printf("close error");
}
if(buffer[length-1] =='^')
{
flag=true;
buffer[length-1] = '\0';
if (file_connector.connect(file,
ACE_FILE_Addr (filePath),
0,
ACE_Addr::sap_any,
0,
O_RDWR|O_CREAT|O_APPEND |O_BINARY
) == -1)
{
dlg->MessageBox((LPCTSTR)"open error");
}
int len=length-1;
if (file.send (buffer, len) != len)
dlg->MessageBox((LPCTSTR)"send error");
//所传文件的信息
if (file.close () == -1)
dlg->MessageBox((LPCTSTR)"close error");
dlg->MessageBox("传送完毕");
}
LocalFree(buffer);
}
}
ACE_Message_Block *nmb;
ACE_NEW_NORETURN(nmb,ACE_Message_Block(8102));
if (this->reader_.read (*nmb, nmb->space ()) != 0)
return;
}
问题:
1.现在可以发送任意大的文本文件。
发送过程,如果大于8100,先按8100个字节来截断,然后依次发送。成功
2。但我如果发送别的文件,比如压缩包。发送的时候最开始只发送5个字节(好像是),没有发送8100个字节? |
|