Initialization
[xylftp.git] / client / gui / src / XylTelnetConnection.java
blobf046cbee780b5c8c294da5ebb555168122d056f0
1 //////////////////////////////////////////////////////////////////////////
2 // Copyright (C) Wang Cong, Apr. 2007.
3 // Hacked by Zhou Xiao-Wei.
4 // This program is free software; you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License version 2 as
6 // published by the Free Software Foundation.
7 // See COPYING to learn more.
8 //////////////////////////////////////////////////////////////////////////
9 import java.net.*;
10 import java.io.*;
12 /**
13 *This class is a subclass of XylFTPConnection.
14 *@author WANG Cong
15 *@version 1.7
17 public class XylTelnetConnection{
19 /**
20 *The hostname of the server to connect to.
22 private String Host;
24 /**
25 *The port to connect to
27 private int ServerPort;
29 /**
30 *The port of local host
33 private int SelfPort;
34 /**
35 *The timeout when attempting to connect a socket.
37 protected int Timeout; //in milliseconds
39 /**
40 *The writer who inputs the commands.
42 private Writer CmdWriter;
44 /**
45 *The reader that reads the echos from the sever.
47 private BufferedReader EchoReader;
49 /**
50 *The socket of the command needs to start.
52 private Socket CmdSocket;
54 /**
55 *Initializes some values.
57 XylTelnetConnection(){
58 CmdWriter = null;
59 EchoReader = null;
60 CmdSocket = null;
61 Host = null;
62 ServerPort = 21;
63 SelfPort = -1;
64 Timeout = 30000; //I don't know whether this is proper. 8(
67 /**
68 *Initializes some values.
69 *@param HostName the hostname of the server to connect to
71 XylTelnetConnection(String HostName){
72 CmdWriter = null;
73 EchoReader = null;
74 CmdSocket = null;
75 Host = HostName;
76 ServerPort = 21;
77 SelfPort = -1;
78 Timeout = 30000;
81 /**
82 *Gets command from the user.
83 *@param Command the command that user inputs
84 *@exception IOException on sending command error
85 *@see IOException
87 public void SendCommand(String Command) throws IOException{
88 try{
89 CmdWriter.write(Command);
90 CmdWriter.flush();
91 } catch (IOException e) {
92 throw e;
95 /**
96 *Gets the string of the IP address of localhost
97 *Each byte of IP is separated by a _comma_, since
98 *this is for PORT command, _not_ for any socket.
100 public String GetSelfIP(){
101 InetAddress ina;
102 byte[] ipa;
103 String ip = "";
104 ina = CmdSocket.getLocalAddress();
105 ipa = ina.getAddress();
106 for (int i=0; i< 4; i++) {
107 ip = ip + (((int)ipa[i])&0xff) + ","; //I know we have 4 commas!
109 return ip;
112 *Attempts to get an available port of local host for use.
114 public String GetSelfPort() throws IOException {
115 ServerSocket sk = new ServerSocket(0);
116 int port = sk.getLocalPort();
117 sk.close();
118 SelfPort = port;
119 int hi, low;
120 low = port%256;
121 hi = port/256;
122 sk.close();
123 return hi+","+low;
126 *Just returns the port to be used by local host.
128 public int ReturnSelfPort(){
129 return SelfPort;
132 *Gets echoes from the server.
133 *@return line the echos from the server
134 *@exception IOException on read error
136 public String GetEcho() throws IOException{
137 String line = EchoReader.readLine();
138 while (line != null && line.length() == 0)
139 line = EchoReader.readLine();
140 return line;
144 *Starts to connect with the server.
145 *@exception Exception on opening connection error
146 *@see Exception
148 public void OpenConnection() throws Exception{
149 if(Host==null)
150 throw new Exception("Unspecified host name.");
151 CmdSocket = new Socket(Host, ServerPort);
152 CmdSocket.setSoTimeout(Timeout);
153 InitStreams();
157 *Initilizes the streams that input and output.
158 *@exception Exception on errors when initializing streams.
159 *@see Exception
161 private void InitStreams() throws Exception{
162 InputStream is = CmdSocket.getInputStream();
163 EchoReader = new BufferedReader(new InputStreamReader(is));
164 OutputStream os = CmdSocket.getOutputStream();
165 CmdWriter = new OutputStreamWriter(os);
169 *Closes the connection with the server.
170 *@exception Exception on closing connection error
171 *@see Exception
173 public void CloseConnection() throws Exception{
174 if(CmdWriter != null) {
175 CmdWriter.close();
176 CmdWriter = null;
178 if(EchoReader != null) {
179 EchoReader.close();
180 EchoReader = null;
182 if(CmdSocket != null) {
183 CmdSocket.close();
184 CmdSocket = null;
189 *Sets a new host.
190 *@param NewHost a new founded host
192 public void SetHost(String NewHost) {
193 Host = NewHost;
197 *Gets the host name .
198 *@return the hostname of the server
200 public String GetHost(){
201 return Host;
205 *Sets a new port.
206 *@param NewPort a new founded port
208 public void SetPort(int NewPort) {
209 ServerPort = NewPort;
213 *Gets the port connected with the server.
214 *@return the number of the current port
216 public int GetPort(){
217 return ServerPort;