zoukankan      html  css  js  c++  java
  • FtpClient ReplyCode 意义及 FtpClientHelper辅助类

    110 重新启动标记应答。
    120 服务在多久时间内ready。
    125 数据链路埠开启,准备传送。
    150 文件状态正常,开启数据连接端口。
    200 命令执行成功。
    202 命令执行失败。
    211 系统状态或是系统求助响应。
    212 目录的状态。
    213 文件的状态。
    214 求助的讯息。
    215 名称系统类型。
    220 新的联机服务ready。
    221 服务的控制连接埠关闭,可以注销。
    225 数据连结开启,但无传输动作。
    226 关闭数据连接端口,请求的文件操作成功。
    227 进入passive mode。
    230 使用者登入。
    250 请求的文件操作完成。
    257 显示目前的路径名称。
    331 用户名称正确,需要密码。
    332 登入时需要账号信息。
    350 请求的操作需要进一部的命令。
    421 无法提供服务,关闭控制连结。
    425 无法开启数据链路。
    426 关闭联机,终止传输。

    450 请求的操作未执行。 
    451 命令终止:有本地的错误。 
    452 未执行命令:磁盘空间不足。 
    500 格式错误,无法识别命令。 
    501 参数语法错误。 
    502 命令执行失败。 
    503 命令顺序错误。 
    504 命令所接的参数不正确。 
    530 未登入。 
    532 储存文件需要账户登入。 
    550 未执行请求的操作。 
    551 请求的命令终止,类

      1     public class FtpClient
      2     {
      3         public static object obj = new object();
      4 
      5         #region 构造函数
      6         /// <summary>
      7         /// 缺省构造函数
      8         /// </summary>
      9         public FtpClient()
     10         {
     11             strRemoteHost = "10.132.26.86";
     12             strRemotePath = "00555";
     13             strRemoteUser = "uname";
     14             strRemotePass = "upass";
     15             strRemotePort = 21;
     16             bConnected = false;
     17         }
     18 
     19         /// <summary>
     20         /// 构造函数
     21         /// </summary>
     22         public FtpClient(string remoteHost, string remotePath, string remoteUser, string remotePass, int remotePort)
     23         {
     24             strRemoteHost = remoteHost;
     25             strRemotePath = remotePath;
     26             strRemoteUser = remoteUser;
     27             strRemotePass = remotePass;
     28             strRemotePort = remotePort;
     29             Connect();
     30         }
     31 
     32         #endregion
     33 
     34         #region 字段
     35         private int strRemotePort;
     36         private Boolean bConnected;
     37         private string strRemoteHost;
     38         private string strRemotePass;
     39         private string strRemoteUser;
     40         private string strRemotePath;
     41 
     42         /// <summary>
     43         /// 服务器返回的应答信息(包含应答码)
     44         /// </summary>
     45         private string strMsg;
     46         /// <summary>
     47         /// 服务器返回的应答信息(包含应答码)
     48         /// </summary>
     49         private string strReply;
     50         /// <summary>
     51         /// 服务器返回的应答码
     52         /// </summary>
     53         private int iReplyCode;
     54         /// <summary>
     55         /// 进行控制连接的socket
     56         /// </summary>
     57         private Socket socketControl;
     58         /// <summary>
     59         /// 传输模式
     60         /// </summary>
     61         private TransferType trType;
     62         /// <summary>
     63         /// 接收和发送数据的缓冲区
     64         /// </summary>
     65         private static int BLOCK_SIZE = 512;
     66         /// <summary>
     67         /// 编码方式
     68         /// </summary>
     69         Encoding ASCII = Encoding.ASCII;
     70         /// <summary>
     71         /// 字节数组
     72         /// </summary>
     73         Byte[] buffer = new Byte[BLOCK_SIZE];
     74         #endregion
     75 
     76         #region 属性
     77         /// <summary>
     78         /// FTP服务器IP地址
     79         /// </summary>
     80         public string RemoteHost
     81         {
     82             get
     83             {
     84                 return strRemoteHost;
     85             }
     86             set
     87             {
     88                 strRemoteHost = value;
     89             }
     90         }
     91 
     92         /// <summary>
     93         /// FTP服务器端口
     94         /// </summary>
     95         public int RemotePort
     96         {
     97             get
     98             {
     99                 return strRemotePort;
    100             }
    101             set
    102             {
    103                 strRemotePort = value;
    104             }
    105         }
    106 
    107         /// <summary>
    108         /// 当前服务器目录
    109         /// </summary>
    110         public string RemotePath
    111         {
    112             get
    113             {
    114                 return strRemotePath;
    115             }
    116             set
    117             {
    118                 strRemotePath = value;
    119             }
    120         }
    121 
    122         /// <summary>
    123         /// 登录用户账号
    124         /// </summary>
    125         public string RemoteUser
    126         {
    127             set
    128             {
    129                 strRemoteUser = value;
    130             }
    131         }
    132 
    133         /// <summary>
    134         /// 用户登录密码
    135         /// </summary>
    136         public string RemotePass
    137         {
    138             set
    139             {
    140                 strRemotePass = value;
    141             }
    142         }
    143 
    144         /// <summary>
    145         /// 是否登录
    146         /// </summary>
    147         public bool Connected
    148         {
    149             get
    150             {
    151                 return bConnected;
    152             }
    153         }
    154         #endregion
    155 
    156         #region 链接
    157         /// <summary>
    158         /// 建立连接 
    159         /// </summary>
    160         public void Connect()
    161         {
    162             lock (obj)
    163             {
    164                 socketControl = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
    165                 IPEndPoint ep = new IPEndPoint(IPAddress.Parse(RemoteHost), strRemotePort);
    166                 try
    167                 {
    168                     socketControl.Connect(ep);
    169                 }
    170                 catch (Exception)
    171                 {
    172                     throw new IOException("不能连接ftp服务器");
    173                 }
    174             }
    175             ReadReply();
    176             if (iReplyCode != 220)
    177             {
    178                 DisConnect();
    179                 throw new IOException(strReply.Substring(4));
    180             }
    181             SendCommand("USER " + strRemoteUser);
    182             if (!(iReplyCode == 331 || iReplyCode == 230))
    183             {
    184                 CloseSocketConnect();
    185                 throw new IOException(strReply.Substring(4));
    186             }
    187             if (iReplyCode != 230)
    188             {
    189                 SendCommand("PASS " + strRemotePass);
    190                 if (!(iReplyCode == 230 || iReplyCode == 202))
    191                 {
    192                     CloseSocketConnect();
    193                     throw new IOException(strReply.Substring(4));
    194                 }
    195             }
    196             bConnected = true;
    197             ChDir(strRemotePath);
    198         }
    199 
    200         /// <summary>
    201         /// 关闭连接
    202         /// </summary>
    203         public void DisConnect()
    204         {
    205             if (socketControl != null)
    206             {
    207                 SendCommand("QUIT");
    208             }
    209             CloseSocketConnect();
    210         }
    211         #endregion
    212 
    213         #region 传输模式
    214         /// <summary>
    215         /// 传输模式:二进制类型、ASCII类型
    216         /// </summary>
    217         public enum TransferType { Binary, ASCII };
    218 
    219         /// <summary>
    220         /// 设置传输模式
    221         /// </summary>
    222         /// <param name="ttType">传输模式</param>
    223         public void SetTransferType(TransferType ttType)
    224         {
    225             if (ttType == TransferType.Binary)
    226             {
    227                 SendCommand("TYPE I");//binary类型传输
    228             }
    229             else
    230             {
    231                 SendCommand("TYPE A");//ASCII类型传输
    232             }
    233             if (iReplyCode != 200)
    234             {
    235                 throw new IOException(strReply.Substring(4));
    236             }
    237             else
    238             {
    239                 trType = ttType;
    240             }
    241         }
    242 
    243         /// <summary>
    244         /// 获得传输模式
    245         /// </summary>
    246         /// <returns>传输模式</returns>
    247         public TransferType GetTransferType()
    248         {
    249             return trType;
    250         }
    251         #endregion
    252 
    253         #region 文件操作
    254         /// <summary>
    255         /// 获得文件列表
    256         /// </summary>
    257         /// <param name="strMask">文件名的匹配字符串</param>
    258         public string[] Dir(string strMask)
    259         {
    260             if (!bConnected)
    261             {
    262                 Connect();
    263             }
    264             Socket socketData = CreateDataSocket();
    265             SendCommand("NLST " + strMask);
    266             if (!(iReplyCode == 150 || iReplyCode == 125 || iReplyCode == 226))
    267             {
    268                 throw new IOException(strReply.Substring(4));
    269             }
    270             strMsg = "";
    271             Thread.Sleep(2000);
    272             while (true)
    273             {
    274                 int iBytes = socketData.Receive(buffer, buffer.Length, 0);
    275                 strMsg += ASCII.GetString(buffer, 0, iBytes);
    276                 if (iBytes < buffer.Length)
    277                 {
    278                     break;
    279                 }
    280             }
    281             char[] seperator = { '
    ' };
    282             string[] strsFileList = strMsg.Split(seperator);
    283             socketData.Close(); //数据socket关闭时也会有返回码
    284             if (iReplyCode != 226)
    285             {
    286                 ReadReply();
    287                 if (iReplyCode != 226)
    288                 {
    289 
    290                     throw new IOException(strReply.Substring(4));
    291                 }
    292             }
    293             return strsFileList;
    294         }
    295 
    296         public void newPutByGuid(string strFileName, string strGuid)
    297         {
    298             if (!bConnected)
    299             {
    300                 Connect();
    301             }
    302             string str = strFileName.Substring(0, strFileName.LastIndexOf("\"));
    303             string strTypeName = strFileName.Substring(strFileName.LastIndexOf("."));
    304             strGuid = str + "\" + strGuid;
    305             Socket socketData = CreateDataSocket();
    306             SendCommand("STOR " + Path.GetFileName(strGuid));
    307             if (!(iReplyCode == 125 || iReplyCode == 150))
    308             {
    309                 throw new IOException(strReply.Substring(4));
    310             }
    311             FileStream input = new FileStream(strGuid, FileMode.Open);
    312             input.Flush();
    313             int iBytes = 0;
    314             while ((iBytes = input.Read(buffer, 0, buffer.Length)) > 0)
    315             {
    316                 socketData.Send(buffer, iBytes, 0);
    317             }
    318             input.Close();
    319             if (socketData.Connected)
    320             {
    321                 socketData.Close();
    322             }
    323             if (!(iReplyCode == 226 || iReplyCode == 250))
    324             {
    325                 ReadReply();
    326                 if (!(iReplyCode == 226 || iReplyCode == 250))
    327                 {
    328                     throw new IOException(strReply.Substring(4));
    329                 }
    330             }
    331         }
    332 
    333         /// <summary>
    334         /// 获取文件大小
    335         /// </summary>
    336         /// <param name="strFileName">文件名</param>
    337         /// <returns>文件大小</returns>
    338         public long GetFileSize(string strFileName)
    339         {
    340             if (!bConnected)
    341             {
    342                 Connect();
    343             }
    344             SendCommand("SIZE " + Path.GetFileName(strFileName));
    345             long lSize = 0;
    346             if (iReplyCode == 213)
    347             {
    348                 lSize = Int64.Parse(strReply.Substring(4));
    349             }
    350             else
    351             {
    352                 throw new IOException(strReply.Substring(4));
    353             }
    354             return lSize;
    355         }
    356 
    357 
    358         /// <summary>
    359         /// 获取文件信息
    360         /// </summary>
    361         /// <param name="strFileName">文件名</param>
    362         /// <returns>文件大小</returns>
    363         public string GetFileInfo(string strFileName)
    364         {
    365             if (!bConnected)
    366             {
    367                 Connect();
    368             }
    369             Socket socketData = CreateDataSocket();
    370             SendCommand("LIST " + strFileName);
    371             string strResult = "";
    372             if (!(iReplyCode == 150 || iReplyCode == 125
    373                 || iReplyCode == 226 || iReplyCode == 250))
    374             {
    375                 throw new IOException(strReply.Substring(4));
    376             }
    377             byte[] b = new byte[512];
    378             MemoryStream ms = new MemoryStream();
    379 
    380             while (true)
    381             {
    382                 int iBytes = socketData.Receive(b, b.Length, 0);
    383                 ms.Write(b, 0, iBytes);
    384                 if (iBytes <= 0)
    385                 {
    386 
    387                     break;
    388                 }
    389             }
    390             byte[] bt = ms.GetBuffer();
    391             strResult = System.Text.Encoding.ASCII.GetString(bt);
    392             ms.Close();
    393             return strResult;
    394         }
    395 
    396         /// <summary>
    397         /// 删除
    398         /// </summary>
    399         /// <param name="strFileName">待删除文件名</param>
    400         public void Delete(string strFileName)
    401         {
    402             if (!bConnected)
    403             {
    404                 Connect();
    405             }
    406             SendCommand("DELE " + strFileName);
    407             if (iReplyCode != 250)
    408             {
    409                 throw new IOException(strReply.Substring(4));
    410             }
    411         }
    412 
    413         /// <summary>
    414         /// 重命名(如果新文件名与已有文件重名,将覆盖已有文件)
    415         /// </summary>
    416         /// <param name="strOldFileName">旧文件名</param>
    417         /// <param name="strNewFileName">新文件名</param>
    418         public void Rename(string strOldFileName, string strNewFileName)
    419         {
    420             if (!bConnected)
    421             {
    422                 Connect();
    423             }
    424             SendCommand("RNFR " + strOldFileName);
    425             if (iReplyCode != 350)
    426             {
    427                 throw new IOException(strReply.Substring(4));
    428             }
    429             //  如果新文件名与原有文件重名,将覆盖原有文件
    430             SendCommand("RNTO " + strNewFileName);
    431             if (iReplyCode != 250)
    432             {
    433                 throw new IOException(strReply.Substring(4));
    434             }
    435         }
    436         #endregion
    437 
    438         #region 上传和下载
    439         /// <summary>
    440         /// 下载一批文件
    441         /// </summary>
    442         /// <param name="strFileNameMask">文件名的匹配字符串</param>
    443         /// <param name="strFolder">本地目录(不得以结束)</param>
    444         public void Get(string strFileNameMask, string strFolder)
    445         {
    446             if (!bConnected)
    447             {
    448                 Connect();
    449             }
    450             string[] strFiles = Dir(strFileNameMask);
    451             foreach (string strFile in strFiles)
    452             {
    453                 if (!strFile.Equals(""))//一般来说strFiles的最后一个元素可能是空字符串
    454                 {
    455                     Get(strFile, strFolder, strFile);
    456                 }
    457             }
    458         }
    459 
    460         /// <summary>
    461         /// 下载一个文件
    462         /// </summary>
    463         /// <param name="strRemoteFileName">要下载的文件名</param>
    464         /// <param name="strFolder">本地目录(不得以结束)</param>
    465         /// <param name="strLocalFileName">保存在本地时的文件名</param>
    466         public void Get(string strRemoteFileName, string strFolder, string strLocalFileName)
    467         {
    468             Socket socketData = CreateDataSocket();
    469             try
    470             {
    471                 if (!bConnected)
    472                 {
    473                     Connect();
    474                 }
    475                 SetTransferType(TransferType.Binary);
    476                 if (strLocalFileName.Equals(""))
    477                 {
    478                     strLocalFileName = strRemoteFileName;
    479                 }
    480                 SendCommand("RETR " + strRemoteFileName);
    481                 if (!(iReplyCode == 150 || iReplyCode == 125 || iReplyCode == 226 || iReplyCode == 250))
    482                 {
    483                     throw new IOException(strReply.Substring(4));
    484                 }
    485                 FileStream output = new FileStream(strFolder + "\" + strLocalFileName, FileMode.Create);
    486                 while (true)
    487                 {
    488                     int iBytes = socketData.Receive(buffer, buffer.Length, 0);
    489                     output.Write(buffer, 0, iBytes);
    490                     if (iBytes <= 0)
    491                     {
    492                         break;
    493                     }
    494                 }
    495                 output.Close();
    496                 if (socketData.Connected)
    497                 {
    498                     socketData.Close();
    499                 }
    500                 if (!(iReplyCode == 226 || iReplyCode == 250))
    501                 {
    502                     ReadReply();
    503                     if (!(iReplyCode == 226 || iReplyCode == 250))
    504                     {
    505                         throw new IOException(strReply.Substring(4));
    506                     }
    507                 }
    508             }
    509             catch
    510             {
    511                 socketData.Close();
    512                 socketData = null;
    513                 socketControl.Close();
    514                 bConnected = false;
    515                 socketControl = null;
    516             }
    517         }
    518 
    519         /// <summary>
    520         /// 下载一个文件
    521         /// </summary>
    522         /// <param name="strRemoteFileName">要下载的文件名</param>
    523         /// <param name="strFolder">本地目录(不得以结束)</param>
    524         /// <param name="strLocalFileName">保存在本地时的文件名</param>
    525         public void GetNoBinary(string strRemoteFileName, string strFolder, string strLocalFileName)
    526         {
    527             if (!bConnected)
    528             {
    529                 Connect();
    530             }
    531 
    532             if (strLocalFileName.Equals(""))
    533             {
    534                 strLocalFileName = strRemoteFileName;
    535             }
    536             Socket socketData = CreateDataSocket();
    537             SendCommand("RETR " + strRemoteFileName);
    538             if (!(iReplyCode == 150 || iReplyCode == 125 || iReplyCode == 226 || iReplyCode == 250))
    539             {
    540                 throw new IOException(strReply.Substring(4));
    541             }
    542             FileStream output = new FileStream(strFolder + "\" + strLocalFileName, FileMode.Create);
    543             while (true)
    544             {
    545                 int iBytes = socketData.Receive(buffer, buffer.Length, 0);
    546                 output.Write(buffer, 0, iBytes);
    547                 if (iBytes <= 0)
    548                 {
    549                     break;
    550                 }
    551             }
    552             output.Close();
    553             if (socketData.Connected)
    554             {
    555                 socketData.Close();
    556             }
    557             if (!(iReplyCode == 226 || iReplyCode == 250))
    558             {
    559                 ReadReply();
    560                 if (!(iReplyCode == 226 || iReplyCode == 250))
    561                 {
    562                     throw new IOException(strReply.Substring(4));
    563                 }
    564             }
    565         }
    566 
    567         /// <summary>
    568         /// 上传一批文件
    569         /// </summary>
    570         /// <param name="strFolder">本地目录(不得以结束)</param>
    571         /// <param name="strFileNameMask">文件名匹配字符(可以包含*和?)</param>
    572         public void Put(string strFolder, string strFileNameMask)
    573         {
    574             string[] strFiles = Directory.GetFiles(strFolder, strFileNameMask);
    575             foreach (string strFile in strFiles)
    576             {
    577                 Put(strFile);
    578             }
    579         }
    580 
    581         /// <summary>
    582         /// 上传一个文件
    583         /// </summary>
    584         /// <param name="strFileName">本地文件名</param>
    585         public void Put(string strFileName)
    586         {
    587             if (!bConnected)
    588             {
    589                 Connect();
    590             }
    591             Socket socketData = CreateDataSocket();
    592             if (Path.GetExtension(strFileName) == "")
    593                 SendCommand("STOR " + Path.GetFileNameWithoutExtension(strFileName));
    594             else
    595                 SendCommand("STOR " + Path.GetFileName(strFileName));
    596 
    597             if (!(iReplyCode == 125 || iReplyCode == 150))
    598             {
    599                 throw new IOException(strReply.Substring(4));
    600             }
    601 
    602             FileStream input = new FileStream(strFileName, FileMode.Open);
    603             int iBytes = 0;
    604             while ((iBytes = input.Read(buffer, 0, buffer.Length)) > 0)
    605             {
    606                 socketData.Send(buffer, iBytes, 0);
    607             }
    608             input.Close();
    609             if (socketData.Connected)
    610             {
    611                 socketData.Close();
    612             }
    613             if (!(iReplyCode == 226 || iReplyCode == 250 || iReplyCode == 200))
    614             {
    615                 ReadReply();
    616                 if (!(iReplyCode == 226 || iReplyCode == 250 || iReplyCode == 200))
    617                 {
    618                     throw new IOException(strReply.Substring(4));
    619                 }
    620             }
    621         }
    622 
    623 
    624         /// <summary>
    625         /// 上传一个文件
    626         /// </summary>
    627         /// <param name="strFileName">本地文件名</param>
    628         public void PutByGuid(string strFileName, string strGuid)
    629         {
    630             if (!bConnected)
    631             {
    632                 Connect();
    633             }
    634             string str = strFileName.Substring(0, strFileName.LastIndexOf("\"));
    635             string strTypeName = strFileName.Substring(strFileName.LastIndexOf("."));
    636             strGuid = str + "\" + strGuid;
    637             System.IO.File.Copy(strFileName, strGuid);
    638             System.IO.File.SetAttributes(strGuid, System.IO.FileAttributes.Normal);
    639             Socket socketData = CreateDataSocket();
    640             SendCommand("STOR " + Path.GetFileName(strGuid));
    641             if (!(iReplyCode == 125 || iReplyCode == 150))
    642             {
    643                 throw new IOException(strReply.Substring(4));
    644             }
    645             FileStream input = new FileStream(strGuid, FileMode.Open, System.IO.FileAccess.Read, System.IO.FileShare.Read);
    646             int iBytes = 0;
    647             while ((iBytes = input.Read(buffer, 0, buffer.Length)) > 0)
    648             {
    649                 socketData.Send(buffer, iBytes, 0);
    650             }
    651             input.Close();
    652             File.Delete(strGuid);
    653             if (socketData.Connected)
    654             {
    655                 socketData.Close();
    656             }
    657             if (!(iReplyCode == 226 || iReplyCode == 250))
    658             {
    659                 ReadReply();
    660                 if (!(iReplyCode == 226 || iReplyCode == 250))
    661                 {
    662                     throw new IOException(strReply.Substring(4));
    663                 }
    664             }
    665         }
    666         #endregion
    667 
    668         #region 目录操作
    669         /// <summary>
    670         /// 创建目录
    671         /// </summary>
    672         /// <param name="strDirName">目录名</param>
    673         public void MkDir(string strDirName)
    674         {
    675             if (!bConnected)
    676             {
    677                 Connect();
    678             }
    679             SendCommand("MKD " + strDirName);
    680             if (iReplyCode != 257)
    681             {
    682                 throw new IOException(strReply.Substring(4));
    683             }
    684         }
    685 
    686         /// <summary>
    687         /// 删除目录
    688         /// </summary>
    689         /// <param name="strDirName">目录名</param>
    690         public void RmDir(string strDirName)
    691         {
    692             if (!bConnected)
    693             {
    694                 Connect();
    695             }
    696             SendCommand("RMD " + strDirName);
    697             if (iReplyCode != 250)
    698             {
    699                 throw new IOException(strReply.Substring(4));
    700             }
    701         }
    702 
    703         /// <summary>
    704         /// 改变目录
    705         /// </summary>
    706         /// <param name="strDirName">新的工作目录名</param>
    707         public void ChDir(string strDirName)
    708         {
    709             if (strDirName.Equals(".") || strDirName.Equals(""))
    710             {
    711                 return;
    712             }
    713             if (!bConnected)
    714             {
    715                 Connect();
    716             }
    717             SendCommand("CWD " + strDirName);
    718             if (iReplyCode != 250)
    719             {
    720                 throw new IOException(strReply.Substring(4));
    721             }
    722             this.strRemotePath = strDirName;
    723         }
    724         #endregion
    725 
    726         #region 内部函数
    727         /// <summary>
    728         /// 将一行应答字符串记录在strReply和strMsg,应答码记录在iReplyCode
    729         /// </summary>
    730         private void ReadReply()
    731         {
    732             strMsg = "";
    733             strReply = ReadLine();
    734             iReplyCode = Int32.Parse(strReply.Substring(0, 3));
    735         }
    736 
    737         /// <summary>
    738         /// 建立进行数据连接的socket
    739         /// </summary>
    740         /// <returns>数据连接socket</returns>
    741         private Socket CreateDataSocket()
    742         {
    743             SendCommand("PASV");
    744             if (iReplyCode != 227)
    745             {
    746                 throw new IOException(strReply.Substring(4));
    747             }
    748             int index1 = strReply.IndexOf('(');
    749             int index2 = strReply.IndexOf(')');
    750             string ipData = strReply.Substring(index1 + 1, index2 - index1 - 1);
    751             int[] parts = new int[6];
    752             int len = ipData.Length;
    753             int partCount = 0;
    754             string buf = "";
    755             for (int i = 0; i < len && partCount <= 6; i++)
    756             {
    757                 char ch = Char.Parse(ipData.Substring(i, 1));
    758                 if (Char.IsDigit(ch))
    759                     buf += ch;
    760                 else if (ch != ',')
    761                 {
    762                     throw new IOException("Malformed PASV strReply: " + strReply);
    763                 }
    764                 if (ch == ',' || i + 1 == len)
    765                 {
    766                     try
    767                     {
    768                         parts[partCount++] = Int32.Parse(buf);
    769                         buf = "";
    770                     }
    771                     catch (Exception)
    772                     {
    773                         throw new IOException("Malformed PASV strReply: " + strReply);
    774                     }
    775                 }
    776             }
    777             string ipAddress = parts[0] + "." + parts[1] + "." + parts[2] + "." + parts[3];
    778             int port = (parts[4] << 8) + parts[5];
    779             Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
    780             IPEndPoint ep = new IPEndPoint(IPAddress.Parse(ipAddress), port);
    781             try
    782             {
    783                 s.Connect(ep);
    784             }
    785             catch (Exception)
    786             {
    787                 throw new IOException("无法连接ftp服务器");
    788             }
    789             return s;
    790         }
    791 
    792         /// <summary>
    793         /// 关闭socket连接(用于登录以前)
    794         /// </summary>
    795         private void CloseSocketConnect()
    796         {
    797             lock (obj)
    798             {
    799                 if (socketControl != null)
    800                 {
    801                     socketControl.Close();
    802                     socketControl = null;
    803                 }
    804                 bConnected = false;
    805             }
    806         }
    807 
    808         /// <summary>
    809         /// 读取Socket返回的所有字符串
    810         /// </summary>
    811         /// <returns>包含应答码的字符串行</returns>
    812         private string ReadLine()
    813         {
    814             lock (obj)
    815             {
    816                 while (true)
    817                 {
    818                     int iBytes = socketControl.Receive(buffer, buffer.Length, 0);
    819                     strMsg += ASCII.GetString(buffer, 0, iBytes);
    820                     if (iBytes < buffer.Length)
    821                     {
    822                         break;
    823                     }
    824                 }
    825             }
    826             char[] seperator = { '
    ' };
    827             string[] mess = strMsg.Split(seperator);
    828             if (strMsg.Length > 2)
    829             {
    830                 strMsg = mess[mess.Length - 2];
    831             }
    832             else
    833             {
    834                 strMsg = mess[0];
    835             }
    836             if (!strMsg.Substring(3, 1).Equals(" ")) //返回字符串正确的是以应答码(如220开头,后面接一空格,再接问候字符串)
    837             {
    838                 return ReadLine();
    839             }
    840             return strMsg;
    841         }
    842 
    843         /// <summary>
    844         /// 发送命令并获取应答码和最后一行应答字符串
    845         /// </summary>
    846         /// <param name="strCommand">命令</param>
    847         public void SendCommand(String strCommand)
    848         {
    849             lock (obj)
    850             {
    851                 Byte[] cmdBytes = Encoding.ASCII.GetBytes((strCommand + "
    ").ToCharArray());
    852                 socketControl.Send(cmdBytes, cmdBytes.Length, 0);
    853                 Thread.Sleep(500);
    854                 ReadReply();
    855             }
    856         }
    857         #endregion
    858     }
  • 相关阅读:
    django自带登录认证与登录自动跳转
    11月份草稿2
    使用JavaScript实现字符串格式化
    requests与BeautifulSoup
    python+minicap(二)
    python+minicap的使用
    python+opencv图像处理(一)
    haproxy实现mysql从库负载均衡
    mysql 5.7 64位 解压版安装
    Redis基础教程
  • 原文地址:https://www.cnblogs.com/teng-0802/p/13353551.html
Copyright © 2011-2022 走看看