//Connect public bool Connect(string address) { try { this.remoteAddress = address; serverCommandSocket = new ClientSocket(); SocketConnectArgs args = serverCommandSocket.ConnectThread(Dns.GetHostAddresses(address)[0], port); if (!args.isConnected) { throw new FTPCannotConnectException(args.exception.Message); } char[] response = WaitForStatus(); Print(response); statusCode = 100 * (response[0] - '0') + 10 * (response[1] - '0') + (response[2] - '0'); if (statusCode != 220) { serverCommandSocket.Disconnect(); throw new FTPCannotConnectException(args.exception.Message); } return(true); } catch (FTPCannotConnectException exception) { WriteLine(exception); return(false); } }
//Get Socket for Transfer in Passive Mode private void GetTransferSocket() { try { char[] response = SendCommand("PASV "); statusCode = 100 * (response[0] - '0') + 10 * (response[1] - '0') + (response[2] - '0'); if (statusCode != 227) { throw new FTPException(statusCode + " status : unable to transfer, task failed"); } Print(response); bool start = false; int arrayCounter = 0; string[] stringServerDetails = new string[6]; for (int counter = 0; counter < response.Length; counter++) { if (response[counter] == '(') { start = true; } else if (start) { if (response[counter] == ',') { arrayCounter++; } else if (response[counter] == ')') { start = false; } else { stringServerDetails[arrayCounter] += response[counter]; } } } serverTransferSocket = new ClientSocket(); serverTransferSocket.ConnectThread(Dns.GetHostAddresses(stringServerDetails[0] + "." + stringServerDetails[1] + "." + stringServerDetails[2] + "." + stringServerDetails[3])[0], int.Parse(stringServerDetails[4]) * 256 + int.Parse(stringServerDetails[5])); } catch (FTPException exception) { WriteLine(exception.Message); } }