Class TelnetClient
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.
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate static final int
private InputStream
private TelnetInputListener
(package private) final int
private OutputStream
protected boolean
Fields inherited from class org.apache.commons.net.telnet.Telnet
COMMAND_AYT, COMMAND_DO, COMMAND_DONT, COMMAND_IS, COMMAND_SB, COMMAND_SE, COMMAND_WILL, COMMAND_WONT, debug, debugoptions, DEFAULT_PORT, DO_MASK, REQUESTED_DO_MASK, REQUESTED_WILL_MASK, TERMINAL_TYPE, TERMINAL_TYPE_IS, TERMINAL_TYPE_SEND, WILL_MASK
Fields inherited from class org.apache.commons.net.SocketClient
_defaultPort_, _hostname_, _input_, _output_, _serverSocketFactory_, _socket_, _socketFactory_, _timeout_, connectTimeout, NETASCII_EOL
-
Constructor Summary
ConstructorsConstructorDescriptionDefault TelnetClient constructor, sets terminal-typeVT100
.TelnetClient
(int maxSubnegotiationLength) Construct an instance with the specified max subnegotiation length and the default terminal-typeVT100
TelnetClient
(String termtype) Construct an instance with the specified terminal type.TelnetClient
(String termtype, int maxSubnegotiationLength) Construct an instance with the specified terminal type and max subnegotiation length -
Method Summary
Modifier and TypeMethodDescriptionprotected void
Handles special connection requirements.void
addOptionHandler
(TelnetOptionHandler opthand) Registers a new TelnetOptionHandler for this telnet client to use.(package private) void
void
deleteOptionHandler
(int optcode) Unregisters a TelnetOptionHandler.void
Disconnects the telnet session, closing the input and output streams as well as the socket.(package private) void
Returns the telnet connection input stream.boolean
getLocalOptionState
(int option) Returns the state of the option on the local side.Returns the telnet connection output stream.boolean
Gets the status of the reader thread.boolean
getRemoteOptionState
(int option) Returns the state of the option on the remote side.(package private) void
void
registerInputListener
(TelnetInputListener listener) Register a listener to be notified when new incoming data is available to be read on theinput stream
.void
registerNotifHandler
(TelnetNotificationHandler notifhand) Registers a notification handler to which will be sent notifications of received telnet option negotiation commands.void
registerSpyStream
(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
Stops spying this TelnetClient.void
Unregisters the currentTelnetInputListener
, if any.void
Unregisters the current notification handler.Methods inherited from class org.apache.commons.net.telnet.Telnet
_registerSpyStream, _sendAYT, _sendCommand, _sendSubnegotiation, _stopSpyStream, processAYTResponse, processCommand, processDo, processDont, processSuboption, processWill, processWont, requestDo, requestDont, requestedDo, requestedDont, requestedWill, requestedWont, requestWill, requestWont, sendByte, sendDo, sendDont, sendTerminalType, sendWill, sendWont, setDo, setDont, setWantDo, setWantDont, setWantWill, setWantWont, setWill, setWont, spyRead, spyWrite, stateIsDo, stateIsDont, stateIsWill, stateIsWont
Methods inherited from class org.apache.commons.net.SocketClient
addProtocolCommandListener, applySocketAttributes, 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
-
Field Details
-
DEFAULT_MAX_SUBNEGOTIATION_LENGTH
private static final int DEFAULT_MAX_SUBNEGOTIATION_LENGTH- See Also:
-
maxSubnegotiationLength
final int maxSubnegotiationLength -
input
-
output
-
readerThread
protected boolean readerThread -
inputListener
-
-
Constructor Details
-
TelnetClient
public TelnetClient()Default TelnetClient constructor, sets terminal-typeVT100
. -
TelnetClient
Construct an instance with the specified terminal type.- Parameters:
termtype
- the terminal type to use, e.g.VT100
-
TelnetClient
public TelnetClient(int maxSubnegotiationLength) Construct an instance with the specified max subnegotiation length and the default terminal-typeVT100
- Parameters:
maxSubnegotiationLength
- the size of the subnegotiation buffer
-
TelnetClient
Construct an instance with the specified terminal type and max subnegotiation length- Parameters:
termtype
- the terminal type to use, e.g.VT100
maxSubnegotiationLength
- the size of the subnegotiation buffer
-
-
Method Details
-
flushOutputStream
- Throws:
IOException
-
closeOutputStream
- Throws:
IOException
-
_connectAction_
Handles special connection requirements.- Overrides:
_connectAction_
in classTelnet
- Throws:
IOException
- If an error occurs during connection setup.
-
disconnect
Disconnects the telnet session, closing the input and output streams as well as the socket. If you have references to the input and output streams of the telnet connection, you should not close them yourself, but rather call disconnect to properly close the connection.- Overrides:
disconnect
in classSocketClient
- Throws:
IOException
- If there is an error closing the socket.
-
getOutputStream
Returns the telnet connection output stream. You should not close the stream when you finish with it. Rather, you should calldisconnect
.- Returns:
- The telnet connection output stream.
-
getInputStream
Returns the telnet connection input stream. You should not close the stream when you finish with it. Rather, you should calldisconnect
.- Returns:
- The telnet connection input stream.
-
getLocalOptionState
public boolean getLocalOptionState(int option) Returns the state of the option on the local side.- Parameters:
option
- - Option to be checked.- Returns:
- The state of the option on the local side.
-
getRemoteOptionState
public boolean getRemoteOptionState(int option) Returns the state of the option on the remote side.- Parameters:
option
- - Option to be checked.- Returns:
- The state of the option on the remote side.
-
sendAYT
public boolean sendAYT(long timeout) throws IOException, IllegalArgumentException, InterruptedException Sends an Are You There sequence and waits for the result.- Parameters:
timeout
- - Time to wait for a response (millis.)- Returns:
- true if AYT received a response, false otherwise
- Throws:
InterruptedException
- on errorIllegalArgumentException
- on errorIOException
- on error
-
sendSubnegotiation
Sends a protocol-specific subnegotiation message to the remote peer.TelnetClient
will add the IAC SB & IAC SE framing bytes; the first byte inmessage
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
.- Parameters:
message
- option code followed by subnegotiation payload- Throws:
IllegalArgumentException
- ifmessage
has length zeroIOException
- if an I/O error occurs while writing the message- Since:
- 3.0
-
sendCommand
Sends a command byte to the remote peer, adding the IAC prefix.This method does not wait for any response. Messages sent by the remote end can be handled by registering an approrpriate
TelnetOptionHandler
.- Parameters:
command
- the code for the command- Throws:
IOException
- if an I/O error occurs while writing the messageIllegalArgumentException
- on error- Since:
- 3.0
-
addOptionHandler
public void addOptionHandler(TelnetOptionHandler opthand) throws InvalidTelnetOptionException, IOException Registers a new TelnetOptionHandler for this telnet client to use.- Overrides:
addOptionHandler
in classTelnet
- Parameters:
opthand
- - option handler to be registered.- Throws:
InvalidTelnetOptionException
- on errorIOException
- on error
-
deleteOptionHandler
Unregisters a TelnetOptionHandler.- Overrides:
deleteOptionHandler
in classTelnet
- Parameters:
optcode
- - Code of the option to be unregistered.- Throws:
InvalidTelnetOptionException
- on errorIOException
- on error
-
registerSpyStream
Registers an OutputStream for spying what's going on in the TelnetClient session.- Parameters:
spystream
- - OutputStream on which session activity will be echoed.
-
stopSpyStream
public void stopSpyStream()Stops spying this TelnetClient. -
registerNotifHandler
Registers a notification handler to which will be sent notifications of received telnet option negotiation commands.- Overrides:
registerNotifHandler
in classTelnet
- Parameters:
notifhand
- - TelnetNotificationHandler to be registered
-
unregisterNotifHandler
public void unregisterNotifHandler()Unregisters the current notification handler.- Overrides:
unregisterNotifHandler
in classTelnet
-
setReaderThread
public void setReaderThread(boolean flag) Sets the status of the reader thread.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.
- Parameters:
flag
- true to enable the reader thread, false to disable- See Also:
-
getReaderThread
public boolean getReaderThread()Gets the status of the reader thread.- Returns:
- true if the reader thread is enabled, false otherwise
-
registerInputListener
Register a listener to be notified when new incoming data is available to be read on theinput 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.- Parameters:
listener
- listener to be registered; replaces any previous- Since:
- 3.0
-
unregisterInputListener
public void unregisterInputListener()Unregisters the currentTelnetInputListener
, if any.- Since:
- 3.0
-
notifyInputListener
void notifyInputListener()
-