网络资源--C#的一个简易的ftp服务器端(zz自C#网络应用案例导航)

using System;
 using System.IO;
 using System.Net;
 using System.Net.Sockets;
 using System.Threading;

 public class MainApp
 {
  public const String Version = "0.0.1";
  public static String szFtpRoot = @"E:/incoming";
  public static bool fDebug = true;

  //服务器管理界面:
  public static void Usage( )
  {
   Console.WriteLine("ftpd usage:");
   Console.WriteLine();
   //改变ftp目录
   Console.WriteLine("-r [path]/t:/tSpecifies FTP Root");
   //帮助
   Console.WriteLine("-?       /t:/tPrints this help");
   return;
  }

  public static int Main( String[] Arguments )
  {

   for( int i = 0; i < Arguments.Length; i++ )
   {

    switch( Arguments [ i ] )
    {
      //设定ftp目录
     case "-r":
      szFtpRoot = Arguments[ i + 1 ];
      break;
     case "-?":
      Usage();
      return 0;
     default:
      //无参数运行时ftp目录设为当前目录
      szFtpRoot = Directory.GetCurrentDirectory();
      break;
    }

   }
   //创建一个Ftpd对象实例
   Ftpd pFtpd = new Ftpd();
   //启动ftp服务
   if( pFtpd.StartServer() == false )
   {

    Console.WriteLine( "Failed to start FTP Server." );
    return -1;

   }

   return 0;

  }

 }
 //会话类,管理会话的信息
 public class SessionInfo
 {
  public bool fBinary = false;
  public bool fPassive = false;
  public String szFtpRoot = MainApp.szFtpRoot;
  public String szUsername;
  public PassiveInfo pi;
 }

 public struct PassiveInfo
 {
  //IPHostEntry类,属于System.Net名称空间,存储主机信息
  public IPHostEntry iphostentry;
  public TcpListener tcpListener;
  //32位无符号数,描述端口
  public Int32 iPort;
 }


 //Ftp服务器类
 public class Ftpd
 {
  //服务器接收一个客户请求后,产生的服务套接字
  public Socket s;
  public TcpListener TCPListener;
  //属于System.Net名称空间,用IP地址和端口号表示一个网络终端
  public IPEndPoint LocalIPEndPoint;
  public IPEndPoint localEP;
  public IPEndPoint remoteEP;

  public Ftpd()
  {
   //实例化一个在端口21监听的TCPListener
   TCPListener = new TcpListener(21);

  }
  //开始服务
  public bool StartServer()
  {

   try
   {
    //开始监听
    TCPListener.Start();
    //循环
    while( true )
    {
     //接收客户端的一个连接请求,返回一个套接字
     s = TCPListener.AcceptSocket();

     /* NOTE: Would be using System.Threading.ThreadPool(s) but they are not supported on 9x systems */
     //创建一个线程,处理当前连接
     Thread client = new Thread( new ThreadStart( ServeConnection ) );
     //线程开始运行
     client.Start();

    }

   }
   catch( SocketException Se )
   {
    DBG_TRACE( "An Socket Class Exception Has Occured" );
    DBG_TRACE( "Error: {0}", Se.ErrorCode );

    return false;
   }

  }

  //:::
  // Function:
  // Reply()
  //
  // Purpose:
  // Sends a status command back to the client, with a descriptive message.
  //
  // Parameters:
  // Socket sSocket - Client's Connected Socket
  // int iResponseCode - Response Code to Send
  // String szMessage - Message to send
  //:::

  public void Reply( Socket sSocket, int iResponseCode, String szMessage )
  {
   try
   {
    String szResponse = "" + iResponseCode + " " + szMessage + "/r/n";
    //将字符串转换成ASCII码
    Byte[] OutputBytes = GetBytes( szResponse );
    //通过套接字发送
    sSocket.Send( OutputBytes );
   }
   catch(Exception e)
   {
   }

   return;

  }


  //:::
  // Function:
  // GetBytes()
  //
  // Purpose:
  // Returns a Byte Array of the Bytes which formulate the String szMessage
  //
  // Parameters:
  // String szMessage - Message to Convert
  //
  //:::

  public Byte[] GetBytes( String szMessage )
  {
   //先把String类型的字符串转化为Unicode的字符数组,然后再转为ASCII码的Byte数组
   return System.Text.Encoding.ASCII.GetBytes( szMessage.ToCharArray() );
  }


  //:::
  // Function:
  // ServeConnection()
  //
  // Purpose:
  // 处理一个请求
  //:::

  public void ServeConnection( )
  {
   Socket sSocket = s;
   //创建一个会话
   SessionInfo si = new SessionInfo();
   //显示服务器版本信息
   String szMessage = " FTPD Server v" + MainApp.Version + "";
   //送到客户端显示
   Reply(sSocket, 220, szMessage);

   ParseInputs(sSocket, si);

   return;
  }
  //处理客户端发送到服务器的命令

  public void ParseInputs( Socket sSocket, SessionInfo si )
  {
   String Username = "";
   String Password = "";
   try
   {
    while( true )
    {
     Byte[] ReceivedBytes = new Byte[256];
     //接收到的信息放在一个Byte数组中
     sSocket.Receive( ReceivedBytes );

     String Command = "";
     String Parameter = "";

     for( int i = 0; i < 4; i++ )
     {

      //如果该ASCII码不是回车换行符则将它加到Command字符串末尾
      if( ReceivedBytes[i] != 0 && ReceivedBytes[i] != 13 && ReceivedBytes[i] != 10 && ReceivedBytes[i] != 32 )
      {

       Command += (char)ReceivedBytes[i];

      }

     }

     if( Command != "CWD" && Command != "PWD" )
     {

      for( int i = 5; i < ReceivedBytes.Length; i++ )
      {

       if( ReceivedBytes[i] != 0 && ReceivedBytes[i] != 13 && ReceivedBytes[i] != 10 )
       {

        //     DBG_TRACE("Char: {0} :: Byte {1}", (char)ReceivedBytes[i], ReceivedBytes[i]);

        Parameter += (char)ReceivedBytes[i];

       }

      }

     }

     else
     {

      for( int i = 4; i < ReceivedBytes.Length; i++ )
      {

       if( Command != "CWD" )
       {

        if( ReceivedBytes[i] != 0 && ReceivedBytes[i] != 13 && ReceivedBytes[i] != 10 )
        {

         //     DBG_TRACE( "Char: {0} :: Byte {1}", (char)ReceivedBytes[i], ReceivedBytes[i] );
         Parameter += (char)ReceivedBytes[i];

        }

       }

       else
       {

        if( ReceivedBytes[i] != 0 && ReceivedBytes[i] != 13 && ReceivedBytes[i] != 10 )
        {

         //   DBG_TRACE("Char: {0} :: Byte {1}", (char)ReceivedBytes[i], ReceivedBytes[i]);

         Parameter += (char)ReceivedBytes[i];

        }

       }

      }

     }

     Parameter.Trim();
     System.Console.Write("From "+sSocket.RemoteEndPoint+" : "+Command+" "+Parameter+"/n");
     //客户端传给服务器的ftp命令都是这样的格式:前四位为命令类型,后面为命令参数
     switch( Command )
     {
       //用户
      case "USER":
       Username = Parameter;
       Reply( sSocket, 331, "Password required for " + Parameter + "." );
       break;
       //密码:
      case "PASS":
       Password = Parameter;
       AuthenticateUser( Username, Password, sSocket );
       break;
       //
      case "EPSV":
       Reply( sSocket, 522, "Extended Passive Mode not supported." );
       break;
       //Passive模式
      case "PASV":
       ProcessPassiveCommand( sSocket, Parameter, si );
       break;
      case "PORT":
       ProcessPortCommand( sSocket,Parameter );
       break;
       //文件及子目录列表
      case "LIST":
       ProcessListCommand( sSocket,Parameter, si );
       break;
       //发送客户端下载的文件
      case "RETR":
       ProcessRetreiveCommand( sSocket, Parameter, si );
       break;
      case "NLST":
       ProcessListCommand( sSocket,Parameter, si );
       break;
      case "SYST":
       Reply( sSocket, 215, ".NET" );
       break;
      case "STOR":
       ProcessStoreCommand( sSocket, Parameter, si );
       break;
       //改变工作目录
      case "CWD":
       ProcessCWDCommand( sSocket, Parameter, si );
       break;
      case "CDUP":
       si.szFtpRoot = MainApp.szFtpRoot;
       Reply( sSocket, 250, "CWD Command successful" );
       break;
       //当前工作目录
      case "XPWD":
       ProcessPWDCommand( sSocket, Parameter, si );
       break;
       //传输模式:Binary或ASCII
      case "TYPE":
      switch( Parameter )
      {
       case "I":
        si.fBinary = true;
        Reply( sSocket, 200, "Type set to I." );
        break;
       case "A":
        si.fBinary = false;
        Reply( sSocket, 200, "Type set to A." );
        break;
       default:
        break;
      }
       break;
       //当前工作目录
      case "PWD":
       ProcessPWDCommand( sSocket, Parameter, si );
       break;
       //退出
      case "QUIT":
       sSocket.Close();
       return;
      default:
       Reply( sSocket, 502, "'" + Command + "': not implemented." );
       break;
     }

    }
   }
   catch(Exception e)
   {
   }

  }
  //Passive模式,该模式下,服务器会为每一个会话提供一个TcpListener监听
  public void ProcessPassiveCommand( Socket sSocket, String Parameter, SessionInfo si )
  {
   //得到主机地址:
   IPHostEntry LocalHostEntry = Dns.GetHostByName(Dns.GetHostName());
   //为服务请求在服务端指定一个端口
   Random iRand = new Random();
   Int32 iRandOne = iRand.Next( 0,100 );
   Int32 iRandTwo = iRand.Next( 2,200 );
   Int32 iPort = (iRandOne << 8) | (iRandTwo);

   String[] tmp = LocalHostEntry.AddressList[0].ToString().Split( '.' );
   //Ip地址和端口
   String szPasvReply = "" + tmp[0] + "," + tmp[1] + "," + tmp[2] + "," + tmp[3] + "," + iRandOne + "," + iRandTwo;

   TcpListener tcpListener = new TcpListener(iPort);

   si.pi.iphostentry = LocalHostEntry;
   si.pi.iPort = iPort;
   si.pi.tcpListener = tcpListener;
   si.fPassive = true;
   si.pi.tcpListener.Start();

   IPEndPoint LocalEndPoint = new IPEndPoint( Dns.GetHostByAddress( "127.0.0.1" ).AddressList[0], iPort );
   localEP = LocalEndPoint;

   Reply( sSocket, 227, szPasvReply );
   return;

  }
  //改变目录
  public void ProcessCWDCommand( Socket sSocket, String Parameter, SessionInfo si )
  {
   try
   {
    char[] chTmp = Parameter.ToCharArray(); // Check for initial '/'
    const Byte byteSlash = (byte)'/';
    const Byte byteBackSlash = (byte)'//';
    String szNewDir = "";
    DirectoryInfo tmp;

    if( chTmp[0] == (char)byteSlash )
    {
     tmp = new DirectoryInfo( MainApp.szFtpRoot + Parameter );
    }
    else
    {

     chTmp = ( si.szFtpRoot + @"/" + Parameter ).ToCharArray();
     for( int i = 0; i < chTmp.Length; i++ )
     {
      switch( chTmp[i] )
      {
       case (char)byteBackSlash:
        szNewDir += @"//";
        break;
       default:
        szNewDir += chTmp[i];
        break;
      }
     }

     tmp = new DirectoryInfo( szNewDir );

    }

    tmp.GetFiles(); // 测试指定目录是否存在,如果不存在将抛出一个DirNotFoundException

    si.szFtpRoot = tmp.FullName;
    Reply( sSocket, 250, "CWD Command successful" );
   }
   catch( DirectoryNotFoundException )
   {
    Reply( sSocket, 550, "" + Parameter + ": No such file or directory" );
   }
   catch( IOException )
   {
    Reply( sSocket, 550, "" + Parameter + ": Not a directory." ); // Someone typed a filename
   }
   catch( ArgumentException )
   {
    Reply(sSocket, 550, "'" + Parameter + "': illegal characters in file/directory name.");
   }

   return;

  }
  //返回当前工作目录
  public void ProcessPWDCommand(Socket sSocket, String Parameter, SessionInfo si)
  {
   Byte byteSlash = 92;

   String[] tmp = si.szFtpRoot.Split((char)byteSlash);
   String cwd = "/";

   for(int i = 1; i < tmp.Length; i++)
   {
    cwd += tmp[i];
    cwd += @"/";
   }
   Reply(sSocket, 257, "'" + cwd + "' is the current directory.");
   return;
  }


  //当前目录下所有文件和文件夹的列表
  public void ProcessListCommand(Socket sSocket, String Parameter, SessionInfo si)
  {

   String[] DirList = Directory.GetDirectories(si.szFtpRoot);
   String szDirList = "";
   DirectoryInfo curDir = new DirectoryInfo(si.szFtpRoot);
   FileInfo[] szFileListArray = curDir.GetFiles();
   String szFileList = "";

   for(int i = 0; i < DirList.Length; i++ )
   {
    DirectoryInfo tmp = new DirectoryInfo(DirList[i]);
    szDirList += tmp.Name;
    szDirList += "/r/n";
   }

   // Now Files

   for(int i = 0; i < szFileListArray.Length; i++)
   {
    szFileList += szFileListArray[i].Name;
    szFileList += "/r/n";
   }

   szDirList += szFileList;


   Reply(sSocket, 150, "Opening ASCII Mode Data Connection for 'file list'");

   if(si.fPassive)
   {
    SendOverPassiveDataConnection(GetBytes( szDirList ), si);
   }
   else
   {
    SendOverDataConnection( GetBytes( szDirList ) );
   }

   Reply(sSocket, 226, "Transfer complete.");

   return;
  }
  //主动模式
  public bool SendOverDataConnection(Byte[] pBytes)
  {
   //
   // Data Connection Socket. See the ProcessPortCommand Function
   // for more information
   //

   try
   {
    Socket s1 = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);

    s1.Bind(localEP);
    s1.Connect(remoteEP);
    s1.Send(pBytes);
    s1.Close();
   }
   catch(SocketException)
   {
    return false;
   }

   return true;
  }

  public bool SendOverPassiveDataConnection(Byte[] pBytes, SessionInfo si)
  {

   if(si.pi.tcpListener.Pending() == true)
   {
    Socket s1 = si.pi.tcpListener.AcceptSocket();

    s1.Send(pBytes);
    s1.Close();
   }

   return true;

  }

  public void ProcessStoreCommand(Socket sSocket, String Parameter, SessionInfo si)
  {
   try
   {
    FileStream pStream = new FileStream( si.szFtpRoot + @"/" + Parameter, FileMode.Create);
    Socket s1 = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);


    if(si.fBinary)
    {
     Reply( sSocket, 150, "Opening BINARY mode data connection for '" + Parameter + "'");
    }
    else
    {
     Reply( sSocket, 150, "Opening ASCII mode data connection for '" + Parameter + "'");
    }

    Socket sBinarySocket;

    if(si.fPassive)
    {

     sBinarySocket = si.pi.tcpListener.AcceptSocket();

    }
    else
    {

     sBinarySocket = s1;
     sBinarySocket.Bind(localEP);
     sBinarySocket.Connect(remoteEP);
    }

    Byte[] ReceivedBytes = new Byte[1];
    BinaryWriter bw = new BinaryWriter(pStream);
    StreamWriter bs = new StreamWriter(pStream);

    while(sBinarySocket.Receive(ReceivedBytes) > 0)
    {
     if(si.fBinary)
     {

      bw.Write(ReceivedBytes);
     }
     else
     {

      for(int i = 0; i < ReceivedBytes.Length; i++)
      {
       bs.Write((char)ReceivedBytes[i]);
      }
     }
    }

    bs.Close();
    bw.Close();
    pStream.Close();
    sBinarySocket.Close();

    Reply( sSocket, 250, "Transfer complete");

   }
   catch( Exception Ex )
   {
    Console.Write("" + Ex.Message);
   }

  }


  public void ProcessRetreiveCommand(Socket sSocket, String Parameter, SessionInfo si)
  {
   try
   {
    FileStream pStream = new FileStream(si.szFtpRoot + @"/" + Parameter, FileMode.Open);
    //               Socket s1 = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);

    if(si.fBinary)
    {
     Reply(sSocket, 150, "Opening BINARY mode data connection for '" + Parameter + "'");

     BinaryReader br = new BinaryReader(pStream);

     Socket sBinarySocket;

     if(si.fPassive)
     {
      sBinarySocket = si.pi.tcpListener.AcceptSocket();
     }
     else
     {
      sBinarySocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
      sBinarySocket.Bind(localEP);
      sBinarySocket.Connect(remoteEP);
     }

     while(br.PeekChar() > -1)
     {
      sBinarySocket.Send(br.ReadBytes(1024));
     }


     br.Close();

     pStream.Close();

     sBinarySocket.Close();

    }
    else
    {
     Reply(sSocket, 150, "Opening ASCII mode data connection for '" + Parameter + "'");
     StreamReader sr = new StreamReader(pStream);
     if(si.fPassive)
     {
      SendOverPassiveDataConnection(GetBytes(sr.ReadToEnd()), si);
     }
     else
     {
      SendOverDataConnection(GetBytes(sr.ReadToEnd()));
     }

     sr.Close();
    }
   }
   catch(Exception Ex)
   {
    DBG_TRACE(Ex.Message);
   }


   Reply(sSocket, 200, "Transfer Complete");
   return;
  }

  public void ProcessPortCommand(Socket sSocket, String Parameter)
  {
   //
   // PORT command -
   // PORT is used to denote the Data Port the client wishes to recieve
   // data on. We have to send all data requests back to the client via this
   // port...

   String[] tmp = Parameter.Split( ',' );

   String szIP = "" + tmp[0] + "." + tmp[1] + "." + tmp[2] + "." + tmp[3];

   int iPortNum = (int.Parse(tmp[4]) << 8) | int.Parse(tmp[5]);

   Random iRand = new Random();
   Int32 iPort = iRand.Next(0,30000);

   IPEndPoint LocalEndPoint = new IPEndPoint( Dns.GetHostByAddress( "127.0.0.1" ).AddressList[0], iPort );

   IPEndPoint RemoteEndPoint = new IPEndPoint( Dns.GetHostByAddress(szIP).AddressList[0], iPortNum );

   localEP = LocalEndPoint;
   remoteEP = RemoteEndPoint;

   Reply(sSocket, 200, "PORT Command successful.");

   return;
  }

  public void AuthenticateUser(String Username, String Password, Socket sSocket)
  {


   //
   // TODO: Integrate w/ NTLM if running on NT, Some new Password Scheme on 9x
   //
   // For now just let anyone in
   //

   Reply(sSocket, 230, "Login Successful.");

   return;
  }


  //
  // Debugging Routines
  //

  public void DumpSocket(Socket sSocket)
  {
   DBG_TRACE("AddressFamily: {0}", sSocket.AddressFamily);
   DBG_TRACE("Available: {0}", sSocket.Available);
   DBG_TRACE("Blocking: {0}", sSocket.Blocking);
   DBG_TRACE("Connection: {0}", sSocket.Connected);
   DBG_TRACE("Handle: {0}", sSocket.Handle);
   DBG_TRACE("LocalEndPoint: {0}", sSocket.LocalEndPoint);
   DBG_TRACE("ProtocolType: {0}", sSocket.ProtocolType);
  }

  public void DBG_TRACE(String szMessage)
  {
   if(MainApp.fDebug)
   {
    Console.WriteLine("DEBUG: " + szMessage);
   }
  }

  public void DBG_TRACE(String szMessage, Object Parm1)
  {
   if(MainApp.fDebug)
   {
    Console.WriteLine("DEBUG: " + szMessage, Parm1);
   }
  }

  public void DBG_TRACE(String szMessage, Object Parm1, Object Parm2)
  {
   if(MainApp.fDebug)
   {
    Console.WriteLine("DEBUG: " + szMessage, Parm1, Parm2);
   }
  }

  public void DBG_TRACE(String szMessage, Object Parm1, Object Parm2, Object Parm3)
  {
   if(MainApp.fDebug)
   {
    Console.WriteLine("DEBUG: " + szMessage, Parm1, Parm2, Parm3);
   }
  }

  public void DBG_TRACE(String szMessage, Object Parm1, Object Parm2, Object Parm3, Object Parm4)
  {
   if(MainApp.fDebug)
   {
    Console.WriteLine("DEBUG: " + szMessage, Parm1, Parm2, Parm3, Parm4);
   }
  }

 }

用VS编写的FTP服务器软件,C#网络程序编程学习用。 代码: using System; using System.Collections.Generic; using System.Globalization; using System.IO; using System.Net; using System.Net.Sockets; using System.Threading; using System.Windows.Forms; namespace FtpServer { public partial class FtpServerForm : Form { TcpListener myTcpListener = null; private Thread listenThread; // 保存用户名和密码 Dictionary users; public FtpServerForm() { InitializeComponent(); // 初始化用户名和密码 users = new Dictionary(); users.Add("admin", "admin"); // 设置默认的主目录 tbxFtpRoot.Text = "F:/MyFtpServerRoot/"; IPAddress[] ips = Dns.GetHostAddresses(""); tbxFtpServerIp.Text = ips[5].ToString(); tbxFtpServerPort.Text = "21"; lstboxStatus.Enabled = false; } // 启动服务器 private void btnFtpServerStartStop_Click(object sender, EventArgs e) { if (myTcpListener == null) { listenThread = new Thread(ListenClientConnect); listenThread.IsBackground = true; listenThread.Start(); lstboxStatus.Enabled = true; lstboxStatus.Items.Clear(); lstboxStatus.Items.Add("已经启动Ftp服务..."); btnFtpServerStartStop.Text = "停止"; } else { myTcpListener.Stop(); myTcpListener = null; listenThread.Abort(); lstboxStatus.Items.Add("Ftp服务已停止!"); lstboxStatus.TopIndex = lstboxStatus.Items.Count - 1; btnFtpServerStartStop.Text = "启动"; } } // 监听口,处理客户连接 private void ListenClientConnect() { myTcpListener = new TcpListener(IPAddress.Parse(tbxFtpServerIp.Text), int.Parse(tbxFtpServerPort.Text)); // 开始监听传入的请求 myTcpListener.Start(); AddInfo("启动FTP服务成功!"); AddInfo("Ftp服务器运行中...[点击”停止“按钮停止FTP服务]"); while (true) { try { // 接收连接请求 TcpClient tcpClient = myTcpListener.AcceptTcpClient(); AddInfo(string.Format("客户({0})与本机({1})建立Ftp连接", tcpClient.Client.RemoteEndPoint, myTcpListener.LocalEndpoint)); User user = new User(); user.commandSession = new UserSeesion(tcpClient); user.workDir = tbxFtpRoot.Text; Thread t = new Thread(UserProcessing); t.IsBackground = true; t.Start(user); } catch { break; } } } // 处理客户用户请求 private void UserProcessing(object obj) { User user = (User)obj; string sendString = "220 FTP Server v1.0"; RepleyCommandToUser(user, sendString); while (true) { string receiveString = null; try { // 读取客户发来的请求信息 receiveString = user.commandSession.streamReader.ReadLine(); } catch(Exception ex) { if (user.commandSession.tcpClient.Connected == false) { AddInfo(string.Format("客户({0}断开连接!)", user.commandSession.tcpClient.Client.RemoteEndPoint)); } else { AddInfo("接收命令失败!" + ex.Message); } break; } if (receiveString == null) { AddInfo("接收字符串为null,结束线程!"); break; } AddInfo(string.Format("来自{0}:[{1}]", user.commandSession.tcpClient.Client.RemoteEndPoint, receiveString)); // 分解客户发来的控制信息中的命令和参数 string command = receiveString; string param = string.Empty; int index = receiveString.IndexOf(' '); if (index != -1) { command = receiveString.Substring(0, index).ToUpper(); param = receiveString.Substring(command.Length).Trim(); } // 处理不需登录即可响应的命令(这里只处理QUIT) if (command == "QUIT") { // 关闭TCP连接并释放与其关联的所有资源 user.commandSession.Close(); return; } else { switch (user.loginOK) { // 等待用户输入用户名: case 0: CommandUser(user, command, param); break; // 等待用户输入密码 case 1: CommandPassword(user, command, param); break; // 用户名和密码验证正确后登陆 case 2: switch (command) { case "CWD": CommandCWD(user, param); break; case "PWD": CommandPWD(user); break; case "PASV": CommandPASV(user); break; case "PORT": CommandPORT(user, param); break; case "LIST": CommandLIST(user, param); break; case "NLIST": CommandLIST(user, param); break; // 处理下载文件命令 case "RETR": CommandRETR(user, param); break; // 处理上传文件命令 case "STOR": CommandSTOR(user, param); break; // 处理删除命令 case "DELE": CommandDELE(user, param); break; // 使用Type命令在ASCII和二进制模式进行变换 case "TYPE": CommandTYPE(user, param); break; default: sendString = "502 command is not implemented."; RepleyCommandToUser(user, sendString); break; } break; } } } } // 想客户返回响应码 private void RepleyCommandToUser(User user, string str) { try { user.commandSession.streamWriter.WriteLine(str); AddInfo(string.Format("向客户({0})发送[{1}]", user.commandSession.tcpClient.Client.RemoteEndPoint, str)); } catch { AddInfo(string.Format("向客户({0})发送信息失败", user.commandSession.tcpClient.Client.RemoteEndPoint)); } } // 向屏幕输出显示状态信息(这里使用了委托机制) private delegate void AddInfoDelegate(string str); private void AddInfo(string str) { // 如果调用AddInfo()方法的线程与创建ListView控件的线程不在一个线程时 // 此时利用委托在创建ListView的线程上调用 if (lstboxStatus.InvokeRequired == true) { AddInfoDelegate d = new AddInfoDelegate(AddInfo); this.Invoke(d, str); } else { lstboxStatus.Items.Add(str); lstboxStatus.TopIndex = lstboxStatus.Items.Count - 1; lstboxStatus.ClearSelected(); } } #region 处理各个命令 #region 登录过程,即用户身份验证过程 // 处理USER命令,接收用户名但不进行验证 private void CommandUser(User user, string command, string param) { string sendString = string.Empty; if (command == "USER") { sendString = "331 USER command OK, password required."; user.userName = param; // 设置loginOk=1为了确保后面紧接的要求输入密码 // 1表示已接收到用户名,等到接收密码 user.loginOK = 1; } else { sendString = "501 USER command syntax error."; } RepleyCommandToUser(user, sendString); } // 处理PASS命令,验证用户名和密码 private void CommandPassword(User user, string command, string param) { string sendString = string.Empty; if (command == "PASS") { string password = null; if (users.TryGetValue(user.userName, out password)) { if (password == param) { sendString = "230 User logged in success"; // 2表示登录成功 user.loginOK = 2; } else { sendString = "530 Password incorrect."; } } else { sendString = "530 User name or password incorrect."; } } else { sendString = "501 PASS command Syntax error."; } RepleyCommandToUser(user, sendString); // 用户当前工作目录 user.currentDir = user.workDir; } #endregion #region 文件管理命令 // 处理CWD命令,改变工作目录 private void CommandCWD(User user, string temp) { string sendString = string.Empty; try { string dir = user.workDir.TrimEnd('/') + temp; // 是否为当前目录的子目录,且不包含父目录名称 if (Directory.Exists(dir)) { user.currentDir = dir; sendString = "250 Directory changed to '" + dir + "' successfully"; } else { sendString = "550 Directory '" + dir + "' does not exist"; } } catch { sendString = "502 Directory changed unsuccessfully"; } RepleyCommandToUser(user,sendString); } // 处理PWD命令,显示工作目录 private void CommandPWD(User user) { string sendString = string.Empty; sendString = "257 '" + user.currentDir + "' is the current directory"; RepleyCommandToUser(user, sendString); } // 处理LIST/NLIST命令,想客户发送当前或指定目录下的所有文件名和子目录名 private void CommandLIST(User user, string parameter) { string sendString = string.Empty; DateTimeFormatInfo dateTimeFormat = new CultureInfo("en-US", true).DateTimeFormat; // 得到目录列表 string[] dir = Directory.GetDirectories(user.currentDir); if (string.IsNullOrEmpty(parameter) == false) { if (Directory.Exists(user.currentDir + parameter)) { dir = Directory.GetDirectories(user.currentDir + parameter); } else { string s = user.currentDir.TrimEnd('/'); user.currentDir = s.Substring(0, s.LastIndexOf("/") + 1); } } for (int i = 0; i < dir.Length; i++) { string folderName = Path.GetFileName(dir[i]); DirectoryInfo d = new DirectoryInfo(dir[i]); // 按下面的格式输出目录列表 sendString += @"dwr-\t" + Dns.GetHostName() + "\t" + dateTimeFormat.GetAbbreviatedMonthName(d.CreationTime.Month) + d.CreationTime.ToString(" dd yyyy") + "\t" + folderName + Environment.NewLine; } // 得到文件列表 string[] files = Directory.GetFiles(user.currentDir); if (string.IsNullOrEmpty(parameter) == false) { if (Directory.Exists(user.currentDir + parameter + "/")) { files = Directory.GetFiles(user.currentDir + parameter + "/"); } } for (int i = 0; i 1024的随机口 // 下面这个运算算法只是为了得到一个大于1024的口值 port = random1 << 8 | random2; try { user.dataListener = new TcpListener(localip, port); AddInfo("TCP 数据连接已打开(被动模式)--" + localip.ToString() + ":" + port); } catch { continue; } user.isPassive = true; string temp = localip.ToString().Replace('.', ','); // 必须把口号IP地址告诉客户,客户接收到响应命令后, // 再通过新的口连接服务器的口P,然后进行文件数据传输 sendString = "227 Entering Passive Mode(" + temp + "," + random1 + "," + random2 + ")"; RepleyCommandToUser(user, sendString); user.dataListener.Start(); break; } } // 处理PORT命令,使用主动模式进行传输 private void CommandPORT(User user, string portstring) { // 主动模式时,客户必须告知服务器接收数据的口号,PORT 命令格式为:PORT address // address参数的格式为i1、i2、i3、i4、p1、p2,其中i1、i2、i3、i4表示IP地址 // 下面通过.字符串来组合这四个参数得到IP地址 // p1、p2表示口号,下面通过int.Parse(temp[4]) << 8) | int.Parse(temp[5] // 这个算法来获得一个大于1024的口来发送给服务器 string sendString = string.Empty; string[] temp = portstring.Split(','); string ipString = "" + temp[0] + "." + temp[1] + "." + temp[2] + "." + temp[3]; // 客户发出PORT命令把客户的IP地址和随机的口告诉服务器 int portNum = (int.Parse(temp[4]) < 0) { user.dataSession.binaryWriter.Write(bytes, 0, count); user.dataSession.binaryWriter.Flush(); count = binaryReader.Read(bytes, 0, bytes.Length); } } else { StreamReader streamReader = new StreamReader(fs); while (streamReader.Peek() > -1) { user.dataSession.streamWriter.WriteLine(streamReader.ReadLine()); } } AddInfo("...]发送完毕!"); } finally { user.dataSession.Close(); fs.Close(); } } // 使用数据连接接收文件流(客户发送上传文件功能) private void ReadFileByUserSession(User user, FileStream fs) { AddInfo("接收用户上传数据(文件流):[..."); try { if (user.isBinary) { byte[] bytes = new byte[1024]; BinaryWriter binaryWriter = new BinaryWriter(fs); int count = user.dataSession.binaryReader.Read(bytes, 0, bytes.Length); while (count > 0) { binaryWriter.Write(bytes, 0, count); binaryWriter.Flush(); count = user.dataSession.binaryReader.Read(bytes, 0, bytes.Length); } } else { StreamWriter streamWriter = new StreamWriter(fs); while (user.dataSession.streamReader.Peek() > -1) { streamWriter.Write(user.dataSession.streamReader.ReadLine()); streamWriter.Flush(); } } AddInfo("...]接收完毕"); } finally { user.dataSession.Close(); fs.Close(); } } private void label3_Click(object sender, EventArgs e) { } } }
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值