MyFtpClient.cpp
资源名称:FTP总集.rar [点击查看]
上传用户:tjfeida
上传日期:2013-03-10
资源大小:1917k
文件大小:12k
源码类别:
Ftp客户端
开发平台:
Visual C++
- #include "StdAfx.h"
- #include ".myftpclient.h"
- #include "MyFtpClient.h"
- #include "Afxsock.h"
- #include <stdio.h>
- #include <tchar.h>
- #include <process.h>
- #include "tchar.h"
- MyFtpClient::MyFtpClient(void)
- {
- m_pControlRarchive=NULL;
- m_pControlTarchive=NULL;
- m_pControlSocketFile=NULL;
- m_pControlSocket=NULL;
- m_pFireWallCheck=2;
- }
- //向服务器发出命令并得到响应的函数
- BOOL MyFtpClient::FTPCommand(CString command)
- {
- if(command!=""&&!WriteString(command))
- return FALSE;
- if((!(ReadString1()))||(m_pFireWallCheck!=2))
- return FALSE;
- return TRUE;
- }
- MyFtpClient::~MyFtpClient(void)
- {
- CloseControlChannel();
- }
- //上传或者下载文件
- BOOL MyFtpClient::MoveFile(CString RemoteFile, CString LocalFile, bool Pasv, bool GetfFile)
- {
- CString LeftServer,temp,RightServer;
- UINT LocalSocket,ServerSocket,i,j;
- CFile File_data;
- CSocket Socket_server;
- CAsyncSocket Channel_data;
- int Number,ReadNumber,SendNumber;
- const int BUFFERSIZE=4096;
- char CurrentBuffer[BUFFERSIZE];
- DWORD IpArgument=0;
- if(!File_data.Open(LocalFile,(GetfFile?CFile::modeWrite|CFile::modeCreate:CFile::modeRead)))
- {
- m_strRetmsg.LoadString(58113);
- return FALSE;
- }
- if(!FTPCommand("TYPE I"))
- return FALSE;
- if(Pasv)
- {
- if(!FTPCommand("PASV"))
- return FALSE;
- if((i=m_strRetmsg.Find("("))==-1||(j=m_strRetmsg.Find(")")))
- return FALSE;
- //获取IP地址,和端口号的字符段
- temp=m_strRetmsg.Mid(i+1,(j-i)-1);
- //将端口转化成整形变量
- //低字节
- i=temp.ReverseFind(',');
- ServerSocket=atol(temp.Right(temp.GetLength()-(i+1)));
- //高字节
- temp=temp.Left(i);
- i=temp.ReverseFind(',');
- ServerSocket+=256*atol(temp.Right(temp.GetLength()-(i+1)));
- //获得IP地址
- RightServer=temp.Left(i);
- while(1)
- {
- if((i=RightServer.Find(','))==-1)
- break;
- RightServer.SetAt(i,'.');
- }
- }
- //通过以上处理此时temp里面放的是以点分隔的ip地址
- else
- {
- m_strRetmsg.LoadString(106);//消息处理
- //获得本地的连接ip地址以及端口号信息
- if(!m_pControlSocket->GetSockName(LeftServer,LocalSocket))
- return FALSE;
- //将本地的ip地址转化为,分隔的
- while(1)
- {
- if((i=LeftServer.Find('.'))==-1)
- break;
- LeftServer.SetAt(i,',');
- }
- //建立连接或者侦听连接
- if((!Socket_server.Create(0,SOCK_STREAM,NULL))||(!Socket_server.Listen()))
- return FALSE;
- //获得本地侦听进程的ip以及端口
- if(!Socket_server.GetSockName(temp,LocalSocket))
- return FALSE;
- //将端口转化为字节以点分隔
- LeftServer.Format(LeftServer+",%d,%d",LocalSocket/256,LocalSocket%256);
- if(!FTPCommand("PORT "+LeftServer))
- return FALSE;
- }
- //发送RETR/STOR/命令到服务器
- if(!WriteString((GetfFile?"RETR ":"SOTR ")+RemoteFile))
- return FALSE;
- if(Pasv)
- {
- if(!Channel_data.Create())
- {
- AfxMessageBox("建立线程连接失败");
- m_strRetmsg.LoadString(106);
- return FALSE;
- }
- Channel_data.Connect(RightServer,ServerSocket);
- }
- if(!ReadString1()||m_pFireWallCheck!=1)
- return FALSE;
- if(!Pasv&&!Socket_server.Accept(Channel_data))
- return FALSE;
- if((!Channel_data.AsyncSelect(0))||(Channel_data.IOCtl(FIONBIO,&IpArgument)))
- {
- AfxMessageBox("建立线程连接失败");
- m_strRetmsg.LoadString(106);
- return FALSE;
- }
- while(1)
- {
- TRY
- {
- if(GetfFile)
- {
- if(!(Number=Channel_data.Receive(CurrentBuffer,BUFFERSIZE,0))||Number==SOCKET_ERROR)
- break;
- else(File_data.Write(CurrentBuffer,Number));
- }
- else
- {
- if(!(ReadNumber=File_data.Read(CurrentBuffer,BUFFERSIZE)))
- break;
- if((SendNumber=Channel_data.Send(CurrentBuffer,ReadNumber,0))==SOCKET_ERROR)
- break;
- if(ReadNumber!=SendNumber)
- File_data.Seek(SendNumber-ReadNumber,CFile::current);
- }
- }
- CATCH(CException,pEx)
- {
- m_strRetmsg.LoadString(105);
- return FALSE;
- }
- END_CATCH
- }
- Channel_data.Close();
- File_data.Close();
- if(!FTPCommand(""))
- return FALSE;
- return TRUE;
- }
- void MyFtpClient::LogOffServer(void)
- {
- WriteString("断开连接");
- CloseControlChannel();
- WSACleanup();
- }
- BOOL MyFtpClient::LogOntoServer(CString ServerName, int ServerPort, CString UserName,
- CString PassWord, CString Accept, CString FireWallServer,
- CString FireWallUserName, CString FireWallPassWord,
- int FireWordPort, int LogOnType)
- {
- int nPort,LogOnPoint=0;
- const int LogSuccess=-2,LogError=-1;
- CString buffer,temp;
- const int LOGONNUMBER=9;
- int LogonSequence[LOGONNUMBER][100] = {
- // 下面的数组保存了针对不同防火墙的登录序列
- {0,LogSuccess,3, 1,LogSuccess,6, 2,LogSuccess,LogError}, // 没有防火墙
- {3,6,3, 4,6,LogError, 5,LogError,9, 0,LogSuccess,12, 1,LogSuccess,15, 2,LogSuccess,LogError}, // 主机名
- {3,6,3, 4,6,LogError, 6,LogSuccess,9, 1,LogSuccess,12, 2,LogSuccess,LogError}, // USER 后跟主机信息的
- {7,3,3, 0,LogSuccess,6, 1,LogSuccess,9, 2,LogSuccess,LogError}, //有代理
- {3,6,3, 4,6,LogError, 0,LogSuccess,9, 1,LogSuccess,12, 2,LogSuccess,LogError}, // Transparent
- {6,LogSuccess,3, 1,LogSuccess,6, 2,LogSuccess,LogError}, // USER with no logon
- {8,6,3, 4,6,LogError, 0,LogSuccess,9, 1,LogSuccess,12, 2,LogSuccess,LogError}, //USER fireID@remotehost
- {9,LogError,3, 1,LogSuccess,6, 2,LogSuccess,LogError}, //USER remoteID@remotehost fireID
- {10,LogSuccess,3, 11,LogSuccess,6, 2,LogSuccess,LogError} // USER remoteID@fireID@remotehost
- };
- if(LogOnType<0||LogOnType>=LOGONNUMBER)
- return FALSE;
- //没有防火墙的
- if(!LogOnType)
- {
- temp=ServerName;
- nPort=ServerPort;
- }
- //有防火墙的
- else
- {
- temp=FireWallServer;
- nPort=FireWordPort;
- }
- if(ServerPort!=21)
- ServerName.Format(ServerName+":%d",ServerPort);
- //打开控制通道
- if(!OpenControlChannel(temp,nPort))
- return false;
- //发出命令以得到服务器的响应
- if(!FTPCommand(""))
- return false;
- //各种不同形式的登录
- while(1)
- {
- switch(LogonSequence[LogOnType][LogOnPoint])
- {
- case 0:
- temp="USER "+UserName;
- break;
- case 1:
- temp="PASS "+PassWord;
- break;
- case 2:
- temp="ACCT "+Accept;
- break;
- case 3:
- temp="USER "+FireWallUserName;
- break;
- case 4:
- temp="PASS "+FireWallPassWord;
- break;
- case 5:
- temp="SITE "+ServerName;
- break;
- case 6:
- temp="USER "+UserName+"@"+ServerName;
- break;
- case 7:
- temp="OPEN "+ServerName;
- break;
- case 8:
- temp="USER "+FireWallUserName+"@"+ServerName;
- break;
- case 9:
- temp="USER "+UserName+"@"+ServerName+" "+FireWallUserName;
- break;
- case 10:
- temp="USER "+UserName+"@"+FireWallUserName+"@"+ServerName;
- break;
- case 11:
- temp="PASS "+PassWord+"@"+FireWallPassWord;
- break;
- }
- if(!WriteString(temp))
- return FALSE;
- if(!ReadString1())
- return FALSE;
- // AfxMessageBox("ok");
- if(m_pFireWallCheck!=2&&m_pFireWallCheck!=3)
- return FALSE;
- LogOnPoint=LogonSequence[LogOnType][LogOnPoint+m_pFireWallCheck-1];
- switch(LogOnPoint)
- {
- case LogError:
- m_strRetmsg.LoadString(108);
- return FALSE;
- case LogSuccess:
- return TRUE;
- }
- }
- }
- //获得服务器的响应函数
- BOOL MyFtpClient::ReadString1(void)
- {
- int RetCode;
- if(!ReadString2())
- return FALSE;
- if(m_strRetmsg.GetLength()<4||m_strRetmsg.GetAt(3)!='-')
- return TRUE;
- RetCode=atol(m_strRetmsg);
- while(1)
- {
- if(m_strRetmsg.GetLength()>3&&(m_strRetmsg.GetAt(3)==' '&&atol(m_strRetmsg)==RetCode))
- return TRUE;
- if(!ReadString2())
- return FALSE;
- }
- }
- //写命令到服务器
- BOOL MyFtpClient::WriteString(CString OutPutString)
- {
- m_strRetmsg.LoadString(106);
- TRY
- {
- m_pControlTarchive->WriteString(OutPutString+"rn");
- m_pControlTarchive->Flush();
- }
- CATCH(CException,e)
- {
- return FALSE;
- }
- END_CATCH
- return TRUE;
- }
- //列出文件列表
- BOOL MyFtpClient::List(void)
- {
- CString LeftServer,temp,RightServer;
- UINT LocalSocket,i;
- CFile File_data;
- CSocket Socket_server;
- CAsyncSocket Channel_data;
- int Number,sum;
- const int BUFFERSIZE=4096;
- DWORD IpArgument=0;
- m_strBuffer.RemoveAll();
- m_strBuffer.SetSize(BUFFERSIZE);
- //向FTP发控制命令
- if(!FTPCommand("TYPE I"))
- return FALSE;
- m_strRetmsg.LoadString(106);
- //获得本地主机的ip及端口信息
- if(!m_pControlSocket->GetSockName(LeftServer,LocalSocket))
- return FALSE;
- //将.转化为,
- while(1)
- {
- if((i=LeftServer.Find('.'))==-1)
- break;
- LeftServer.SetAt(i,',');
- }
- if((!Socket_server.Create(0,SOCK_STREAM,NULL))||(!Socket_server.Listen()))
- return FALSE;
- if(!Socket_server.GetSockName(temp,LocalSocket))
- return FALSE;
- LeftServer.Format(LeftServer+",%d,%d",LocalSocket/256,LocalSocket%256);
- if(!FTPCommand("PORT "+LeftServer))
- return FALSE;
- if(!WriteString("LIST"))
- return FALSE;
- if(!ReadString1())
- return FALSE;
- if(!Socket_server.Accept(Channel_data))
- return FALSE;
- if((!Channel_data.AsyncSelect(0))||(!Channel_data.IOCtl(FIONBIO,&IpArgument)))
- {
- m_strRetmsg.LoadString(106);
- return FALSE;
- }
- sum=0;
- while(1)
- {
- TRY
- {
- if(!(Number=Channel_data.Receive(m_strBuffer.GetData()+sum,BUFFERSIZE,0))||Number==SOCKET_ERROR)
- break;
- TRACE("Received :%dn",Number);
- Sleep(0);
- sum+=Number;
- m_strBuffer.SetSize(sum+BUFFERSIZE);
- }
- CATCH(CException,e)
- {
- m_strRetmsg.LoadString(105);
- return FALSE;
- }
- END_CATCH
- }
- Channel_data.Close();
- }
- BOOL MyFtpClient::GetLine(int& index)
- {
- m_strLine.Empty();
- int nBytes=m_strBuffer.GetSize();
- BOOL bLine=FALSE;
- while((bLine==FALSE)&&(index<nBytes))
- {
- char ch=(char)(m_strBuffer.GetAt(index));
- switch(ch)
- {
- case 'n':
- bLine=TRUE;
- break;
- default:
- m_strLine+=ch;
- break;
- }
- ++index;
- }
- m_strLine=m_strLine.Left(m_strLine.GetLength()-1);
- return bLine;
- }
- //从服务器控制通道获取一行的响应
- BOOL MyFtpClient::ReadString2(void)
- {
- if(!m_pControlRarchive->ReadString(m_strRetmsg))
- {
- m_strRetmsg.LoadString(106);
- return FALSE;
- }
- if(m_strRetmsg.GetLength()>0)
- m_pFireWallCheck=m_strRetmsg.GetAt(0)-48;
- return TRUE;
- }
- //打开控制通道
- BOOL MyFtpClient::OpenControlChannel(CString serverAddress, int serverPort)
- {
- m_strRetmsg.LoadString(104);
- if(!(m_pControlSocket=new CSocket))
- return FALSE;
- if(!(m_pControlSocket->Create()))
- return FALSE;
- m_strRetmsg.LoadString(103);
- if(!(m_pControlSocket->Connect(serverAddress,serverPort)))
- return FALSE;
- m_strRetmsg.LoadString(104);
- if(!(m_pControlSocketFile=new CSocketFile(m_pControlSocket)))
- return FALSE;
- if(!(m_pControlRarchive=new CArchive(m_pControlSocketFile,CArchive::load)))
- return FALSE;
- if(!(m_pControlTarchive=new CArchive(m_pControlSocketFile,CArchive::store)))
- return FALSE;
- return TRUE;
- }
- void MyFtpClient::CloseControlChannel(void)
- {
- if(m_pControlTarchive)
- delete m_pControlTarchive;
- m_pControlTarchive=NULL;
- if(m_pControlRarchive)
- delete m_pControlRarchive;
- m_pControlRarchive=NULL;
- if(m_pControlSocketFile)
- delete m_pControlSocketFile;
- m_pControlSocketFile=NULL;
- if(m_pControlSocket)
- delete m_pControlSocket;
- m_pControlSocket=NULL;
- return;
- }