讨论:如何用最简单的方法实现局域网中的两台计算机文件互传? 如何用最简单的方法实现局域网中的两台计算机文件互传? 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 文件互传最简单的方式就是把文件copy到对方电脑的共享文件夹 一部分代码供参考UINT SendDataThread(LPVOID lpParam){ CTzg004Dlg *pDlg=(CTzg004Dlg *)lpParam; CFile file; if( !file.Open(pDlg->m_strFileName, CFile::modeRead) ) { AfxMessageBox("打开文件出错!"); return 0; } pDlg->m_bSendRun=TRUE; CSocket sockTemp; CString str,str1; sockTemp.Create(pDlg->m_iDataPort1); sockTemp.Listen(1);//只接受一个连接 CSocket sockSend; pDlg->GetDlgItem(IDC_BUTTON_SEND)->EnableWindow(FALSE); sockTemp.Accept(sockSend);//注意,sockTemp已交了自己的指针地址到sockSend,故不用Close pDlg->GetDlgItem(IDC_BUTTON_SEND_END)->EnableWindow(TRUE); int iBufSize = 1024 * 5; int iSize = iBufSize; LPBYTE pBuf = new BYTE[iBufSize]; DWORD dwTemp = 0; BOOL bTest = sockSend.AsyncSelect(0);//由于CSocket实际是异步,将它变为同步(阻塞)方式。 sockSend.IOCtl( FIONBIO, &dwTemp);//用IOCtl要将AsyncSelect的第一个参数为0,参看MSDN UINT uiLength = file.GetLength(); sockSend.Send(&uiLength, 4);//传送文件大小到接收方(Client端) int iNumByte; UINT uiTotal = 0; while(uiTotal < uiLength) { int iEnd=pDlg->m_bSendEnd; iNumByte = sockSend.Send(&iEnd, sizeof(int)); if(iNumByte == SOCKET_ERROR) { AfxMessageBox("发送错误!"); goto ExitLable1; }else if(iEnd==1) { AfxMessageBox("发送端终止"); goto ExitLable1; } if((int)(uiLength - uiTotal) < iBufSize) iSize = uiLength - uiTotal;//当小于缓冲区iTEST时的处理 iSize=file.Read(pBuf , iSize);//得到读取的字节数 int iCount=0; while(iCount<iSize) { iNumByte = sockSend.Send(pBuf, iSize-iCount);//注意iNumByte为实际的发送字节数,不要以iSize为准 if(iNumByte == SOCKET_ERROR) { AfxMessageBox("发送错误!"); goto ExitLable1; } iCount+=iNumByte; if(iCount<iSize) { file.Seek(iSize-iCount,CFile::current); } } uiTotal += iCount; pDlg->m_CtrlProgressSend.SetPos(int(((double)uiTotal/uiLength)*100)); str.Format("发送进度:%d%%",int(((double)uiTotal/uiLength)*100)); pDlg->GetDlgItem(IDC_STATIC_SEND)->GetWindowText(str1); if(str1!=str) pDlg->GetDlgItem(IDC_STATIC_SEND)->SetWindowText(str); } AfxMessageBox("发送文件成功!");ExitLable1: delete[] pBuf; pDlg->m_bSendRun=FALSE; file.Close(); sockSend.Close(); pDlg->m_CtrlProgressSend.SetPos(0); pDlg->GetDlgItem(IDC_BUTTON_SEND_END)->EnableWindow(FALSE); pDlg->GetDlgItem(IDC_BUTTON_SEND)->EnableWindow(TRUE); pDlg->GetDlgItem(IDC_STATIC_SEND)->SetWindowText("发送进度:"); return 0;}UINT ReceiveDataThread(LPVOID lpParam){ CTzg004Dlg *pDlg=(CTzg004Dlg *)lpParam; CFileDialog dlg(FALSE,NULL,NULL,OFN_HIDEREADONLY|OFN_OVERWRITEPROMPT, "所有文件 (*.*)|*.*||"); while(dlg.DoModal()!=IDOK) { AfxMessageBox("选择文件出错,请重新选择!"); } CString str,str1,str2; CSocket sockRecv; sockRecv.Create(); pDlg->m_CtrlIPSend.GetWindowText(str); pDlg->GetDlgItem(IDC_BUTTON_RECEIVE)->EnableWindow(FALSE); while(sockRecv.Connect(str,pDlg->m_iDataPort2)==0)//接收方地址,若上网,可改为实际IP地址,端口要跟Server端相同。 { Sleep(50); } pDlg->GetDlgItem(IDC_BUTTON_REC_END)->EnableWindow(TRUE); pDlg->m_bRecRun=TRUE; str2=dlg.GetPathName(); CFile file; file.Open(str2, CFile::modeCreate | CFile::modeWrite); BOOL bFileFail=FALSE; DWORD dwTemp = 0; sockRecv.AsyncSelect(0); sockRecv.IOCtl( FIONBIO, &dwTemp);//变为阻塞方式 UINT uiLength; sockRecv.Receive(&uiLength, 4);//接收发方(Server端)的文件大小 int iBufSize = 1024 * 5; int iSize = iBufSize; LPBYTE pBuf = new BYTE[iBufSize]; int iNumByte; UINT uiTotal = 0; while(uiTotal < uiLength) { int iEnd=0; if(pDlg->m_bRecEnd) { AfxMessageBox("接收端终止!"); goto ExitLable2; } iNumByte=sockRecv.Receive(&iEnd, sizeof(int)); if(iNumByte == SOCKET_ERROR) { AfxMessageBox("接收信号错误!"); goto ExitLable2; } if(iEnd==1) { AfxMessageBox("发送端终止!"); goto ExitLable2; } if((int)(uiLength - uiTotal) < iBufSize) iSize = uiLength - uiTotal; int iCount=0; while(iCount<iSize) { iNumByte = sockRecv.Receive(pBuf, iSize-iCount); if(iNumByte == SOCKET_ERROR) { AfxMessageBox("接收错误!"); goto ExitLable2; } iCount+=iNumByte; file.Write(pBuf, iNumByte); } uiTotal += iCount;//以实际接收字节为准 pDlg->m_CtrlProgressRec.SetPos(int(((double)uiTotal/uiLength)*100)); str.Format("接收进度:%d%%",int(((double)uiTotal/uiLength)*100)); pDlg->GetDlgItem(IDC_STATIC_REC)->GetWindowText(str1); if(str1!=str) pDlg->GetDlgItem(IDC_STATIC_REC)->SetWindowText(str); } AfxMessageBox("接收文件成功!"); bFileFail=TRUE;ExitLable2: pDlg->m_bRecRun=FALSE; delete[] pBuf; file.Close(); //文件接收失败,则删除接收文件 if(!bFileFail) { CFile::Remove( str2 ); } sockRecv.Close(); pDlg->m_CtrlProgressRec.SetPos(0); pDlg->GetDlgItem(IDC_BUTTON_REC_END)->EnableWindow(FALSE); pDlg->GetDlgItem(IDC_BUTTON_RECEIVE)->EnableWindow(TRUE); pDlg->GetDlgItem(IDC_STATIC_REC)->SetWindowText("接收进度:"); return 0;} ftp最简单 呵呵 不过要启动服务 Winpacp编程问题,急求高手解答,坐等达人,多谢捧场 DLL中 ansi unicode能否共存? 完成端口 多次发送问题 高分求:我是一个菜鸟,却很想知道tab control 怎么用,能够在每一页里面出现一个表(listcontrol)最好能有详细的例程(中文)。 简单多分,马上给分! 怎么使用上一级对话框中 的函数和变量 请问个关于XML中增加一个接点的问题!急啊!大家近来看看吧! 在线等待 运行环境扮演问题 OPENGL里面显示汉字,中文字下面出现两道横杠,百思不得其解 请问哪有ipp下载 我的注册表被黑网站禁用了,谁能帮忙解决一下,100分送上,在线等待
UINT SendDataThread(LPVOID lpParam)
{
CTzg004Dlg *pDlg=(CTzg004Dlg *)lpParam;
CFile file;
if( !file.Open(pDlg->m_strFileName, CFile::modeRead) )
{
AfxMessageBox("打开文件出错!");
return 0;
}
pDlg->m_bSendRun=TRUE;
CSocket sockTemp;
CString str,str1;
sockTemp.Create(pDlg->m_iDataPort1);
sockTemp.Listen(1);//只接受一个连接
CSocket sockSend;
pDlg->GetDlgItem(IDC_BUTTON_SEND)->EnableWindow(FALSE);
sockTemp.Accept(sockSend);//注意,sockTemp已交了自己的指针地址到sockSend,故不用Close
pDlg->GetDlgItem(IDC_BUTTON_SEND_END)->EnableWindow(TRUE);
int iBufSize = 1024 * 5;
int iSize = iBufSize;
LPBYTE pBuf = new BYTE[iBufSize];
DWORD dwTemp = 0;
BOOL bTest = sockSend.AsyncSelect(0);//由于CSocket实际是异步,将它变为同步(阻塞)方式。
sockSend.IOCtl( FIONBIO, &dwTemp);//用IOCtl要将AsyncSelect的第一个参数为0,参看MSDN
UINT uiLength = file.GetLength();
sockSend.Send(&uiLength, 4);//传送文件大小到接收方(Client端)
int iNumByte;
UINT uiTotal = 0;
while(uiTotal < uiLength)
{
int iEnd=pDlg->m_bSendEnd;
iNumByte = sockSend.Send(&iEnd, sizeof(int));
if(iNumByte == SOCKET_ERROR)
{
AfxMessageBox("发送错误!");
goto ExitLable1;
}else if(iEnd==1)
{
AfxMessageBox("发送端终止");
goto ExitLable1;
}
if((int)(uiLength - uiTotal) < iBufSize)
iSize = uiLength - uiTotal;//当小于缓冲区iTEST时的处理
iSize=file.Read(pBuf , iSize);//得到读取的字节数
int iCount=0;
while(iCount<iSize)
{
iNumByte = sockSend.Send(pBuf, iSize-iCount);//注意iNumByte为实际的发送字节数,不要以iSize为准
if(iNumByte == SOCKET_ERROR)
{
AfxMessageBox("发送错误!");
goto ExitLable1;
}
iCount+=iNumByte;
if(iCount<iSize)
{
file.Seek(iSize-iCount,CFile::current);
}
}
uiTotal += iCount;
pDlg->m_CtrlProgressSend.SetPos(int(((double)uiTotal/uiLength)*100));
str.Format("发送进度:%d%%",int(((double)uiTotal/uiLength)*100));
pDlg->GetDlgItem(IDC_STATIC_SEND)->GetWindowText(str1);
if(str1!=str)
pDlg->GetDlgItem(IDC_STATIC_SEND)->SetWindowText(str);
}
AfxMessageBox("发送文件成功!");
ExitLable1:
delete[] pBuf;
pDlg->m_bSendRun=FALSE;
file.Close();
sockSend.Close();
pDlg->m_CtrlProgressSend.SetPos(0);
pDlg->GetDlgItem(IDC_BUTTON_SEND_END)->EnableWindow(FALSE);
pDlg->GetDlgItem(IDC_BUTTON_SEND)->EnableWindow(TRUE);
pDlg->GetDlgItem(IDC_STATIC_SEND)->SetWindowText("发送进度:");
return 0;
}
UINT ReceiveDataThread(LPVOID lpParam)
{
CTzg004Dlg *pDlg=(CTzg004Dlg *)lpParam;
CFileDialog dlg(FALSE,NULL,NULL,OFN_HIDEREADONLY|OFN_OVERWRITEPROMPT,
"所有文件 (*.*)|*.*||");
while(dlg.DoModal()!=IDOK)
{
AfxMessageBox("选择文件出错,请重新选择!");
}
CString str,str1,str2;
CSocket sockRecv;
sockRecv.Create();
pDlg->m_CtrlIPSend.GetWindowText(str);
pDlg->GetDlgItem(IDC_BUTTON_RECEIVE)->EnableWindow(FALSE);
while(sockRecv.Connect(str,pDlg->m_iDataPort2)==0)//接收方地址,若上网,可改为实际IP地址,端口要跟Server端相同。
{
Sleep(50);
}
pDlg->GetDlgItem(IDC_BUTTON_REC_END)->EnableWindow(TRUE);
pDlg->m_bRecRun=TRUE;
str2=dlg.GetPathName();
CFile file;
file.Open(str2, CFile::modeCreate | CFile::modeWrite);
BOOL bFileFail=FALSE;
DWORD dwTemp = 0;
sockRecv.AsyncSelect(0);
sockRecv.IOCtl( FIONBIO, &dwTemp);//变为阻塞方式
UINT uiLength;
sockRecv.Receive(&uiLength, 4);//接收发方(Server端)的文件大小
int iBufSize = 1024 * 5;
int iSize = iBufSize;
LPBYTE pBuf = new BYTE[iBufSize];
int iNumByte;
UINT uiTotal = 0;
while(uiTotal < uiLength)
{
int iEnd=0;
if(pDlg->m_bRecEnd)
{
AfxMessageBox("接收端终止!");
goto ExitLable2;
}
iNumByte=sockRecv.Receive(&iEnd, sizeof(int));
if(iNumByte == SOCKET_ERROR)
{
AfxMessageBox("接收信号错误!");
goto ExitLable2;
}
if(iEnd==1)
{
AfxMessageBox("发送端终止!");
goto ExitLable2;
} if((int)(uiLength - uiTotal) < iBufSize)
iSize = uiLength - uiTotal;
int iCount=0;
while(iCount<iSize)
{
iNumByte = sockRecv.Receive(pBuf, iSize-iCount);
if(iNumByte == SOCKET_ERROR)
{
AfxMessageBox("接收错误!");
goto ExitLable2;
}
iCount+=iNumByte;
file.Write(pBuf, iNumByte);
}
uiTotal += iCount;//以实际接收字节为准
pDlg->m_CtrlProgressRec.SetPos(int(((double)uiTotal/uiLength)*100));
str.Format("接收进度:%d%%",int(((double)uiTotal/uiLength)*100));
pDlg->GetDlgItem(IDC_STATIC_REC)->GetWindowText(str1);
if(str1!=str)
pDlg->GetDlgItem(IDC_STATIC_REC)->SetWindowText(str);
}
AfxMessageBox("接收文件成功!");
bFileFail=TRUE;
ExitLable2:
pDlg->m_bRecRun=FALSE;
delete[] pBuf;
file.Close();
//文件接收失败,则删除接收文件
if(!bFileFail)
{
CFile::Remove( str2 );
}
sockRecv.Close();
pDlg->m_CtrlProgressRec.SetPos(0);
pDlg->GetDlgItem(IDC_BUTTON_REC_END)->EnableWindow(FALSE);
pDlg->GetDlgItem(IDC_BUTTON_RECEIVE)->EnableWindow(TRUE);
pDlg->GetDlgItem(IDC_STATIC_REC)->SetWindowText("接收进度:");
return 0;
}