public class Telnet
extends java.lang.Object
implements java.io.Serializable
public class TelnetExample extends TelnetAdapter {
private Telnet telnet = null;
private OutputStream output = null;
private static BufferedReader reader = null;
private boolean connected = false;
private String hostname = "10.0.0.1";
public TelnetExample() throws IOException, TelnetException {
// create new Telnet instance
telnet = new Telnet(hostname);
// register this class as TelnetListener
telnet.addTelnetListener(this);
// establish Telnet connection
telnet.connect();
connected = true;
// get output stream
output = telnet.getOutputStream();
// sends all data entered at console to Telnet server
String input = null;
while ((input = reader.readLine()) != null) {
if (connected) {
((TelnetOutputStream) output).println(input);
} else {
break;
}
}
}
// Invoked when Telnet socked is connected.
public void connected(TelnetConnectedEvent event) {
System.out.println("Connected");
}
// Invoked when Telnet socket is disconnected. Disconnect can
public void disconnected(TelnetDisconnectedEvent event) {
connected = false;
System.out.print("Disconnected. Press enter key to quit.");
}
// Invoked when Telnet server requests that the Telnet client begin performing specified TelnetOption.
public void doOption(DoOptionEvent event) {
// refuse any options requested by Telnet server
telnet.sendWontOption(event.getOption());
}
// Invoked when Telnet server offers to begin performing specified TelnetOption.
public void willOption(WillOptionEvent event) {
// refuse any options offered by Telnet server
telnet.sendDontOption(event.getOption());
}
// Invoked when data is received from Telnet server.
public void dataReceived(TelnetDataReceivedEvent event) {
// print data recevied from Telnet server to console
System.out.print(event.getData());
}
// starts console program
public static void main(String[] args) {
try {
// create BufferedReader to read data from console
reader = new BufferedReader(new InputStreamReader(System.in));
// create new TelnetExample instance
TelnetExample example = new TelnetExample();
} catch (Exception e) {
e.printStackTrace(System.out);
}
}
}
| Modifier and Type | Class and Description |
|---|---|
class |
Telnet.ByteDecoder
Byte Decoder
|
class |
Telnet.DefaultDecoder
Default Decoder
|
class |
Telnet.EUCKoreanDecoder
EUC Korean Decoder
|
class |
Telnet.UTF8Decoder
UTF8 Decoder
|
| Modifier and Type | Field and Description |
|---|---|
static java.lang.String[] |
COMMAND_MAP
This field can be used to map command codes to their String
representation.
|
static int |
TC_AO
Telnet command : Abort output
|
static int |
TC_AYT
Telnet command : Are You There
|
static int |
TC_BRK
Telnet command : Break.
NVT character BRK. |
static int |
TC_DM
Telnet command : Data Mark.
The data stream portion of a Synch. |
static int |
TC_DO
Telnet command : DO.
Indicates the request that the other party perform, or confirmation that you are expecting the other party to perform, the indicated option. |
static int |
TC_DONT
Telnet command : DON'T.
Indicates the demand that the other party stop performing, or confirmation that you are no longer expecting the other party to perform, the indicated option. |
static int |
TC_GA
Telnet command : Go ahead
|
static int |
TC_IP
Telnet command : Interrupt Process.
|
static int |
TC_NOP
Telnet command : No operation.
|
static int |
TC_SB
Telnet command : Subnegotiation.
Indicates that what follows is subnegotiation of the indicated option. |
static int |
TC_SE
Telnet command : End of subnegotiation parameters.
|
static int |
TC_WILL
Telnet command : WILL.
Indicates the desire to begin performing, or confirmation that you are now performing, the indicated option. |
static int |
TC_WONT
Telnet command : WON'T.
Indicates the refusal to perform, or continue performing, the indicated option. |
static int |
TSC_BEL
Telnet special character : BELL.
Produces an audible or visible signal (which does NOT move the print head). |
static int |
TSC_BS
Telnet special character : Back Space.
Moves the print head one character position towards the left margin. |
static int |
TSC_CR
Telnet special character : Carriage Return.
Moves the printer to the left margin of the current line. |
static int |
TSC_EC
Telnet special character : Erase character
|
static int |
TSC_EL
Telnet special character : Erase line
|
static int |
TSC_FF
Telnet special character : Form Feed.
Moves the printer to the top of the next page, keeping the same horizontal position. |
static int |
TSC_HT
Telnet special character : Horizontal Tab.
Moves the printer to the next horizontal tab stop. |
static int |
TSC_IAC
Telnet escape charater : Interpret as Command
|
static int |
TSC_LF
Telnet special character : Line Feed.
Moves the printer to the next print line, keeping the same horizontal position. |
static int |
TSC_NULL
Telnet special character : No operation
|
static int |
TSC_VT
Telnet special character : Vertical Tab.
Moves the printer to the next vertical tab stop. |
| Constructor and Description |
|---|
Telnet(java.lang.String hostname)
Constructs Telnet object for connection to specified host.
|
Telnet(java.lang.String host,
int port)
Constructs Telnet object.
|
| Modifier and Type | Method and Description |
|---|---|
void |
addTelnetListener(TelnetListener listener)
Add telnet event listener.
|
void |
clearProxySettings()
Clears proxy server values.
|
void |
connect()
Connect the client socket to host and port specified by contructor or
setHostname(java.lang.String), setPort(int) methods. |
void |
disconnect()
Disconnects and releases all associated resources.
|
java.lang.String |
getCharacterSet()
Gets the character set used when reading data from Telnet server.
|
java.lang.String |
getCommandName(int command)
Gets name of command based on corresponding command.
|
boolean |
getDebug()
Gets debugging state.
|
java.io.PrintStream |
getDebugStream()
Gets PrintStream used in reporting debug statements.
|
java.lang.String |
getHostname()
Return server hostname.
|
java.io.InputStream |
getInputStream()
Returns input stream associated with current Telnet connection.
|
java.io.OutputStream |
getOutputStream()
Returns output stream associated with current telnet connection.
|
int |
getPort()
Return server port.
|
java.io.Reader |
getReader()
Returns Reader object for reading from the server.
|
int |
getReadingTimeout()
Returns the socket reading timeout value.
|
int |
getTimeout()
Gets the timeout for opening connection to Telnet server.
|
java.io.Writer |
getWriter()
Returns Writer object for writing to server.
|
void |
removeTelnetListener(TelnetListener listener)
Remove telnet event listener.
|
void |
sendCommand(int command)
Sends telnet command.
|
void |
sendCommand(int command,
int parameter)
Sends telnet command with specified parameter.
|
void |
sendDontOption(TelnetOption opt)
Sends Telnet option to server with Telnet DONT OPTION (refuse request to use option)
command
|
void |
sendDoOption(TelnetOption opt)
Sends Telnet option to server with Telnet DO OPTION (request to use option)
command
|
void |
sendOptionSubnegotiation(TelnetOption opt)
Initiate subnegotiation for option.
|
void |
sendWillOption(TelnetOption opt)
Sends Telnet option to server with Telnet WILL OPTION (accept offer to use option)
command.
|
void |
sendWontOption(TelnetOption opt)
Sends Telnet option to server with Telnet WONT OPTION (refuse offer to use option)
command.
|
void |
setCharacterSet(java.lang.String characterSet)
Sets character set used when reading data from telnet server
|
void |
setDebug(boolean debug)
Enable debugging output to debugging stream.
|
void |
setDebugStream(java.io.PrintStream debugStream)
Sets PrintStream used in reporting debug statements.
|
void |
setHostname(java.lang.String hostname)
Set server hostname.
|
void |
setPort(int port)
Set server port.
|
void |
setProxyAuthentication(java.lang.String proxyUsername,
java.lang.String proxyPassword)
Sets the username and password to use when for authentication with
proxy server.
|
void |
setProxyHost(java.lang.String proxyHostname,
int proxyPort)
Sets the proxy hostname and port for this connection.
|
void |
setProxyType(java.lang.String proxyType)
Sets the proxy type will be used for this connection.
|
void |
setReadingTimeout(int value)
Sets the socket reading timeout value.
|
void |
setTimeout(int timeout)
Sets the timeout for opening connection to Telnet server.
|
public static final int TSC_NULL
public static final int TSC_BEL
public static final int TSC_BS
public static final int TSC_HT
public static final int TSC_LF
public static final int TSC_VT
public static final int TSC_FF
public static final int TSC_CR
public static final int TC_SE
public static final int TC_NOP
public static final int TC_DM
public static final int TC_BRK
public static final int TC_IP
public static final int TC_AO
public static final int TC_AYT
public static final int TSC_EC
public static final int TSC_EL
public static final int TC_GA
public static final int TC_SB
public static final int TC_WILL
public static final int TC_WONT
public static final int TC_DO
public static final int TC_DONT
public static final int TSC_IAC
public static final java.lang.String[] COMMAND_MAP
public Telnet(java.lang.String hostname)
hostname - the hostname or IP address of the Telnet serverpublic Telnet(java.lang.String host,
int port)
host - the hostname or IP address of the Telnet serverport - the port of the Telnet serverpublic void connect()
throws TelnetException
setHostname(java.lang.String), setPort(int) methods.TelnetExceptionpublic void disconnect()
public void setHostname(java.lang.String hostname)
hostname - the hostname or IP address of the Telnet serverpublic java.lang.String getHostname()
public void setPort(int port)
port - the port of the Telnet serverpublic int getPort()
public void setProxyAuthentication(java.lang.String proxyUsername,
java.lang.String proxyPassword)
#clearProxySettings method.proxyUsername - the proxy usernameproxyPassword - the proxy passwordclearProxySettings()public void setProxyHost(java.lang.String proxyHostname,
int proxyPort)
#clearProxySettings method.proxyHostname - the hostname or ip address of the proxy serverproxyPort - the port of the proxy serverclearProxySettings()public void setProxyType(java.lang.String proxyType)
proxyType - The proxy type. Valid values: HTTP, SOCKS5public void clearProxySettings()
public void addTelnetListener(TelnetListener listener)
listener - a TelnetListenerTelnetListenerpublic void removeTelnetListener(TelnetListener listener)
listener - a TelnetListenerTelnetListenerpublic void sendCommand(int command)
command - the command to sendpublic void sendCommand(int command,
int parameter)
command - the command to sendparameter - the parameter to sendpublic java.io.InputStream getInputStream()
public java.io.OutputStream getOutputStream()
public java.io.Reader getReader()
public java.io.Writer getWriter()
public void sendOptionSubnegotiation(TelnetOption opt)
opt - a TelnetOptionTelnetOptionpublic java.lang.String getCommandName(int command)
command - the command codepublic void sendWillOption(TelnetOption opt)
opt - the option to sendpublic void sendWontOption(TelnetOption opt)
opt - the option to sendpublic void sendDoOption(TelnetOption opt)
opt - the option to sendpublic void sendDontOption(TelnetOption opt)
opt - the option to sendpublic void setDebug(boolean debug)
debug - true if debugging enabled, false otherwisesetDebugStream(java.io.PrintStream)public boolean getDebug()
true if debugging is enabledpublic java.io.PrintStream getDebugStream()
PrintStreampublic void setTimeout(int timeout)
timeout - the timeout in millisecondspublic int getTimeout()
public int getReadingTimeout()
public void setReadingTimeout(int value)
value - timeout value is millispublic void setCharacterSet(java.lang.String characterSet)
characterSet - the character setpublic java.lang.String getCharacterSet()
public void setDebugStream(java.io.PrintStream debugStream)
debugStream - the PrintStream to send debug statements toPrintStreamCopyright © JSCAPE LLC. 1999-2021. All Rights Reserved