2 * Jexer - Java Text User Interface
4 * License: LGPLv3 or later
6 * This module is licensed under the GNU Lesser General Public License
7 * Version 3. Please see the file "COPYING" in this directory for more
8 * information about the GNU Lesser General Public License Version 3.
10 * Copyright (C) 2015 Kevin Lamonte
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU Lesser General Public License
14 * as published by the Free Software Foundation; either version 3 of
15 * the License, or (at your option) any later version.
17 * This program is distributed in the hope that it will be useful, but
18 * WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 * General Public License for more details.
22 * You should have received a copy of the GNU Lesser General Public
23 * License along with this program; if not, see
24 * http://www.gnu.org/licenses/, or write to the Free Software
25 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
28 * @author Kevin Lamonte [kevin.lamonte@gmail.com]
33 import java
.io
.InputStream
;
34 import java
.io
.IOException
;
35 import java
.io
.OutputStream
;
36 import java
.net
.Socket
;
39 * This class provides a Socket that performs the telnet protocol to both
40 * establish an 8-bit clean no echo channel and expose window resize events
41 * to the Jexer ECMA48 backend.
43 public final class TelnetSocket
extends Socket
{
48 private Socket socket
;
51 * The telnet-aware socket InputStream.
53 private TelnetInputStream input
;
56 * The telnet-aware socket OutputStream. Note package private access:
57 * input sends stuff to output.
59 TelnetOutputStream output
;
61 // Telnet protocol special characters. Note package private access.
62 public static final int TELNET_SE
= 240;
63 public static final int TELNET_NOP
= 241;
64 public static final int TELNET_DM
= 242;
65 public static final int TELNET_BRK
= 243;
66 public static final int TELNET_IP
= 244;
67 public static final int TELNET_AO
= 245;
68 public static final int TELNET_AYT
= 246;
69 public static final int TELNET_EC
= 247;
70 public static final int TELNET_EL
= 248;
71 public static final int TELNET_GA
= 249;
72 public static final int TELNET_SB
= 250;
73 public static final int TELNET_WILL
= 251;
74 public static final int TELNET_WONT
= 252;
75 public static final int TELNET_DO
= 253;
76 public static final int TELNET_DONT
= 254;
77 public static final int TELNET_IAC
= 255;
78 public static final int C_NUL
= 0x00;
79 public static final int C_LF
= 0x0A;
80 public static final int C_CR
= 0x0D;
83 * Telnet protocol speaks to a Network Virtual Terminal (NVT).
87 // General status flags outside the NVT spec
98 boolean doEnvironment
;
101 // Flags used by the TelnetInputStream
110 // Flags used by the TelnetOutputStream
114 * Constuctor calls reset().
116 public TelnetState() {
121 * Reset NVT to default state as per RFC 854.
123 public void reset() {
130 doEnvironment
= true;
146 * State of the protocol. Note package private access.
151 * See if telnet server/client is in ASCII mode.
153 * @return if true, this connection is in ASCII mode
155 public boolean isAscii() {
156 return (!nvt
.binaryMode
);
160 * Creates a Socket that knows the telnet protocol.
162 * @param socket the underlying Socket
164 TelnetSocket(Socket socket
) throws IOException
{
166 nvt
= new TelnetState();
167 this.socket
= socket
;
169 output
= new TelnetOutputStream(this, super.getOutputStream());
170 input
= new TelnetInputStream(this, super.getInputStream(), output
);
172 // Initiate the telnet protocol negotiation.
173 input
.telnetSendOptions();
176 // Socket interface -------------------------------------------------------
179 * Returns an input stream for this socket.
181 * @return the input stream
184 public InputStream
getInputStream() throws IOException
{
189 * Returns an output stream for this socket.
191 * @return the output stream
194 public OutputStream
getOutputStream() throws IOException
{