📄 ftpconnection.cs
字号:
using System;
using System.Net;
using System.IO;
using System.Text;
using System.Net.Sockets;
using System.Collections;
using System.Data;
using System.Threading;
namespace FTPClient
{
/// <summary>
/// Summary description for FTPConnection.
/// </summary>
public class FTPConnection
{
private TcpClient tcpClient;
private static int BLOCK_SIZE = 512;
private static int DEFAULT_REMOTE_PORT = 21;
private static int DATA_PORT_RANGE_FROM = 1500;
private static int DATA_PORT_RANGE_TO = 65000;
private FTPMode mode;
private int activeConnectionsCount;
private string remoteHost;
private ArrayList messageList = new ArrayList();
private bool logMessages;
public FTPConnection()
{
this.activeConnectionsCount = 0;
this.mode = FTPMode.Active;
this.logMessages = false;
}
public ArrayList MessageList
{
get
{
return this.messageList;
}
}
public bool LogMessages
{
get
{
return this.logMessages;
}
set
{
if(!value)
{
this.messageList = new ArrayList();
}
this.logMessages = value;
}
}
public virtual void Open(string remoteHost, string user, string password)
{
Open(remoteHost, DEFAULT_REMOTE_PORT, user, password, FTPMode.Active);
}
public virtual void Open(string remoteHost, string user, string password, FTPMode mode)
{
Open(remoteHost, DEFAULT_REMOTE_PORT, user, password, mode);
}
public virtual void Open(string remoteHost, int remotePort, string user, string password)
{
Open(remoteHost, remotePort, user, password, FTPMode.Active);
}
public virtual void Open(string remoteHost, int remotePort, string user, string password, FTPMode pMode)
{
ArrayList tempMessageList = new ArrayList();
int returnValue;
this.mode = pMode;
this.tcpClient = new TcpClient();
this.remoteHost = remoteHost;
// As we cannot detect the local address from the TCPClient class, convert "127.0.0.1" and "localhost" to
// the DNS record of this machine; this will ensure that the connection address and the PORT command address
// are identical. This fixes bug 854919.
if (remoteHost == "localhost" || remoteHost == "127.0.0.1")
{
remoteHost = GetLocalAddressList()[0].ToString();
}
//CONNECT
try
{
this.tcpClient.Connect(remoteHost, remotePort);
}
catch(Exception)
{
throw new IOException("Couldn't connect to remote server");
}
tempMessageList = Read();
returnValue = GetMessageReturnValue((string)tempMessageList[0]);
if(returnValue != 220)
{
Close();
throw new Exception((string)tempMessageList[0]);
}
//SEND USER
tempMessageList = SendCommand("USER " + user);
returnValue = GetMessageReturnValue((string)tempMessageList[0]);
if(!(returnValue == 331 || returnValue == 202))
{
Close();
throw new Exception((string)tempMessageList[0]);
}
//SEND PASSWORD
if(returnValue == 331)
{
tempMessageList = SendCommand("PASS " + password);
returnValue = GetMessageReturnValue((string)tempMessageList[0]);
if(!(returnValue == 230 || returnValue == 202))
{
Close();
throw new Exception((string)tempMessageList[0]);
}
}
}
public virtual void Close()
{
ArrayList messageList = new ArrayList();
if(this.tcpClient != null )
{
messageList = SendCommand("QUIT");
this.tcpClient.Close();
}
}
public ArrayList Dir(String mask)
{
ArrayList tmpList = Dir();
DataTable table = new DataTable();
table.Columns.Add("Name");
for(int i = 0; i < tmpList.Count; i++)
{
DataRow row = table.NewRow();
row["Name"] = (string)tmpList[i];
table.Rows.Add(row);
}
DataRow [] rowList = table.Select("Name LIKE '" + mask + "'", "", DataViewRowState.CurrentRows);
tmpList = new ArrayList();
for(int i = 0; i < rowList.Length; i++)
{
tmpList.Add((string)rowList[i]["Name"]);
}
return tmpList;
}
public ArrayList Dir()
{
LockTcpClient();
TcpListener listner = null;
TcpClient client = null;
NetworkStream networkStream = null;
ArrayList tempMessageList = new ArrayList();
int returnValue = 0;
string returnValueMessage = "";
ArrayList fileList = new ArrayList();
SetTransferType(FTPFileTransferType.ASCII);
if(this.mode == FTPMode.Active)
{
listner = CreateDataListner();
listner.Start();
}
else
{
client = CreateDataClient();
}
tempMessageList = new ArrayList();
tempMessageList = SendCommand("NLST");
returnValue = GetMessageReturnValue((string)tempMessageList[0]);
if(!(returnValue == 150 || returnValue == 125 || returnValue == 550))
{
throw new Exception((string)tempMessageList[0]);
}
if(returnValue == 550) //No files found
{
return fileList;
}
if(this.mode == FTPMode.Active)
{
client = listner.AcceptTcpClient();
}
networkStream = client.GetStream();
fileList = ReadLines(networkStream);
if(tempMessageList.Count == 1)
{
tempMessageList = Read();
returnValue = GetMessageReturnValue((string)tempMessageList[0]);
returnValueMessage = (string)tempMessageList[0];
}
else
{
returnValue = GetMessageReturnValue((string)tempMessageList[1]);
returnValueMessage = (string)tempMessageList[1];
}
if(!(returnValue == 226))
{
throw new Exception(returnValueMessage);
}
networkStream.Close();
client.Close();
if(this.mode == FTPMode.Active)
{
listner.Stop();
}
UnlockTcpClient();
return fileList;
}
public void SendStream(Stream stream, string remoteFileName, FTPFileTransferType type)
{
LockTcpClient();
TcpListener listner = null;
TcpClient client = null;
NetworkStream networkStream = null;
ArrayList tempMessageList = new ArrayList();
int returnValue = 0;
string returnValueMessage = "";
tempMessageList = new ArrayList();
SetTransferType(type);
if(this.mode == FTPMode.Active)
{
listner = CreateDataListner();
listner.Start();
}
else
{
client = CreateDataClient();
}
tempMessageList = SendCommand("STOR " + remoteFileName);
returnValue = GetMessageReturnValue((string)tempMessageList[0]);
if(!(returnValue == 150 || returnValue == 125))
{
throw new Exception((string)tempMessageList[0]);
}
if(this.mode == FTPMode.Active)
{
client = listner.AcceptTcpClient();
}
networkStream = client.GetStream();
Byte[] buffer = new Byte[BLOCK_SIZE];
int bytes = 0;
int totalBytes = 0;
while(totalBytes < stream.Length)
{
bytes = (int)stream.Read(buffer, 0, BLOCK_SIZE);
totalBytes = totalBytes + bytes;
networkStream.Write(buffer, 0, bytes);
}
networkStream.Close();
client.Close();
if(this.mode == FTPMode.Active)
{
listner.Stop();
}
if(tempMessageList.Count == 1)
{
tempMessageList = Read();
returnValue = GetMessageReturnValue((string)tempMessageList[0]);
returnValueMessage = (string)tempMessageList[0];
}
else
{
returnValue = GetMessageReturnValue((string)tempMessageList[1]);
returnValueMessage = (string)tempMessageList[1];
}
if(!(returnValue == 226))
{
throw new Exception(returnValueMessage);
}
UnlockTcpClient();
}
public virtual void SendFile(string localFileName, FTPFileTransferType type)
{
SendFile(localFileName, Path.GetFileName(localFileName), type);
}
public virtual void SendFile(string localFileName, string remoteFileName, FTPFileTransferType type)
{
FileStream fs = new FileStream(localFileName,FileMode.Open);
SendStream(fs, remoteFileName, type);
fs.Close();
}
public void GetStream(string remoteFileName, Stream stream, FTPFileTransferType type)
{
TcpListener listner = null;
TcpClient client = null;
NetworkStream networkStream = null;
ArrayList tempMessageList = new ArrayList();
int returnValue = 0;
string returnValueMessage = "";
LockTcpClient();
SetTransferType(type);
if(this.mode == FTPMode.Active)
{
listner = CreateDataListner();
listner.Start();
}
else
{
client = CreateDataClient();
}
tempMessageList = new ArrayList();
tempMessageList = SendCommand("RETR " + remoteFileName);
returnValue = GetMessageReturnValue((string)tempMessageList[0]);
if(!(returnValue == 150 || returnValue == 125))
{
throw new Exception((string)tempMessageList[0]);
}
if(this.mode == FTPMode.Active)
{
client = listner.AcceptTcpClient();
}
networkStream = client.GetStream();
Byte[] buffer = new Byte[BLOCK_SIZE];
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -