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 //////////////////////////////////////////////////////////////////////////
13 *This class is a subclass of XylFTPConnection.
17 public class XylTelnetConnection
{
20 *The hostname of the server to connect to.
25 *The port to connect to
27 private int ServerPort
;
30 *The port of local host
35 *The timeout when attempting to connect a socket.
37 protected int Timeout
; //in milliseconds
40 *The writer who inputs the commands.
42 private Writer CmdWriter
;
45 *The reader that reads the echos from the sever.
47 private BufferedReader EchoReader
;
50 *The socket of the command needs to start.
52 private Socket CmdSocket
;
55 *Initializes some values.
57 XylTelnetConnection(){
64 Timeout
= 30000; //I don't know whether this is proper. 8(
68 *Initializes some values.
69 *@param HostName the hostname of the server to connect to
71 XylTelnetConnection(String HostName
){
82 *Gets command from the user.
83 *@param Command the command that user inputs
84 *@exception IOException on sending command error
87 public void SendCommand(String Command
) throws IOException
{
89 CmdWriter
.write(Command
);
91 } catch (IOException e
) {
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(){
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!
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();
126 *Just returns the port to be used by local host.
128 public int ReturnSelfPort(){
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();
144 *Starts to connect with the server.
145 *@exception Exception on opening connection error
148 public void OpenConnection() throws Exception
{
150 throw new Exception("Unspecified host name.");
151 CmdSocket
= new Socket(Host
, ServerPort
);
152 CmdSocket
.setSoTimeout(Timeout
);
157 *Initilizes the streams that input and output.
158 *@exception Exception on errors when initializing streams.
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
173 public void CloseConnection() throws Exception
{
174 if(CmdWriter
!= null) {
178 if(EchoReader
!= null) {
182 if(CmdSocket
!= null) {
190 *@param NewHost a new founded host
192 public void SetHost(String NewHost
) {
197 *Gets the host name .
198 *@return the hostname of the server
200 public String
GetHost(){
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(){