📄 ftpclient.cs
字号:
* */
private string[] SplitStringForUnix(string l_strInString){
string[] l_strarrOut = new string[9];
string l_strTemp = "";
char[] l_arrchSep = new char[2];
l_arrchSep[0] = ' ';
l_arrchSep[1] = '\0';
/* Squeeze more spaces into single space character */
int l_iCount = 0,l_iLen = l_strInString.Length;
for ( ; l_iCount < l_iLen ; l_iCount++ ) {
if ( l_strInString[l_iCount] == ' ' ) {
l_strTemp += l_strInString[l_iCount];
if ( l_strInString[l_iCount + 1] == ' ' ) {
for ( l_iCount++ ; l_strInString[l_iCount] == ' '; l_iCount++ ) ;
l_strTemp += l_strInString[l_iCount];
}
}
else {
l_strTemp += l_strInString[l_iCount];
}
}
l_strarrOut = l_strTemp.Split(l_arrchSep);
return l_strarrOut;
}
/* Generate Port Number for Data communication */
private int GetPort(){
if ( m_iDataPort > 30000 ) {
m_iDataPort = 10000;
}
return m_iDataPort++;
}
/* Executes a data connection command */
private int ExecuteDataCommand(string l_strCommand,out string l_strOutputData){
TCPListener l_FTPListener = null;
Socket l_ClientDataSocket = null;
int l_iRetval = 0, l_iDataPort = 0;
try {
l_strOutputData = "";
l_iDataPort = GetPort();
l_FTPListener = new TCPListener(l_iDataPort);
l_FTPListener.Start();
string l_strOutput = "";
string l_strPortParams = GetPortParameters(l_iDataPort);
/* PORT command */
string l_strPortCommand = "PORT " + l_strPortParams + "\r\n";
l_iRetval = ExecuteCommand(l_strPortCommand,ref l_strOutput);
if ( l_iRetval == 0 ) {
return l_iRetval;
}
/* Execute the actual command here */
l_iRetval = ExecuteCommand(l_strCommand,ref l_strOutput);
if ( l_iRetval == 0 ) {
return l_iRetval;
}
/* Accept Client connection here */
l_ClientDataSocket = l_FTPListener.Accept();
l_ClientDataSocket.SetSockOpt(SocketOption.SolSocket,SocketOption.SoRcvTimeo,m_iRecvTimeout);
//Byte[] l_bSendData = new Byte[1024];
Byte[] l_bRecvData = new Byte[1024];
l_strOutput = "";
string l_strTemp = "";
l_iRetval = 0;
/* Loop through and Receive all the data */
//for ( ; (l_iRetval = l_ClientDataSocket.Receive(l_bRecvData,511,0)) > 0 ; ) {
for ( ; l_ClientDataSocket.Available > 0 ; ) {
l_iRetval = l_ClientDataSocket.Receive(l_bRecvData,1023,0);
l_strTemp = Encoding.ASCII.GetString(l_bRecvData,0,l_iRetval);
l_strOutput += l_strTemp;
}
l_ClientDataSocket.Shutdown(SocketShutdown.SdBoth);
l_ClientDataSocket.Close();
l_ClientDataSocket = null;
m_strList = l_strOutput;
l_strOutputData = l_strOutput;
l_iRetval = 1;
}
catch ( Exception e ) {
m_strErrorMessage = "Error : " + e;
l_strOutputData = "";
return 0;
}
finally{
if ( l_FTPListener != null ) {
l_FTPListener.Stop();
l_FTPListener = null;
}
if ( l_ClientDataSocket != null ) {
l_ClientDataSocket.Shutdown(SocketShutdown.SdBoth);
l_ClientDataSocket.Close();
l_ClientDataSocket = null;
}
}
return l_iRetval;
}
/* Get Port parameters */
private string GetPortParameters(int l_iPort){
string l_strLocalMachine = DNS.GetHostName();
IPAddress l_IPAddress = DNS.Resolve(l_strLocalMachine);
string l_strIPAddress = l_IPAddress.ToString();
l_strIPAddress = l_strIPAddress.Replace('.',',');
int l_iParam1 = ( 0xff00 & l_iPort ) >> 8;
int l_iParam2 = ( 0xff & l_iPort ) ;
string l_strPortParam = l_strIPAddress + "," + l_iParam1 + "," + l_iParam2;
return l_strPortParam;
}
public FileList GetFileList(){
return m_FileList;
}
public string GetRootDirectory(){
return m_strRootDir;
}
public string GetWorkingDirectory(){
return m_strCurrentWorkingDir;
}
private string ParseForPWD(string l_strInput){
int l_iCount = 0;
int l_iLen = l_strInput.Length;
string l_strOutput = "";
for ( ; l_iCount < l_iLen ; l_iCount++) {
if ( l_strInput[l_iCount] == '"' ){
for ( l_iCount++ ; l_iCount < l_iLen && l_strInput[l_iCount] != '"' ; l_iCount++) {
l_strOutput += l_strInput[l_iCount];
}
}
}
return l_strOutput;
}
/* This function parses the response code for the command passed
* Returns 1 for successfull response codes.
* Returns 0 for error response code, also fills the message into m_strErrorMessage variable
*/
private int ParseResponseCode(string l_strCommand,string l_strResponseCode){
int l_iResponseCode = 0, l_iRetval = 0;
try {
if ( l_strResponseCode.Length > 0 ) {
l_iResponseCode = l_strResponseCode.ToInt32();
}
}
catch ( FormatException e ) {
m_strErrorMessage = "Error : Unrecognized Response code from Server" ;
string err = "err" + e;
return 0;
}
/* 421 Error is trapped as a common error */
if ( l_iResponseCode == 421 ) {
m_strErrorMessage = "Error : Service not available, closing control connection. Please try after some time" ;
return 0;
}
/* If connect Command */
if ( l_strCommand.Equals("CONNECT") ) {
/* 120,220 (success)
* 421 (service not available)
*/
switch ( l_iResponseCode ) {
case 120 :
case 220 :
l_iRetval = 1;
break;
default:
l_iRetval = 0;
break;
}
}
else if ( l_strCommand.Equals("USER") ) {
/* 220,230,530,331 (success) */
/* 332,530,500,501,530 (syntax error) */
/* 421 (service not available) */
switch( l_iResponseCode ) {
case 220 : case 230 : case 331 :
l_iRetval = 1;
break;
case 332 :
m_strErrorMessage = "Error : You don't have account for logging in";
l_iRetval = 0;
break;
case 500 :
m_strErrorMessage = "Error : <USER> command. Syntax Error Command Unrecognized";
l_iRetval = 0;
break;
case 501 :
m_strErrorMessage = "Error : <USER> command. Syntax error in parameters or arguments";
l_iRetval = 0;
break;
case 530 :
m_strErrorMessage = "Error : User not logged on. Check your User id and password";
l_iRetval = 0;
break;
default:
m_strErrorMessage = "Error : Unknown error in USER command. Response code :" + l_iResponseCode;
l_iRetval = 0;
break;
}
}
else if ( l_strCommand.Equals("PASS") ) {
/* 220,230,530,331,332 (success) */
/* 500,501 (syntax error) */
/* 530 not logged on */
/* 332 not logged on */
/* 421 (service not available) */
switch( l_iResponseCode ) {
case 230 : case 202 :
l_iRetval = 1;
break;
case 332 :
m_strErrorMessage = "Error : You don't have account for logging in";
l_iRetval = 0;
break;
case 500 :
m_strErrorMessage = "Error : <PASS> command. Syntax Error Command Unrecognized";
l_iRetval = 0;
break;
case 501 :
m_strErrorMessage = "Error : <PASS> command. Syntax error in parameters or arguments";
l_iRetval = 0;
break;
case 503 :
m_strErrorMessage = "Error : <PASS> command. Bad sequence of commands";
l_iRetval = 0;
break;
case 530 :
m_strErrorMessage = "Error : User not logged on. Check your User id and password";
l_iRetval = 0;
break;
default:
m_strErrorMessage = "Error : Unknown error in PASS command. Response code :" + l_iResponseCode;
l_iRetval = 0;
break;
}
}
else if ( l_strCommand.Equals("SYST") ) {
/* 215 (success) */
/* 500,501,502 (syntax error) */
/* 421 (service not available) */
switch( l_iResponseCode ) {
case 215 :
l_iRetval = 1;
break;
case 500 :
m_strErrorMessage = "Error : <SYST> command. Syntax Error Command Unrecognized";
l_iRetval = 0;
break;
case 501 :
m_strErrorMessage = "Error : <SYST> command. Syntax error in parameters or arguments";
l_iRetval = 0;
break;
case 502 :
m_strErrorMessage = "Error : <SYST> command. Command not implemented";
l_iRetval = 0;
break;
default:
m_strErrorMessage = "Error : Unknown error in SYST command. Response code :" + l_iResponseCode;
l_iRetval = 0;
break;
}
}
else if ( l_strCommand.StartsWith("PWD") ) {
/* 257 (success) */
/* 500,501,502,550 (syntax error) */
/* 421 (service not available) */
switch( l_iResponseCode ) {
case 257 :
l_iRetval = 1;
break;
case 500 :
m_strErrorMessage = "Error : <PWD> command. Syntax Error Command Unrecognized";
l_iRetval = 0;
break;
case 501 :
m_strErrorMessage = "Error : <PWD> command. Syntax error in parameters or arguments";
l_iRetval = 0;
break;
case 502 :
m_strErrorMessage = "Error : <PWD> command. Command not implemented";
l_iRetval = 0;
break;
case 550 :
m_strErrorMessage = "Error : <PWD> command. Requested action not taken or access denied";
l_iRetval = 0;
break;
default:
m_strErrorMessage = "Error : Unknown error in PWD command. Response code :" + l_iResponseCode;
l_iRetval = 0;
break;
}
}
else if ( l_strCommand.Equals("MODE") ) {
/* 200 (success) */
/* 500,501,504,530 (syntax error) */
/* 421 (service not available) */
switch( l_iResponseCode ) {
case 200 :
l_iRetval = 1;
break;
case 500 :
m_strErrorMessage = "Error : <MODE> command. Syntax Error Command Unrecognized";
l_iRetval = 0;
break;
case 501 :
m_strErrorMessage = "Error : <MODE> command. Syntax error in parameters or arguments";
l_iRetval = 0;
break;
case 504 :
m_strErrorMessage = "Error : <MODE> command. Command not implemented";
l_iRetval = 0;
break;
case 530 :
m_strErrorMessage = "Error : <MODE> command. User not logged on";
l_iRetval = 0;
break;
default:
m_strErrorMessage = "Error : Unknown error in MODE command. Response code :" + l_iResponseCode;
l_iRetval = 0;
break;
}
}
else if ( l_strCommand.Equals("TYPE") ) {
/* 200 (success) */
/* 500,501,504,530 (syntax error) */
/* 421 (service not available) */
switch( l_iResponseCode ) {
case 200 :
l_iRetval = 1;
break;
case 500 :
m_strErrorMessage = "Error : <TYPE> command. Syntax Error Command Unrecognized";
l_iRetval = 0;
break;
case 501 :
m_strErrorMessage = "Error : <TYPE> command. Syntax error in parameters or arguments";
l_iRetval = 0;
break;
case 504 :
m_strErrorMessage = "Error : <TYPE> command. Command not implemented";
l_iRetval = 0;
break;
case 530 :
m_strErrorMessage = "Error : <TYPE> command. User not logged on";
l_iRetval = 0;
break;
default:
m_strErrorMessage = "Error : Unknown error in TYPE command. Response code :" + l_iResponseCode;
l_iRetval = 0;
break;
}
}
else if ( l_strCommand.Equals("PORT") ) {
/* 200 (success) */
/* 500,501,530 (syntax error) */
/* 421 (service not available) */
switch( l_iResponseCode ) {
case 200 :
l_iRetval = 1;
break;
case 500 :
m_strErrorMessage = "Error : <PORT> command. Syntax Error Command Unrecognized";
l_iRetval = 0;
break;
case 501 :
m_strErrorMessage = "Error : <PORT> command. Syntax error in parameters or arguments";
l_iRetval = 0;
break;
case 530 :
m_strErrorMessage = "Error : <PORT> command. User not logged on";
l_iRetval = 0;
break;
case 426 :
m_strErrorMessage = "Error : <PORT> command. Connection closed transfer aborted";
l_iRetval = 0;
break;
default:
m_strErrorMessage = "Error : Unknown error in PORT command. Response code :" + l_iResponseCode;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -