Class TelnetClient
- java.lang.Object
-
- org.apache.commons.net.SocketClient
-
- org.apache.commons.net.telnet.TelnetClient
-
public class TelnetClient extends SocketClient
The TelnetClient class implements the simple network virtual terminal (NVT) for the Telnet protocol according to RFC 854. It does not implement any of the extra Telnet options because it is meant to be used within a Java program providing automated access to Telnet accessible resources.The class can be used by first connecting to a server using the SocketClient
connectmethod. Then an InputStream and OutputStream for sending and receiving data over the Telnet connection can be obtained by using thegetInputStream()andgetOutputStream()methods. When you finish using the streams, you must calldisconnectrather than simply closing the streams.
-
-
Field Summary
Fields Modifier and Type Field Description protected booleanreaderThreadprotected static intTERMINAL_TYPETerminal type optionprotected static intTERMINAL_TYPE_ISIs (for subnegotiation)protected static intTERMINAL_TYPE_SENDSend (for subnegotiation)-
Fields inherited from class org.apache.commons.net.SocketClient
_defaultPort_, _hostname_, _input_, _output_, _serverSocketFactory_, _socket_, _socketFactory_, _timeout_, connectTimeout, NETASCII_EOL
-
-
Constructor Summary
Constructors Constructor Description TelnetClient()Default TelnetClient constructor, sets terminal-typeVT100.TelnetClient(java.lang.String termtype)Construct an instance with the specified terminal type.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description protected void_connectAction_()Handles special connection requirements.voidaddOptionHandler(TelnetOptionHandler opthand)Registers a new TelnetOptionHandler for this telnet client to use.voiddeleteOptionHandler(int optcode)Unregisters a TelnetOptionHandler.voiddisconnect()Disconnects the telnet session, closing the input and output streams as well as the socket.java.io.InputStreamgetInputStream()Returns the telnet connection input stream.booleangetLocalOptionState(int option)Returns the state of the option on the local side.java.io.OutputStreamgetOutputStream()Returns the telnet connection output stream.booleangetReaderThread()Gets the status of the reader thread.booleangetRemoteOptionState(int option)Returns the state of the option on the remote side.voidregisterInputListener(TelnetInputListener listener)Register a listener to be notified when new incoming data is available to be read on theinput stream.voidregisterNotifHandler(TelnetNotificationHandler notifhand)Registers a notification handler to which will be sent notifications of received telnet option negotiation commands.voidregisterSpyStream(java.io.OutputStream spystream)Registers an OutputStream for spying what's going on in the TelnetClient session.booleansendAYT(long timeout)Sends an Are You There sequence and waits for the result.voidsendCommand(byte command)Sends a command byte to the remote peer, adding the IAC prefix.voidsendSubnegotiation(int[] message)Sends a protocol-specific subnegotiation message to the remote peer.voidsetReaderThread(boolean flag)Sets the status of the reader thread.voidstopSpyStream()Stops spying this TelnetClient.voidunregisterInputListener()Unregisters the currentTelnetInputListener, if any.voidunregisterNotifHandler()Unregisters the current notification handler.-
Methods inherited from class org.apache.commons.net.SocketClient
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
-
-
-
-
Field Detail
-
readerThread
protected boolean readerThread
-
TERMINAL_TYPE
protected static final int TERMINAL_TYPE
Terminal type option- See Also:
- Constant Field Values
-
TERMINAL_TYPE_SEND
protected static final int TERMINAL_TYPE_SEND
Send (for subnegotiation)- See Also:
- Constant Field Values
-
TERMINAL_TYPE_IS
protected static final int TERMINAL_TYPE_IS
Is (for subnegotiation)- See Also:
- Constant Field Values
-
-
Method Detail
-
_connectAction_
protected void _connectAction_() throws java.io.IOExceptionHandles special connection requirements.- Throws:
java.io.IOException- If an error occurs during connection setup.
-
disconnect
public void disconnect() throws java.io.IOExceptionDisconnects 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:
disconnectin classSocketClient- Throws:
java.io.IOException- If there is an error closing the socket.
-
getOutputStream
public java.io.OutputStream 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
public java.io.InputStream 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 java.io.IOException, java.lang.IllegalArgumentException, java.lang.InterruptedExceptionSends 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:
java.lang.InterruptedException- on errorjava.lang.IllegalArgumentException- on errorjava.io.IOException- on error
-
sendSubnegotiation
public void sendSubnegotiation(int[] message) throws java.io.IOException, java.lang.IllegalArgumentExceptionSends a protocol-specific subnegotiation message to the remote peer.TelnetClientwill add the IAC SB & IAC SE framing bytes; the first byte inmessageshould 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:
java.lang.IllegalArgumentException- ifmessagehas length zerojava.io.IOException- if an I/O error occurs while writing the message- Since:
- 3.0
-
sendCommand
public void sendCommand(byte command) throws java.io.IOException, java.lang.IllegalArgumentExceptionSends 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:
java.io.IOException- if an I/O error occurs while writing the messagejava.lang.IllegalArgumentException- on error- Since:
- 3.0
-
addOptionHandler
public void addOptionHandler(TelnetOptionHandler opthand) throws InvalidTelnetOptionException, java.io.IOException
Registers a new TelnetOptionHandler for this telnet client to use.- Parameters:
opthand- - option handler to be registered.- Throws:
InvalidTelnetOptionException- on errorjava.io.IOException- on error
-
deleteOptionHandler
public void deleteOptionHandler(int optcode) throws InvalidTelnetOptionException, java.io.IOExceptionUnregisters a TelnetOptionHandler.- Parameters:
optcode- - Code of the option to be unregistered.- Throws:
InvalidTelnetOptionException- on errorjava.io.IOException- on error
-
registerSpyStream
public void registerSpyStream(java.io.OutputStream spystream)
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
public void registerNotifHandler(TelnetNotificationHandler notifhand)
Registers a notification handler to which will be sent notifications of received telnet option negotiation commands.- Parameters:
notifhand- - TelnetNotificationHandler to be registered
-
unregisterNotifHandler
public void unregisterNotifHandler()
Unregisters the current notification handler.
-
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
TelnetInputListenersupport.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:
registerInputListener(org.apache.commons.net.telnet.TelnetInputListener)
-
getReaderThread
public boolean getReaderThread()
Gets the status of the reader thread.- Returns:
- true if the reader thread is enabled, false otherwise
-
registerInputListener
public void registerInputListener(TelnetInputListener listener)
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 threadis 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
-
-