public virtual void Open(string remoteHost, int remotePort, string user, string password, FTPMode pMode) { ArrayList tempMessageList = new ArrayList(); int returnValue; this.mode = pMode; this.tcpClient = new TcpClient(); this.remoteHost = remoteHost; // As we cannot detect the local address from the TCPClient class, convert "127.0.0.1" and "localhost" to // the DNS record of this machine; this will ensure that the connection address and the PORT command address // are identical. This fixes bug 854919. if (remoteHost == "localhost" || remoteHost == "127.0.0.1") { remoteHost = GetLocalAddressList()[0].ToString(); } //CONNECT try { this.tcpClient.Connect(remoteHost, remotePort); } catch (Exception) { throw new IOException("Couldn't connect to remote server"); } tempMessageList = Read(); returnValue = GetMessageReturnValue((string)tempMessageList[0]); if (returnValue != 220) { Close(); throw new Exception((string)tempMessageList[0]); } //SEND USER tempMessageList = SendCommand("USER " + user); returnValue = GetMessageReturnValue((string)tempMessageList[0]); if (!(returnValue == 331 || returnValue == 202)) { Close(); throw new Exception((string)tempMessageList[0]); } //SEND PASSWORD if (returnValue == 331) { tempMessageList = SendCommand("PASS " + password); returnValue = GetMessageReturnValue((string)tempMessageList[0]); if (!(returnValue == 230 || returnValue == 202)) { Close(); throw new Exception((string)tempMessageList[0]); } } }
/// <summary> /// Opens a new ftp connection /// </summary> /// <param name="remoteHost">The remote hostname</param> /// <param name="remotePort">The remote port</param> /// <param name="user">The remote user</param> /// <param name="password">The remote password</param> /// <param name="mode">The ftp mode</param> public virtual void Open(string remoteHost, int remotePort, string user, string password, FTPMode mode) { var tempMessageList = new List <string>(); int returnValue; _mode = mode; _tcpClient = new TcpClient(); _remoteHost = remoteHost; _remotePort = remotePort; //CONNECT try { _tcpClient.Connect(_remoteHost, _remotePort); } catch (Exception) { throw new IOException("Couldn't connect to remote server"); } tempMessageList = Read(); returnValue = GetMessageReturnValue(tempMessageList[0]); if (returnValue != 220) { Close(); throw new Exception(tempMessageList[0]); } //SEND USER tempMessageList = SendCommand("USER " + user); returnValue = GetMessageReturnValue(tempMessageList[0]); if (!(returnValue == 331 || returnValue == 202)) { Close(); throw new Exception(tempMessageList[0]); } //SEND PASSWORD if (returnValue == 331) { tempMessageList = SendCommand("PASS " + password); returnValue = GetMessageReturnValue(tempMessageList[0]); if (!(returnValue == 230 || returnValue == 202)) { Close(); throw new Exception(tempMessageList[0]); } } }
/// <summary> /// /// </summary> /// <param name="ftpServerIP">FTPService IP</param> /// <param name="userName">帳號</param> /// <param name="password">密碼</param> /// <param name="ftpMode">設定是否走加密的FTP</param> public FTPHelper(string ftpServerIP, string userName, string password, FTPMode ftpMode = FTPMode.None) { _ftpServerIP = ftpServerIP; _userName = userName; _passwoed = password; _ftpMode = ftpMode; if (!PingIPByTcpClient(ftpServerIP)) { if (!PingIPByPowerShell(ftpServerIP)) { throw new Exception(string.Format("連線FTP失敗,原因:{0}", "此IP不通")); } } }
public FTPHelper(FTPParameter param) { _ftpServerIP = param.FTPServerIP; _userName = param.UserName; _passwoed = param.Password; _ftpMode = param.FTPMode; if (!PingIPByTcpClient(_ftpServerIP)) { if (!PingIPByPowerShell(_ftpServerIP)) { throw new Exception(string.Format("連線FTP失敗,原因:{0}", "此IP不通")); } } }
public FtpConnection() { this.activeConnectionsCount = 0; this.mode = FTPMode.Active; this.logMessages = false; }
public override void Open(string remoteHost, string user, string password, FTPMode mode) { base.Open(remoteHost, user, password, mode); }
/// <summary> /// Creates a new ftp connection /// </summary> public FTPConnection() { _activeConnectionsCount = 0; _mode = FTPMode.Active; _logMessages = false; }
/// <summary> /// Opens a new ftp connection /// </summary> /// <param name="remoteHost">The remote hostname</param> /// <param name="remotePort">The remote port</param> /// <param name="user">The remote user</param> /// <param name="password">The remote password</param> /// <param name="mode">The ftp mode</param> public virtual void Open(string remoteHost, int remotePort, string user, string password, FTPMode mode) { var tempMessageList = new List<string>(); int returnValue; _mode = mode; _tcpClient = new TcpClient(); _remoteHost = remoteHost; _remotePort = remotePort; //CONNECT try { _tcpClient.Connect(_remoteHost, _remotePort); } catch (Exception) { throw new IOException("Couldn't connect to remote server"); } tempMessageList = Read(); returnValue = GetMessageReturnValue(tempMessageList[0]); if (returnValue != 220) { Close(); throw new Exception(tempMessageList[0]); } //SEND USER tempMessageList = SendCommand("USER " + user); returnValue = GetMessageReturnValue(tempMessageList[0]); if (!(returnValue == 331 || returnValue == 202)) { Close(); throw new Exception(tempMessageList[0]); } //SEND PASSWORD if (returnValue == 331) { tempMessageList = SendCommand("PASS " + password); returnValue = GetMessageReturnValue(tempMessageList[0]); if (!(returnValue == 230 || returnValue == 202)) { Close(); throw new Exception(tempMessageList[0]); } } }
/// <summary> /// Opens a new ftp connection /// </summary> /// <param name="remoteHost">The remote hostname</param> /// <param name="user">The remote user</param> /// <param name="password">The remote password</param> /// <param name="mode">The ftp mode</param> public virtual void Open(string remoteHost, string user, string password, FTPMode mode) { Open(remoteHost, DefaultRemotePort, user, password, mode); }
private void perform_background_upload(FTPMode mode, Stream stream) { _client = get_client(mode); NetworkStream networkStream = _client.GetStream(); var buffer = new Byte[BLOCK_SIZE]; int bytes = 0; int totalBytes = 0; while(totalBytes < stream.Length) { bytes = (int)stream.Read(buffer, 0, BLOCK_SIZE); totalBytes = totalBytes + bytes; networkStream.Write(buffer, 0, bytes); } networkStream.Close(); _client.Close(); if(mode == FTPMode.Active) { _listener.Stop(); } }
public override void Open(string remoteHost, int remotePort, string user, string password, FTPMode mode) { base.Open(remoteHost, remotePort, user, password, mode); }
public virtual void Open(string remoteHost, string user, string password, FTPMode mode) { Open(remoteHost, DEFAULT_REMOTE_PORT, user, password, mode); }
public FTPConnection() { this.activeConnectionsCount = 0; this.mode = FTPMode.Active; this.logMessages = false; }
public virtual int Open(string remoteHost, int remotePort, string user, string password, FTPMode pMode) { Logger.Debug(this, string.Format("Opening host {0}, port {1} for user {2}", remoteHost, remotePort, user)); remoteHost = trim_host(remoteHost); Connected = false; Mode = pMode; _tcpClient = new TcpClient(); Host = remoteHost; // As we cannot detect the local address from the TCPClient class, convert "127.0.0.1" and "localhost" to // the DNS record of this machine; this will ensure that the connection address and the PORT command address // are identical. This fixes bug 854919. if (remoteHost == "localhost" || remoteHost == "127.0.0.1") { remoteHost = FTPUtilities.GetLocalAddressList()[0].ToString(); } //CONNECT int returnValue = connect(remoteHost, remotePort); if(returnValue != 220) { Close(); return returnValue; } //SEND USER returnValue = send_user(user); if(!(returnValue == 331 || returnValue == 202)) { Close(); throw new ApplicationException(_currentMsg); } //SEND PASSWORD if(returnValue == 331) { returnValue = send_password(password); if(!(returnValue == 230 || returnValue == 202)) { Close(); throw new ApplicationException(_currentMsg); } } Connected = true; return returnValue; }
public override int Open(string remoteHost, int remotePort, string user, string password, FTPMode mode) { return base.Open(remoteHost, remotePort, user, password, mode); }
private TcpClient get_client(FTPMode mode) { if(mode == FTPMode.Active) { return _listener.AcceptTcpClient(); } return _client; }