📄 copy of ftpclient.cs
字号:
}
l_strarrFields = SplitStringForWindows(l_strcurFile);
if ( l_strarrFields.Length == 1 || l_strarrFields.Length <= 0 ) {
/* If the element is empty, do not attempt to parse it */
continue;
}
/* Each field is now in l_strarrFields */
FileInfo l_FileInfo = GetFileInfoForWindows(l_strarrFields);
if ( l_FileInfo != null ) {
/* Add it to list Object */
m_FileList.Add(l_FileInfo);
}
l_iFileCount++;
}
return l_iFileCount;
}
private string[] SplitStringForWindows(string l_strInString){
string[] l_strarrOut = new string[5];
string l_strTemp = "";
char[] l_arrchSep = new char[2];
l_arrchSep[0] = ' ';
l_arrchSep[1] = '\0';
int l_iFieldCount = 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] == ' ' ) {
if ( l_iFieldCount <= 1 ) {
l_strarrOut[l_iFieldCount] = l_strTemp;
l_iFieldCount++;
l_strTemp = "";
if ( l_strInString[l_iCount + 1] == ' ' ) {
for ( l_iCount++ ; l_strInString[l_iCount] == ' '; l_iCount++ ) ;
/* Postion the counter back to previous character */
l_iCount--;
}
}
else if ( l_iFieldCount == 2 ) {
/* Check for first digit here
* if '<' then it is a directory
* else it is a file
* */
if ( l_strTemp[0] == '<' ) {
/* Directory Flag */
l_strarrOut[l_iFieldCount] = "D";
l_iFieldCount++;
/* size of directory is not known */
l_strarrOut[l_iFieldCount] = "0";
l_iFieldCount++;
/* Skip the rest of characters */
for ( l_iCount++ ; l_strInString[l_iCount] != ' '; l_iCount++ ) ;
}
else {
/* Normal File */
l_strarrOut[l_iFieldCount] = "N";
l_iFieldCount++;
l_strarrOut[l_iFieldCount] = l_strTemp;
l_iFieldCount++;
}
}
}
else {
if ( l_iFieldCount == 4 ) {
/* File name */
l_strTemp = "";
for ( ; l_iCount < l_strInString.Length ; l_iCount++ ){
l_strTemp += l_strInString[l_iCount];
}
/* remove \r\n character */
l_strTemp = l_strTemp.Substring(0,l_strTemp.Length - 1);
l_strarrOut[l_iFieldCount] = l_strTemp;
}
else {
l_strTemp += l_strInString[l_iCount];
}
}
}
//l_strarrOut = l_strTemp.Split(l_arrchSep);
return l_strarrOut;
}
private FileInfo GetFileInfoForWindows(string[] l_strarrFields){
FileInfo l_FileInfo = new FileInfo();
string l_strField = "";
string l_strFileName = "" ,l_strFilePath = "" ,l_strDateCreated = "";
string l_strTimeCreated = "";
int l_iFileType = 0,l_iFileSize = 0;
int l_iCount = 0;
foreach(object obj in l_strarrFields) {
l_strField = (string) obj;
if (l_strField == null ) {
break;
}
switch( l_iCount) {
case 0: /* Date */
l_strDateCreated = l_strField;
break;
case 1 : /* Time */
l_strTimeCreated = l_strField;
break;
case 2 : /* File Type N - normal file, D - directory */
if ( l_strField.Equals("N") ) {
l_iFileType = 1;
}
else if ( l_strField.Equals("D") ) {
l_iFileType = 2;
}
break;
case 3 : /* File size in bytes */
try {
l_iFileSize = int.Parse(l_strField);
}
catch ( FormatException e ) {
l_iFileSize = 0;
}
break;
case 4 : /* Name of the file */
l_strFileName = l_strField;
break;
} /* End of switch statement */
l_iCount++;
} /* End of for */
/* If error occured while parsing return null and ignore this element */
/*if (l_iErrFlag == 1 ) {
return null;
}
*/
l_strFilePath = m_strCurrentWorkingDir + "/" + l_strFileName;
l_FileInfo.SetFileInfo(l_iFileType,l_strFileName,l_strFilePath,l_strDateCreated,l_iFileSize,"","",l_strTimeCreated);
return l_FileInfo;
}
/* Called for Unix Servers,Parses the array and fills the
* m_FileList object with list of files in each node
* */
private int ParseStringArrayForUnix(string[] l_arrFiles){
m_FileList.RemoveAll();
string l_strcurFile = "";
int l_iFileCount = l_arrFiles.Length;
int l_iLineCount = 0;
char[] l_arrchSep = new char[2];
l_arrchSep[0] = ' ';
l_arrchSep[1] = '\0';
foreach(object obj in l_arrFiles) {
l_strcurFile = (string) obj;
/* If first line then parse for length */
if ( l_iLineCount == 0 ) {
string[] l_strTemp = l_strcurFile.Split(l_arrchSep);
try {
string l_strLen = l_strTemp[1];
l_strLen.Trim();
m_FileList.TotalLength = l_strLen.ToInt32();
}
catch ( FormatException e ) {
m_FileList.TotalLength = 0;
}
catch(Exception e) {
m_FileList.TotalLength = 0;
}
}
else {
/* Parse for file info */
string[] l_strarrFields = SplitStringForUnix(l_strcurFile);
if (l_strarrFields.Length == 1 || l_strarrFields[0].Length <= 0 ) {
/* If the element is empty, do not attempt to parse it */
l_iLineCount++;
continue;
}
/* Each field is now in l_strarrFields */
FileInfo l_FileInfo = GetFileInfoForUnix(l_strarrFields);
if ( l_FileInfo != null ) {
/* Add it to list Object */
m_FileList.Add(l_FileInfo);
}
}
l_iLineCount++;
}
return l_iLineCount;
}
private FileInfo GetFileInfoForUnix(string[] l_strarrFields){
FileInfo l_FileInfo = new FileInfo();
string l_strField = "";
string l_strFileName = "" ,l_strFilePath = "" ,l_strDateCreated = "";
string l_strFileOwner = "",l_strFileGroup = "";
int l_iFileType = 0,l_iFileSize = 0;
int l_iCount = 0,l_iErrFlag = 0;
char l_chTemp = '\0';
foreach(object obj in l_strarrFields) {
l_strField = (string) obj;
if (l_strField.Length <= 0 ) {
l_iErrFlag = 1;
break;
}
switch( l_iCount) {
case 0: /* File Type */
l_chTemp = l_strField[0];
if ( l_chTemp == '-' ) {
/* Normal File */
l_iFileType = 1;
}
else if ( l_chTemp == 'd') {
/* Directory */
l_iFileType = 2;
}
else if ( l_chTemp == 'c' ) {
/* System file */
l_iFileType = 3;
}
else {
/* Unknown file type */
l_iFileType = 0;
}
break;
case 1 : /* File Links */
break;
case 2 : /* File Owner */
l_strFileOwner = l_strField;
break;
case 3 : /* File group */
l_strFileGroup = l_strField;
break;
case 4 : /* File size in bytes */
l_iFileSize = int.Parse(l_strField);
break;
case 5 : /* Date created ( contains month in mmm format ) */
l_strDateCreated = l_strField;
break;
case 6 : /* Date created ( contains day in dd format ) */
l_strDateCreated += " " + l_strField;
break;
case 7 : /* Date created ( contains time in HH:SS or year in YYYY format ) */
l_strDateCreated += " " + l_strField;
break;
case 8 : /* File name */
l_strFileName = l_strField;
l_strFileName = l_strFileName.Trim();
if (l_strFileName.Length == 0 ) {
l_strFileName = "<unknown>";
}
break;
case 9 :
break;
} /* End of switch statement */
l_iCount++;
} /* End of for */
/* If error occured while parsing return null and ignore this element */
if (l_iErrFlag == 1 ) {
return null;
}
l_strFilePath = m_strCurrentWorkingDir + "/" + l_strFileName;
l_FileInfo.SetFileInfo(l_iFileType,l_strFileName,l_strFilePath,l_strDateCreated,l_iFileSize,l_strFileOwner,l_strFileGroup,"");
return l_FileInfo;
}
/* This function takes a string and squeezes more than
* one space characters into one character and
* then splits into array and returns back to the caller
* */
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;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -