Exemplo n.º 1
0
        /// <summary>
        /// 选择模式并创建数据连接
        /// </summary>
        /// <param name="dataSocket"></param>
        /// <param name="dataHelper"></param>
        private void InitDataConnection(out Socket dataSocket, out SocketHelper dataHelper)
        {
            string line;
            int    status;

            // EPSV => 229;
            _commandHelper.Writeln("EPSV");
            line = System.Text.Encoding.UTF8.GetString(_commandHelper.Readln(out status));
            if (status == 229)
            {
                string dataConnection = line;
                // 创建数据连接
                dataSocket = CommandHelper.AddressParserAndConnect(1, dataConnection, _serverIpe.Address);
                dataHelper = new SocketHelper(dataSocket);
            }
            else if (status / 100 == 5) // 不支持 EPSV, 使用 PASV
            {
                // PASV => 227;
                _commandHelper.Writeln("PASV");
                line = System.Text.Encoding.UTF8.GetString(_commandHelper.Readln(out status));
                if (status == 227)
                {
                    string dataConnection = line;
                    // 创建数据连接
                    dataSocket = CommandHelper.AddressParserAndConnect(0, dataConnection);
                    dataHelper = new SocketHelper(dataSocket);
                }
                else
                {
                    throw new FTPClientException(status, line);
                }
            }
            else
            {
                throw new FTPClientException(status, line);
            }
        }