各位大侠快来救救小弟吧~~~~!搞定了还可以加分! 请问在WIN2K和VC6.0的环境下怎么去实现通过网络传送文件呢?比如说传送*.exe文件等!头好痛,救命啊~~~~~! 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 UINT SendDataThread(LPVOID lpParam);UINT ReceiveDataThread(LPVOID lpParam);void CTzg004Dlg::OnButtonSend() { // TODO: Add your control notification handler code here m_bSendEnd=FALSE; m_bRecEnd=FALSE; UpdateData(TRUE); CFileDialog dlg(TRUE,NULL,NULL,OFN_HIDEREADONLY|OFN_OVERWRITEPROMPT, "所有文件 (*.*)|*.*||"); if(dlg.DoModal()==IDOK) { m_strFileName=dlg.GetPathName(); AfxBeginThread(SendDataThread,this,THREAD_PRIORITY_NORMAL); }}void CTzg004Dlg::OnButtonReceive() { // TODO: Add your control notification handler code here m_bSendEnd=FALSE; m_bRecEnd=FALSE; UpdateData(TRUE); AfxBeginThread(ReceiveDataThread,this,THREAD_PRIORITY_NORMAL);}void CTzg004Dlg::OnButtonSendEnd() { // TODO: Add your control notification handler code here m_bSendEnd=TRUE;}void CTzg004Dlg::OnButtonRecEnd() { // TODO: Add your control notification handler code here m_bRecEnd=TRUE;}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的,不是两台电脑通讯,是一个机器要重PC里下载程序 最讨厌那种给人回答问题不负责任的发一堆代码的人。强烈鄙视!TO 楼猪:看你的具体需要了,比如:1、如果你是从HTTP SERVER上取文件,就用HTTP系列函数。VCKBASE的第十几期(具体忘了)有个同志写的文章比较使用,可以参考。2、如果是FTP SERVER的话,可以用FTP系列函数。上面两种情况都是WININET里的函数,需要对方SERVER有HTTP、FTP服务器。3、另外就是一般情况,没有什么HTTP、FTP服务的情况。这就涉及到SOCKET编程,需要自己写代码来实现,把一个文件一段一段的传,接收方接受一点就往硬盘上写一点。 connectsendacceptreceive sorry~! 刚才打错了,是一个机器要从PC里下载文件(*.txt,*.exe 等等)后在执行,机器整个过程中都是被动的。还有东方浩气老兄,我是用SOCKET编程的,现在网络通了,但是呢提到的一段一段传,怎么实现呢,能说得具体一点吗?谢谢~~~~! 贴代码的兄弟,先谢谢你了,我接触VC不久,一时看不懂这些东西,你能吧你的成品发到我的邮箱吗?谢谢,让我慢慢的研究:)我的邮箱是[email protected] 请问有关回声消除? 问一下,向CButton控件或CEdit控件有没有自己的窗口函数? 谁有计算两个颜色经过特定透明度混合后的新颜色的算法? ListBox控件的问题 请问,想让一个函数返回一个数组要如何定义这个函数? 什么工具可以把32x32的icon转换为16x16的icon? 怎样画矩形框,并且线条颜色可以改变??? 小问题请求帮助 怎样使用EnumWindows函数搜索所有窗体的句柄?请跟进! 低能儿的疑惑···· 在线等待~~~~ 请问大家阿,mailslot能不能在2000 server下用阿
UINT ReceiveDataThread(LPVOID lpParam);void CTzg004Dlg::OnButtonSend()
{
// TODO: Add your control notification handler code here
m_bSendEnd=FALSE;
m_bRecEnd=FALSE;
UpdateData(TRUE);
CFileDialog dlg(TRUE,NULL,NULL,OFN_HIDEREADONLY|OFN_OVERWRITEPROMPT,
"所有文件 (*.*)|*.*||");
if(dlg.DoModal()==IDOK)
{
m_strFileName=dlg.GetPathName();
AfxBeginThread(SendDataThread,this,THREAD_PRIORITY_NORMAL);
}
}
void CTzg004Dlg::OnButtonReceive()
{
// TODO: Add your control notification handler code here
m_bSendEnd=FALSE;
m_bRecEnd=FALSE;
UpdateData(TRUE);
AfxBeginThread(ReceiveDataThread,this,THREAD_PRIORITY_NORMAL);
}
void CTzg004Dlg::OnButtonSendEnd()
{
// TODO: Add your control notification handler code here
m_bSendEnd=TRUE;
}void CTzg004Dlg::OnButtonRecEnd()
{
// TODO: Add your control notification handler code here
m_bRecEnd=TRUE;
}
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;
}
TO 楼猪:
看你的具体需要了,比如:
1、如果你是从HTTP SERVER上取文件,就用HTTP系列函数。VCKBASE的第十几期(具体忘了)有个同志写的文章比较使用,可以参考。
2、如果是FTP SERVER的话,可以用FTP系列函数。
上面两种情况都是WININET里的函数,需要对方SERVER有HTTP、FTP服务器。
3、另外就是一般情况,没有什么HTTP、FTP服务的情况。这就涉及到SOCKET编程,需要自己写代码来实现,把一个文件一段一段的传,接收方接受一点就往硬盘上写一点。
send
accept
receive