会回你第三个
private void round()
{
NetworkStream netStream = new NetworkStream(newClient);
WriteToNetStream(ref netStream,"220 FTP Server Ready!");
while(true)
{
//接受信息++++++
byte[] byteMessage = new byte[1024];
int i = newClient.Receive(byteMessage,byteMessage.Length,0);
string ss = System.Text.Encoding.ASCII.GetString(byteMessage);
int x = ss.IndexOf("\r\n",0);
int y = ss.IndexOf(" " ,0);
string commMessage = System.Text.Encoding.ASCII.GetString(byteMessage,0,x);
if(y!=-1)
{
command = commMessage.Substring(0,y);
}
else
{
command = commMessage.Substring(0,x);
}
command = command.ToLower();
if (y!=-1)
{
parameter = commMessage.Substring(y+1,x-y-1);
parameter = parameter.ToLower();
}
else
{
parameter = "";
}
this.richTextBox1.AppendText("Command=" + command + "," + "Parameter=" +parameter + "\r\n");
commMessage.Remove(0,commMessage.Length);
if(command=="user")
{
try
{
WriteToNetStream(ref netStream,"331 User name okay, need password.");
}
catch
{
WriteToNetStream(ref netStream,"not done!");
}
}
else if(command=="pass")
{
try
{
WriteToNetStream(ref netStream,"230 User logged in, proceed.");
}
catch
{
WriteToNetStream(ref netStream,"not done!");
}
}
else if(command =="list")
{
try
{
string[] str = new string[1024];
string dir = null;
for(int k=0;k<Directory.GetDirectories(parameter).Length;k++)
{
str[k] = Directory.GetDirectories(parameter)[k];
dir = dir + "目录:" + str[k] + "\r\n";
}
for(int k=0;k<Directory.GetFiles(parameter).Length;k++)
{
str[k] = Directory.GetFiles(parameter)[k];
dir+="文件:" + str[k] + "\r\n";
}
WriteToNetStream(ref netStream,"125 the files and directory is here");
sendDir(ref netStream,dir);
}
catch
{
WriteToNetStream(ref netStream,"502 unsuccessful,try again");
}
}
else if(command=="retr")//获得文件,下载
{
try
{
if ((pasv==false)&&(pasvPort==false))
{
string path = parameter;
//在新的端口监听,进行数据转输(文件下载)
newlistener = new TcpListener(Int32.Parse(textBox1.Text)+1);
newlistener.Start();
WriteToNetStream(ref netStream,"150 data link listening");
Socket nextLink = newlistener.AcceptSocket();
if(nextLink.Connected)
{
WriteToNetStream(ref netStream,"125 now thie file's data will be sended");
NetworkStream stream = new NetworkStream(nextLink);
transfer(ref stream,path);
stream.Close();
nextLink.Close();
newlistener.Stop();
}
}
else if((pasv==true)&&(pasvPort==true))
{
string path=parameter;
newlistener = new TcpListener(newport);
newlistener.Start();
WriteToNetStream(ref netStream,"150 data link listening");
Socket nextLink = newlistener.AcceptSocket();
if(nextLink.Connected)
{
WriteToNetStream(ref netStream,"125 now the file's data while be sended");
NetworkStream stream = new NetworkStream(nextLink);
transfer(ref stream,path);
stream.Close();
nextLink.Close();
newlistener.Stop();
pasv=false;
pasvPort=false;
}
}
else
{
WriteToNetStream(ref netStream,"350 PASV OR PORT command needed");
}
}
catch
{
WriteToNetStream(ref netStream,"502 performed unsuccessful,try again");
}
}
else if(command=="pasv")
{
try
{
pasv = true;
WriteToNetStream(ref netStream,"227 now the server pasv");
}
catch
{
WriteToNetStream(ref netStream,"502 performed unsuccessful,try again");
}
}
else if (command=="port")
{
try
{
if (pasv==true)
{
pasvPort=true;
newport=Int32.Parse(parameter);
WriteToNetStream(ref netStream,"200 new data link listen begin");
}
else
{
WriteToNetStream(ref netStream,"300 PASV command needed");
}
}
catch
{
WriteToNetStream(ref netStream,"504 the command can't be performed with the parameter");
}
}
else if(command=="help")
{
try
{
WriteToNetStream(ref netStream,"215 the FTP system ready,it can the comman such aa list PORT, PASV,");
}
catch
{
WriteToNetStream(ref netStream,"502 performed unsuccessful,try again");
}
}
else if (command=="quit")
{
try
{
WriteToNetStream(ref netStream,"221 commection Closed");
newClient.Close();
}
catch
{
WriteToNetStream(ref netStream,"502 performed unsuccessful,try again");
}
}
else
{
WriteToNetStream(ref netStream,"500 unrecognized command");
}
}
}
可以看出就是先判断前面的命令集是什么然后再判断parameter