📄 ftpconnection.cs
字号:
int bytes = 0;
bool read = true;
while(read)
{
bytes = (int)networkStream.Read(buffer, 0, buffer.Length);
stream.Write(buffer, 0, bytes);
if(bytes == 0)
{
read = false;
}
}
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 GetFile(string remoteFileName, FTPFileTransferType type)
{
GetFile(remoteFileName, Path.GetFileName(remoteFileName), type);
}
public virtual void GetFile(string remoteFileName, string localFileName, FTPFileTransferType type)
{
FileStream fs = new FileStream(localFileName,FileMode.Create);
GetStream(remoteFileName, fs, type);
fs.Close();
}
public virtual void DeleteFile(String remoteFileName)
{
System.Threading.Monitor.Enter(this.tcpClient);
ArrayList tempMessageList = new ArrayList();
int returnValue = 0;
tempMessageList = SendCommand("DELE " + remoteFileName);
returnValue = GetMessageReturnValue((string)tempMessageList[0]);
if(returnValue != 250)
{
throw new Exception((string)tempMessageList[0]);
}
System.Threading.Monitor.Exit(this.tcpClient);
}
public virtual void MoveFile(string remoteFileName, string toRemotePath)
{
if(toRemotePath.Length > 0 && toRemotePath.Substring(toRemotePath.Length - 1, 1) != "/")
{
toRemotePath = toRemotePath + "/";
}
RenameFile(remoteFileName, toRemotePath + remoteFileName);
}
public virtual void RenameFile(string fromRemoteFileName, string toRemoteFileName)
{
System.Threading.Monitor.Enter(this.tcpClient);
ArrayList tempMessageList = new ArrayList();
int returnValue = 0;
tempMessageList = SendCommand("RNFR " + fromRemoteFileName);
returnValue = GetMessageReturnValue((string)tempMessageList[0]);
if(returnValue != 350)
{
throw new Exception((string)tempMessageList[0]);
}
tempMessageList = SendCommand("RNTO " + toRemoteFileName);
returnValue = GetMessageReturnValue((string)tempMessageList[0]);
if(returnValue != 250)
{
throw new Exception((string)tempMessageList[0]);
}
System.Threading.Monitor.Exit(this.tcpClient);
}
public virtual void SetCurrentDirectory(String remotePath)
{
LockTcpClient();
ArrayList tempMessageList = new ArrayList();
int returnValue = 0;
tempMessageList = SendCommand("CWD " + remotePath);
returnValue = GetMessageReturnValue((string)tempMessageList[0]);
if(returnValue != 250)
{
throw new Exception((string)tempMessageList[0]);
}
UnlockTcpClient();
}
private void SetTransferType(FTPFileTransferType type)
{
switch (type)
{
case FTPFileTransferType.ASCII:
SetMode("TYPE A");
break;
case FTPFileTransferType.Binary:
SetMode("TYPE I");
break;
default:
throw new Exception("Invalid File Transfer Type");
}
}
private void SetMode(string mode)
{
LockTcpClient();
ArrayList tempMessageList = new ArrayList();
int returnValue = 0;
tempMessageList = SendCommand(mode);
returnValue = GetMessageReturnValue((string)tempMessageList[0]);
if(returnValue != 200)
{
throw new Exception((string)tempMessageList[0]);
}
UnlockTcpClient();
}
private TcpListener CreateDataListner()
{
int port = GetPortNumber();
SetDataPort(port);
TcpListener listner = new TcpListener(port);
return listner;
}
private TcpClient CreateDataClient()
{
int port = GetPortNumber();
//IPEndPoint ep = new IPEndPoint(GetLocalAddressList()[0], port);
TcpClient client = new TcpClient();
//client.Connect(ep);
client.Connect(this.remoteHost, port);
return client;
}
private void SetDataPort(int portNumber)
{
LockTcpClient();
ArrayList tempMessageList = new ArrayList();
int returnValue = 0;
int portHigh = portNumber >> 8;
int portLow = portNumber & 255;
tempMessageList = SendCommand("PORT "
+ GetLocalAddressList()[0].ToString().Replace(".", ",")
+ "," + portHigh.ToString() + "," + portLow);
returnValue = GetMessageReturnValue((string)tempMessageList[0]);
if(returnValue != 200)
{
throw new Exception((string)tempMessageList[0]);
}
UnlockTcpClient();
}
public virtual void MakeDir(string directoryName)
{
LockTcpClient();
ArrayList tempMessageList = new ArrayList();
int returnValue = 0;
tempMessageList = SendCommand("MKD " + directoryName);
returnValue = GetMessageReturnValue((string)tempMessageList[0]);
if(returnValue != 257)
{
throw new Exception((string)tempMessageList[0]);
}
UnlockTcpClient();
}
public virtual void RemoveDir(string directoryName)
{
LockTcpClient();
ArrayList tempMessageList = new ArrayList();
int returnValue = 0;
tempMessageList = SendCommand("RMD " + directoryName);
returnValue = GetMessageReturnValue((string)tempMessageList[0]);
if(returnValue != 250)
{
throw new Exception((string)tempMessageList[0]);
}
UnlockTcpClient();
}
public ArrayList SendCommand(String command)
{
this.activeConnectionsCount++;
Byte[] cmdBytes = Encoding.ASCII.GetBytes((command+"\r\n").ToCharArray());
NetworkStream stream = this.tcpClient.GetStream();
stream.Write(cmdBytes, 0, cmdBytes.Length);
this.activeConnectionsCount--;
return Read();
}
private ArrayList Read ()
{
NetworkStream stream = this.tcpClient.GetStream();
ArrayList messageList = new ArrayList();
ArrayList tempMessage = ReadLines(stream);
int tryCount = 0;
while(tempMessage.Count == 0)
{
if(tryCount == 10)
{
throw new Exception("Server does not return message to the message");
}
Thread.Sleep(1000);
tryCount++;
tempMessage = ReadLines(stream);
}
while(((string)tempMessage[tempMessage.Count - 1]).Substring(3, 1) == "-")
{
messageList.AddRange(tempMessage);
tempMessage = ReadLines(stream);
}
messageList.AddRange(tempMessage);
AddMessagesToMessageList(messageList);
return messageList;
}
private ArrayList ReadLines(NetworkStream stream)
{
ArrayList messageList = new ArrayList();
char[] seperator = {'\n'};
char[] toRemove = {'\r'};
Byte[] buffer = new Byte[BLOCK_SIZE];
int bytes = 0;
string tmpMes = "";
while(stream.DataAvailable)
{
bytes = stream.Read(buffer, 0, buffer.Length);
tmpMes += Encoding.ASCII.GetString(buffer, 0, bytes);
}
string[] mess = tmpMes.Split(seperator);
for (int i = 0; i < mess.Length; i++)
{
if(mess[i].Length > 0)
{
messageList.Add(mess[i].Trim(toRemove));
}
}
return messageList;
}
private int GetMessageReturnValue(string message)
{
return int.Parse(message.Substring(0, 3));
}
private int GetPortNumber()
{
LockTcpClient();
int port = 0;
switch (this.mode)
{
case FTPMode.Active:
Random rnd = new Random((int)DateTime.Now.Ticks);
port = DATA_PORT_RANGE_FROM + rnd.Next(DATA_PORT_RANGE_TO - DATA_PORT_RANGE_FROM);
break;
case FTPMode.Passive:
ArrayList tempMessageList = new ArrayList();
int returnValue = 0;
tempMessageList = SendCommand("PASV");
returnValue = GetMessageReturnValue((string)tempMessageList[0]);
if(returnValue != 227)
{
if(((string)tempMessageList[0]).Length > 4)
{
throw new Exception((string)tempMessageList[0]);
}
else
{
throw new Exception((string)tempMessageList[0] + " Passive Mode not implemented");
}
}
string message = (string)tempMessageList[0];
int index1 = message.IndexOf(",", 0);
int index2 = message.IndexOf(",", index1 + 1);
int index3 = message.IndexOf(",", index2 + 1);
int index4 = message.IndexOf(",", index3 + 1);
int index5 = message.IndexOf(",", index4 + 1);
int index6 = message.IndexOf(")", index5 + 1);
port = 256 * int.Parse(message.Substring(index4 + 1, index5 - index4 - 1)) + int.Parse(message.Substring(index5 + 1, index6 - index5 - 1));
break;
}
UnlockTcpClient();
return port;
}
private void AddMessagesToMessageList(ArrayList messages)
{
if(this.logMessages)
{
this.messageList.AddRange(messages);
}
}
private IPAddress[] GetLocalAddressList()
{
return Dns.Resolve(Dns.GetHostName()).AddressList;
}
private void LockTcpClient()
{
System.Threading.Monitor.Enter(this.tcpClient);
}
private void UnlockTcpClient()
{
System.Threading.Monitor.Exit(this.tcpClient);
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -