using System;
using System.Net;
using System.IO;
using System.Text;
using System.Net.Sockets;
![](/Images/OutliningIndicators/None.gif)
namespace FTP
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/ContractedBlock.gif)
{
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// 类描述:对FTP 操作类
/// 作者:何火生
/// </summary>
public class FTPFactory
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](/Images/OutliningIndicators/ContractedSubBlock.gif)
Arrbuite#region Arrbuite
private string remoteHost, remotePath, remoteUser, remotePass, mes;
private int remotePort, bytes;
private Socket clientSocket;
![](/Images/OutliningIndicators/InBlock.gif)
private int retValue;
private Boolean debug;
private Boolean logined;
private string reply;
private static int BLOCK_SIZE = 512;
Byte[] buffer = new Byte[BLOCK_SIZE];
Encoding ASCII = Encoding.ASCII;
#endregion
![](/Images/OutliningIndicators/InBlock.gif)
protected FTPFactory()
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
remoteHost = "localhost";
remotePath = ".";
remoteUser = "anonymous";
remotePass = "jaimon@school2000.co.uk";
FTPSetup();
}
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ContractedSubBlock.gif)
构造函数#region 构造函数
private void FTPSetup()
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
remotePort = 21;
debug = false;
logined = false;
}
#endregion
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ContractedSubBlock.gif)
getFileList#region getFileList
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// Return a string array containing the remote directory's file list.
/// </summary>
/// <param name="mask"></param>
/// <returns></returns>
public string[] getFileList(string mask)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](/Images/OutliningIndicators/InBlock.gif)
if (!logined)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
login();
}
![](/Images/OutliningIndicators/InBlock.gif)
Socket cSocket = createDataSocket();
sendCommand("NLST " + mask);
if (!(retValue == 150 || retValue == 125))
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
throw new IOException(reply.Substring(4));
}
![](/Images/OutliningIndicators/InBlock.gif)
mes = "";
![](/Images/OutliningIndicators/InBlock.gif)
while (true)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](/Images/OutliningIndicators/InBlock.gif)
int bytes = cSocket.Receive(buffer, buffer.Length, 0);
mes += ASCII.GetString(buffer, 0, bytes);
![](/Images/OutliningIndicators/InBlock.gif)
if (bytes < buffer.Length)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
break;
}
}
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
char[] seperator =
{ '\n' };
string[] mess = mes.Split(seperator);
![](/Images/OutliningIndicators/InBlock.gif)
cSocket.Close();
![](/Images/OutliningIndicators/InBlock.gif)
readReply();
![](/Images/OutliningIndicators/InBlock.gif)
if (retValue != 226)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
throw new IOException(reply.Substring(4));
}
return mess;
![](/Images/OutliningIndicators/InBlock.gif)
}
#endregion
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ContractedSubBlock.gif)
getFileSize#region getFileSize
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// Return the size of a file.
/// </summary>
/// <param name="fileName"></param>
/// <returns></returns>
public long getFileSize(string fileName)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](/Images/OutliningIndicators/InBlock.gif)
if (!logined)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
login();
}
![](/Images/OutliningIndicators/InBlock.gif)
sendCommand("SIZE " + fileName);
long size = 0;
![](/Images/OutliningIndicators/InBlock.gif)
if (retValue == 213)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
size = Int64.Parse(reply.Substring(4));
}
else
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
throw new IOException(reply.Substring(4));
}
![](/Images/OutliningIndicators/InBlock.gif)
return size;
![](/Images/OutliningIndicators/InBlock.gif)
}
#endregion
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ContractedSubBlock.gif)
login#region login
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// Login to the remote server.
/// </summary>
public void login()
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](/Images/OutliningIndicators/InBlock.gif)
clientSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
IPEndPoint ep = new IPEndPoint(Dns.GetHostEntry(remoteHost).AddressList[0], remotePort);
try
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
clientSocket.Connect(ep);
}
catch
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
//throw new IOException("Couldn't connect to remote server");;
}
![](/Images/OutliningIndicators/InBlock.gif)
readReply();
if (retValue != 220)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
Close();
//throw new IOException(reply.Substring(4));
}
if (debug)
Console.WriteLine("USER " + remoteUser);
![](/Images/OutliningIndicators/InBlock.gif)
sendCommand("USER " + remoteUser);
if (!(retValue == 331 || retValue == 230))
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
cleanup();
//throw new IOException(reply.Substring(4));
}
![](/Images/OutliningIndicators/InBlock.gif)
if (retValue != 230)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if (debug)
Console.WriteLine("PASS xxx");
![](/Images/OutliningIndicators/InBlock.gif)
sendCommand("PASS " + remotePass);
if (!(retValue == 230 || retValue == 202))
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
cleanup();
//throw new IOException(reply.Substring(4));
}
}
logined = true;
Console.WriteLine("Connected to " + remoteHost);
ChDir(remotePath);
}
#endregion
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ContractedSubBlock.gif)
setBinaryMode#region setBinaryMode
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// If the value of mode is true, set binary mode for downloads.
/// Else, set Ascii mode.
/// </summary>
/// <param name="mode"></param>
public void setBinaryMode(Boolean mode)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](/Images/OutliningIndicators/InBlock.gif)
if (mode)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
sendCommand("TYPE I");
}
else
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
sendCommand("TYPE A");
}
if (retValue != 200)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
throw new IOException(reply.Substring(4));
}
}
#endregion
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ContractedSubBlock.gif)
Download#region Download
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// Download a file to the Assembly's local directory,
/// keeping the same file name.
/// </summary>
/// <param name="remFileName"></param>
/// <returns></returns>
public bool Download(string remFileName)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return Download(remFileName, "", false);
}
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// Download a remote file to the Assembly's local directory,
/// keeping the same file name, and set the resume flag.
/// </summary>
/// <param name="remFileName"></param>
/// <param name="resume"></param>
/// <returns></returns>
public bool Download(string remFileName, Boolean resume)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return Download(remFileName, "", resume);
}
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// Download a remote file to a local file name which can include
/// a path. The local file name will be created or overwritten,
/// but the path must exist.
/// </summary>
/// <param name="remFileName"></param>
/// <param name="locFileName"></param>
/// <returns></returns>
public bool Download(string remFileName, string locFileName)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return Download(remFileName, locFileName, false);
}
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// Download a remote file to a local file name which can include
/// a path, and set the resume flag. The local file name will be
/// created or overwritten, but the path must exist.
/// </summary>
/// <param name="remFileName"></param>
/// <param name="locFileName"></param>
/// <param name="resume"></param>
/// <returns></returns>
public bool Download(string remFileName, string locFileName, Boolean resume)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](/Images/OutliningIndicators/InBlock.gif)
bool Finish = false;
bool FileExists = false;
if (!logined)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
login();
}
setBinaryMode(true);
try
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
FileInfo fileInfo = new FileInfo(locFileName);
if (fileInfo.Exists)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
fileInfo.IsReadOnly = false;
fileInfo.Delete();
}
}
catch
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return false;
}
foreach (string str in getFileList("."))
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if (str.IndexOf(remFileName.ToUpper(), StringComparison.CurrentCultureIgnoreCase) != -1)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
FileExists = true;
break;
}
}
if (!FileExists)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
//Console.WriteLine("文件不存在!");
return false;
}
Console.WriteLine("Downloading file " + remFileName + " from " + remoteHost + "/" + remotePath);
![](/Images/OutliningIndicators/InBlock.gif)
if (locFileName.Equals(""))
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
locFileName = remFileName;
}
![](/Images/OutliningIndicators/InBlock.gif)
if (!File.Exists(locFileName))
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
Stream st = File.Create(locFileName);
st.Close();
}
FileStream output = null;
try
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
output = new FileStream(locFileName, FileMode.Open);
}
catch (Exception ex)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
Console.WriteLine(ex.Message);
return false;
}
Socket cSocket = createDataSocket();
long offset = 0;
if (resume)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
offset = output.Length;
if (offset > 0)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
sendCommand("REST " + offset);
if (retValue != 350)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
//throw new IOException(reply.Substring(4));
//Some servers may not support resuming.
Finish = false;
offset = 0;
}
}
![](/Images/OutliningIndicators/InBlock.gif)
if (offset > 0)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if (debug)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
Console.WriteLine("seeking to " + offset);
}
long npos = output.Seek(offset, SeekOrigin.Begin);
Console.WriteLine("new pos=" + npos);
}
}
![](/Images/OutliningIndicators/InBlock.gif)
sendCommand("RETR " + remFileName);
if (!(retValue == 150 || retValue == 125))
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
throw new IOException(reply.Substring(4));
}
![](/Images/OutliningIndicators/InBlock.gif)
while (true)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
bytes = cSocket.Receive(buffer, buffer.Length, 0);
output.Write(buffer, 0, bytes);
if (bytes <= 0)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
Finish = true;
break;
}
}
![](/Images/OutliningIndicators/InBlock.gif)
output.Close();
if (cSocket.Connected)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
cSocket.Close();
}
readReply();
if (!(retValue == 226 || retValue == 250))
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
throw new IOException(reply.Substring(4));
}
return Finish;
}
#endregion
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ContractedSubBlock.gif)
Upload#region Upload
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// Upload a file
/// </summary>
/// <param name="fileName"></param>
/// <returns>Upload a file is success!</returns>
public bool Upload(string fileName)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return Upload(fileName, false);
}
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// Upload a file and set the resume flag.
/// </summary>
/// <param name="fileName"></param>
/// <param name="resume"></param>
/// <returns></returns>
public bool Upload(string fileName, Boolean resume)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if (!logined)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
login();
}
Socket cSocket = createDataSocket();
long offset = 0;
if (resume)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
try
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
setBinaryMode(true);
offset = getFileSize(fileName);
![](/Images/OutliningIndicators/InBlock.gif)
}
catch (Exception)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
offset = 0;
}
}
![](/Images/OutliningIndicators/InBlock.gif)
if (offset > 0)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
sendCommand("REST " + offset);
if (retValue != 350)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
//throw new IOException(reply.Substring(4));
//Remote server may not support resuming.
offset = 0;
}
}
![](/Images/OutliningIndicators/InBlock.gif)
sendCommand("STOR " + Path.GetFileName(fileName));
![](/Images/OutliningIndicators/InBlock.gif)
if (!(retValue == 125 || retValue == 150))
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
throw new IOException(reply.Substring(4));
}
![](/Images/OutliningIndicators/InBlock.gif)
// open input stream to read source file
FileStream input = new FileStream(fileName, FileMode.Open);
if (offset != 0)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](/Images/OutliningIndicators/InBlock.gif)
if (debug)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
Console.WriteLine("seeking to " + offset);
}
input.Seek(offset, SeekOrigin.Begin);
}
![](/Images/OutliningIndicators/InBlock.gif)
Console.WriteLine("Uploading file " + fileName + " to " + remotePath);
![](/Images/OutliningIndicators/InBlock.gif)
while ((bytes = input.Read(buffer, 0, buffer.Length)) > 0)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
cSocket.Send(buffer, bytes, 0);
}
input.Close();
Console.WriteLine("");
if (cSocket.Connected)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
cSocket.Close();
}
readReply();
if (!(retValue == 226 || retValue == 250))
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
throw new IOException(reply.Substring(4));
}
return true;
}
#endregion
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ContractedSubBlock.gif)
DeleteRemoteFile#region DeleteRemoteFile
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// Delete a file from the remote FTP server.
/// </summary>
/// <param name="fileName"></param>
public void DeleteRemoteFile(string fileName)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](/Images/OutliningIndicators/InBlock.gif)
if (!logined)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
login();
}
![](/Images/OutliningIndicators/InBlock.gif)
sendCommand("DELE " + fileName);
if (retValue != 250)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
throw new IOException(reply.Substring(4));
}
![](/Images/OutliningIndicators/InBlock.gif)
}
#endregion
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ContractedSubBlock.gif)
RenameRemoteFile#region RenameRemoteFile
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// Rename a file on the remote FTP server.
/// </summary>
/// <param name="oldFileName"></param>
/// <param name="newFileName"></param>
public void RenameRemoteFile(string oldFileName, string newFileName)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](/Images/OutliningIndicators/InBlock.gif)
if (!logined)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
login();
}
![](/Images/OutliningIndicators/InBlock.gif)
sendCommand("RNFR " + oldFileName);
![](/Images/OutliningIndicators/InBlock.gif)
if (retValue != 350)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
throw new IOException(reply.Substring(4));
}
![](/Images/OutliningIndicators/InBlock.gif)
// known problem
// rnto will not take care of existing file.
// i.e. It will overwrite if newFileName exist
sendCommand("RNTO " + newFileName);
if (retValue != 250)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
throw new IOException(reply.Substring(4));
}
![](/Images/OutliningIndicators/InBlock.gif)
}
#endregion
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ContractedSubBlock.gif)
MkDir#region MkDir
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// Create a directory on the remote FTP server.
/// </summary>
/// <param name="dirName"></param>
public void MkDir(string dirName)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](/Images/OutliningIndicators/InBlock.gif)
if (!logined)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
login();
}
![](/Images/OutliningIndicators/InBlock.gif)
sendCommand("MKD " + dirName);
![](/Images/OutliningIndicators/InBlock.gif)
if (retValue != 250)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
throw new IOException(reply.Substring(4));
}
![](/Images/OutliningIndicators/InBlock.gif)
}
#endregion
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ContractedSubBlock.gif)
RmDir#region RmDir
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// Delete a directory on the remote FTP server
/// </summary>
/// <param name="dirName"></param>
public void RmDir(string dirName)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](/Images/OutliningIndicators/InBlock.gif)
if (!logined)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
login();
}
![](/Images/OutliningIndicators/InBlock.gif)
sendCommand("RMD " + dirName);
![](/Images/OutliningIndicators/InBlock.gif)
if (retValue != 250)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
throw new IOException(reply.Substring(4));
}
![](/Images/OutliningIndicators/InBlock.gif)
}
#endregion
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ContractedSubBlock.gif)
ChDir#region ChDir
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// Change the current working directory on the remote FTP server.
/// </summary>
/// <param name="dirName"></param>
public void ChDir(string dirName)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](/Images/OutliningIndicators/InBlock.gif)
if (dirName.Equals("."))
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return;
}
![](/Images/OutliningIndicators/InBlock.gif)
if (!logined)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
login();
}
![](/Images/OutliningIndicators/InBlock.gif)
sendCommand("CWD " + dirName);
![](/Images/OutliningIndicators/InBlock.gif)
if (retValue != 250)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
throw new IOException(reply.Substring(4));
}
![](/Images/OutliningIndicators/InBlock.gif)
this.remotePath = dirName;
![](/Images/OutliningIndicators/InBlock.gif)
Console.WriteLine("Current directory is " + remotePath);
![](/Images/OutliningIndicators/InBlock.gif)
}
#endregion
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ContractedSubBlock.gif)
Close()#region Close()
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// Close the FTP connection.
/// </summary>
public void Close()
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](/Images/OutliningIndicators/InBlock.gif)
if (clientSocket != null)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
sendCommand("QUIT");
}
![](/Images/OutliningIndicators/InBlock.gif)
cleanup();
Console.WriteLine("Closing
");
}
#endregion
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ContractedSubBlock.gif)
OTHER#region OTHER
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// Set debug mode.
/// </summary>
/// <param name="debug"></param>
public void setDebug(Boolean debug)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
this.debug = debug;
}
![](/Images/OutliningIndicators/InBlock.gif)
private void readReply()
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
mes = "";
reply = readLine();
retValue = Int32.Parse(reply.Substring(0, 3));
}
![](/Images/OutliningIndicators/InBlock.gif)
private void cleanup()
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if (clientSocket != null)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
clientSocket.Close();
clientSocket = null;
}
logined = false;
}
#endregion
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ContractedSubBlock.gif)
readLine#region readLine
private string readLine()
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](/Images/OutliningIndicators/InBlock.gif)
while (true)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
bytes = clientSocket.Receive(buffer, buffer.Length, 0);
mes += ASCII.GetString(buffer, 0, bytes);
if (bytes < buffer.Length)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
break;
}
}
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
char[] seperator =
{ '\n' };
string[] mess = mes.Split(seperator);
![](/Images/OutliningIndicators/InBlock.gif)
if (mes.Length > 2)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
mes = mess[mess.Length - 2];
}
else
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
mes = mess[0];
}
![](/Images/OutliningIndicators/InBlock.gif)
if (!mes.Substring(3, 1).Equals(" "))
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return readLine();
}
![](/Images/OutliningIndicators/InBlock.gif)
if (debug)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
for (int k = 0; k < mess.Length - 1; k++)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
Console.WriteLine(mess[k]);
}
}
return mes;
}
#endregion
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ContractedSubBlock.gif)
sendCommand#region sendCommand
private void sendCommand(String command)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](/Images/OutliningIndicators/InBlock.gif)
Byte[] cmdBytes = Encoding.ASCII.GetBytes((command + "\r\n").ToCharArray());
clientSocket.Send(cmdBytes, cmdBytes.Length, 0);
readReply();
}
#endregion
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ContractedSubBlock.gif)
createDataSocket#region createDataSocket
private Socket createDataSocket()
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
sendCommand("PASV");
if (retValue != 227)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
throw new IOException(reply.Substring(4));
}
![](/Images/OutliningIndicators/InBlock.gif)
int index1 = reply.IndexOf('(');
int index2 = reply.IndexOf(')');
string ipData = reply.Substring(index1 + 1, index2 - index1 - 1);
int[] parts = new int[6];
int len = ipData.Length;
int partCount = 0;
string buf = "";
for (int i = 0; i < len && partCount <= 6; i++)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](/Images/OutliningIndicators/InBlock.gif)
char ch = Char.Parse(ipData.Substring(i, 1));
if (Char.IsDigit(ch))
buf += ch;
else if (ch != ',')
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
throw new IOException("Malformed PASV reply: " + reply);
}
![](/Images/OutliningIndicators/InBlock.gif)
if (ch == ',' || i + 1 == len)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](/Images/OutliningIndicators/InBlock.gif)
try
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
parts[partCount++] = Int32.Parse(buf);
buf = "";
}
catch (Exception)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
throw new IOException("Malformed PASV reply: " + reply);
}
}
}
string ipAddress = parts[0] + "." + parts[1] + "." + parts[2] + "." + parts[3];
int port = (parts[4] << 8) + parts[5];
Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
IPEndPoint ep = new IPEndPoint(Dns.GetHostEntry(ipAddress).AddressList[0], port);
try
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
s.Connect(ep);
}
catch (Exception)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
throw new IOException("Can't connect to remote server");
}
return s;
}
#endregion
![](/Images/OutliningIndicators/InBlock.gif)
}
![](/Images/OutliningIndicators/InBlock.gif)
}
![](/Images/OutliningIndicators/None.gif)
![](/skins/gray/images/speech.gif)
posted on
2007-11-30 13:06
wskfire
阅读(
5513)
评论()
编辑
收藏
举报