public class TelnetClient extends Telnet
The class can be used by first connecting to a server using the
SocketClient
connect
method. Then an InputStream and OutputStream for sending and
receiving data over the Telnet connection can be obtained by
using the getInputStream()
and
getOutputStream()
methods.
When you finish using the streams, you must call
disconnect
rather than simply
closing the streams.
Modifier and Type | Field and Description |
---|---|
private java.io.InputStream |
__input |
private java.io.OutputStream |
__output |
private TelnetInputListener |
inputListener |
protected boolean |
readerThread |
_COMMAND_AYT, _COMMAND_DO, _COMMAND_DONT, _COMMAND_IS, _COMMAND_SB, _COMMAND_SE, _COMMAND_WILL, _COMMAND_WONT, _DO_MASK, _doResponse, _options, _REQUESTED_DO_MASK, _REQUESTED_WILL_MASK, _WILL_MASK, _willResponse, debug, debugoptions, DEFAULT_PORT, TERMINAL_TYPE, TERMINAL_TYPE_IS, TERMINAL_TYPE_SEND
_defaultPort_, _hostname_, _input_, _output_, _serverSocketFactory_, _socket_, _socketFactory_, _timeout_, connectTimeout, NETASCII_EOL
Constructor and Description |
---|
TelnetClient()
Default TelnetClient constructor, sets terminal-type
VT100 . |
TelnetClient(java.lang.String termtype)
Construct an instance with the specified terminal type.
|
Modifier and Type | Method and Description |
---|---|
(package private) void |
_closeOutputStream() |
protected void |
_connectAction_()
Handles special connection requirements.
|
(package private) void |
_flushOutputStream() |
void |
addOptionHandler(TelnetOptionHandler opthand)
Registers a new TelnetOptionHandler for this telnet client to use.
|
void |
deleteOptionHandler(int optcode)
Unregisters a TelnetOptionHandler.
|
void |
disconnect()
Disconnects the telnet session, closing the input and output streams
as well as the socket.
|
java.io.InputStream |
getInputStream()
Returns the telnet connection input stream.
|
boolean |
getLocalOptionState(int option)
Returns the state of the option on the local side.
|
java.io.OutputStream |
getOutputStream()
Returns the telnet connection output stream.
|
boolean |
getReaderThread()
Gets the status of the reader thread.
|
boolean |
getRemoteOptionState(int option)
Returns the state of the option on the remote side.
|
(package private) void |
notifyInputListener() |
void |
registerInputListener(TelnetInputListener listener)
Register a listener to be notified when new incoming data is
available to be read on the
input stream . |
void |
registerNotifHandler(TelnetNotificationHandler notifhand)
Registers a notification handler to which will be sent
notifications of received telnet option negotiation commands.
|
void |
registerSpyStream(java.io.OutputStream spystream)
Registers an OutputStream for spying what's going on in
the TelnetClient session.
|
boolean |
sendAYT(long timeout)
Sends an Are You There sequence and waits for the result.
|
void |
sendCommand(byte command)
Sends a command byte to the remote peer, adding the IAC prefix.
|
void |
sendSubnegotiation(int[] message)
Sends a protocol-specific subnegotiation message to the remote peer.
|
void |
setReaderThread(boolean flag)
Sets the status of the reader thread.
|
void |
stopSpyStream()
Stops spying this TelnetClient.
|
void |
unregisterInputListener()
Unregisters the current
TelnetInputListener , if any. |
void |
unregisterNotifHandler()
Unregisters the current notification handler.
|
_processAYTResponse, _processCommand, _processDo, _processDont, _processSuboption, _processWill, _processWont, _registerSpyStream, _requestDo, _requestDont, _requestedDo, _requestedDont, _requestedWill, _requestedWont, _requestWill, _requestWont, _sendAYT, _sendByte, _sendCommand, _sendDo, _sendDont, _sendSubnegotiation, _sendTerminalType, _sendWill, _sendWont, _setDo, _setDont, _setWantDo, _setWantDont, _setWantWill, _setWantWont, _setWill, _setWont, _spyRead, _spyWrite, _stateIsDo, _stateIsDont, _stateIsWill, _stateIsWont, _stopSpyStream
addProtocolCommandListener, connect, connect, connect, connect, connect, connect, createCommandSupport, fireCommandSent, fireReplyReceived, getCharset, getCharsetName, getCommandSupport, getConnectTimeout, getDefaultPort, getDefaultTimeout, getKeepAlive, getLocalAddress, getLocalPort, getProxy, getReceiveBufferSize, getRemoteAddress, getRemotePort, getSendBufferSize, getServerSocketFactory, getSoLinger, getSoTimeout, getTcpNoDelay, isAvailable, isConnected, removeProtocolCommandListener, setCharset, setConnectTimeout, setDefaultPort, setDefaultTimeout, setKeepAlive, setProxy, setReceiveBufferSize, setSendBufferSize, setServerSocketFactory, setSocketFactory, setSoLinger, setSoTimeout, setTcpNoDelay, verifyRemote
private java.io.InputStream __input
private java.io.OutputStream __output
protected boolean readerThread
private TelnetInputListener inputListener
public TelnetClient()
VT100
.public TelnetClient(java.lang.String termtype)
termtype
- the terminal type to use, e.g. VT100
void _flushOutputStream() throws java.io.IOException
java.io.IOException
void _closeOutputStream() throws java.io.IOException
java.io.IOException
protected void _connectAction_() throws java.io.IOException
_connectAction_
in class Telnet
java.io.IOException
- If an error occurs during connection setup.public void disconnect() throws java.io.IOException
disconnect
in class SocketClient
java.io.IOException
- If there is an error closing the socket.public java.io.OutputStream getOutputStream()
disconnect
.public java.io.InputStream getInputStream()
disconnect
.public boolean getLocalOptionState(int option)
option
- - Option to be checked.public boolean getRemoteOptionState(int option)
option
- - Option to be checked.public boolean sendAYT(long timeout) throws java.io.IOException, java.lang.IllegalArgumentException, java.lang.InterruptedException
timeout
- - Time to wait for a response (millis.)java.lang.InterruptedException
- on errorjava.lang.IllegalArgumentException
- on errorjava.io.IOException
- on errorpublic void sendSubnegotiation(int[] message) throws java.io.IOException, java.lang.IllegalArgumentException
TelnetClient
will add the IAC SB & IAC SE framing bytes;
the first byte in message
should be the appropriate telnet
option code.
This method does not wait for any response. Subnegotiation messages
sent by the remote end can be handled by registering an approrpriate
TelnetOptionHandler
.
message
- option code followed by subnegotiation payloadjava.lang.IllegalArgumentException
- if message
has length zerojava.io.IOException
- if an I/O error occurs while writing the messagepublic void sendCommand(byte command) throws java.io.IOException, java.lang.IllegalArgumentException
This method does not wait for any response. Messages
sent by the remote end can be handled by registering an approrpriate
TelnetOptionHandler
.
command
- the code for the commandjava.io.IOException
- if an I/O error occurs while writing the messagejava.lang.IllegalArgumentException
- on errorpublic void addOptionHandler(TelnetOptionHandler opthand) throws InvalidTelnetOptionException, java.io.IOException
addOptionHandler
in class Telnet
opthand
- - option handler to be registered.InvalidTelnetOptionException
- on errorjava.io.IOException
- on errorpublic void deleteOptionHandler(int optcode) throws InvalidTelnetOptionException, java.io.IOException
deleteOptionHandler
in class Telnet
optcode
- - Code of the option to be unregistered.InvalidTelnetOptionException
- on errorjava.io.IOException
- on errorpublic void registerSpyStream(java.io.OutputStream spystream)
spystream
- - OutputStream on which session activity
will be echoed.public void stopSpyStream()
public void registerNotifHandler(TelnetNotificationHandler notifhand)
registerNotifHandler
in class Telnet
notifhand
- - TelnetNotificationHandler to be registeredpublic void unregisterNotifHandler()
unregisterNotifHandler
in class Telnet
public void setReaderThread(boolean flag)
When enabled, a seaparate internal reader thread is created for new
connections to read incoming data as it arrives. This results in
immediate handling of option negotiation, notifications, etc.
(at least until the fixed-size internal buffer fills up).
Otherwise, no thread is created an all negotiation and option
handling is deferred until a read() is performed on the
input stream
.
The reader thread must be enabled for TelnetInputListener
support.
When this method is invoked, the reader thread status will apply to all subsequent connections; the current connection (if any) is not affected.
flag
- true to enable the reader thread, false to disableregisterInputListener(org.apache.commons.net.telnet.TelnetInputListener)
public boolean getReaderThread()
public void registerInputListener(TelnetInputListener listener)
input stream
.
Only one listener is supported at a time.
More precisely, notifications are issued whenever the number of
bytes available for immediate reading (i.e., the value returned
by InputStream.available()
) transitions from zero to non-zero.
Note that (in general) multiple reads may be required to empty the
buffer and reset this notification, because incoming bytes are being
added to the internal buffer asynchronously.
Notifications are only supported when a reader thread
is enabled for the connection.
listener
- listener to be registered; replaces any previouspublic void unregisterInputListener()
TelnetInputListener
, if any.void notifyInputListener()