2 * This file is part of lanterna (http://code.google.com/p/lanterna/).
4 * lanterna is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU Lesser General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 * Copyright (C) 2010-2015 Martin
19 package com
.googlecode
.lanterna
.terminal
.ansi
;
21 import java
.io
.IOException
;
22 import java
.net
.ServerSocket
;
23 import java
.net
.Socket
;
24 import java
.nio
.charset
.Charset
;
25 import javax
.net
.ServerSocketFactory
;
28 * This class implements a Telnet server, capable of accepting multiple clients and presenting each one as their own
29 * Terminal. You need to tell it at least what port to listen on and then it create a Server socket listening for
30 * incoming connections. Use {@code acceptConnection()} to wait for the next incoming connection, it will be returned as
31 * a {@code TelnetTerminal} object that represents the client and which will be the way for the server to send content
32 * to this client. Next connecting client (through {@code acceptConnection()} will get a different
33 * {@code TelnetTerminal}, i.e. their content will not be in sync automatically but considered as two different
37 * @see <a href="http://en.wikipedia.org/wiki/Telnet">Wikipedia</a>
39 @SuppressWarnings("WeakerAccess")
40 public class TelnetTerminalServer
{
41 private final Charset charset
;
42 private final ServerSocket serverSocket
;
45 * Creates a new TelnetTerminalServer on a specific port
46 * @param port Port to listen for incoming telnet connections
47 * @throws IOException If there was an underlying I/O exception
49 public TelnetTerminalServer(int port
) throws IOException
{
50 this(ServerSocketFactory
.getDefault(), port
);
54 * Creates a new TelnetTerminalServer on a specific port, using a certain character set
55 * @param port Port to listen for incoming telnet connections
56 * @param charset Character set to use
57 * @throws IOException If there was an underlying I/O exception
59 public TelnetTerminalServer(int port
, Charset charset
) throws IOException
{
60 this(ServerSocketFactory
.getDefault(), port
, charset
);
64 * Creates a new TelnetTerminalServer on a specific port through a ServerSocketFactory
65 * @param port Port to listen for incoming telnet connections
66 * @param serverSocketFactory ServerSocketFactory to use when creating the ServerSocket
67 * @throws IOException If there was an underlying I/O exception
69 public TelnetTerminalServer(ServerSocketFactory serverSocketFactory
, int port
) throws IOException
{
70 this(serverSocketFactory
, port
, Charset
.defaultCharset());
74 * Creates a new TelnetTerminalServer on a specific port through a ServerSocketFactory with a certain Charset
75 * @param serverSocketFactory ServerSocketFactory to use when creating the ServerSocket
76 * @param port Port to listen for incoming telnet connections
77 * @param charset Character set to use
78 * @throws IOException If there was an underlying I/O exception
80 public TelnetTerminalServer(ServerSocketFactory serverSocketFactory
, int port
, Charset charset
) throws IOException
{
81 this.serverSocket
= serverSocketFactory
.createServerSocket(port
);
82 this.charset
= charset
;
86 * Returns the actual server socket used by this object. Can be used to tweak settings but be careful!
87 * @return Underlying ServerSocket
89 public ServerSocket
getServerSocket() {
94 * Waits for the next client to connect in to our server and returns a Terminal implementation, TelnetTerminal, that
95 * represents the remote terminal this client is running. The terminal can be used just like any other Terminal, but
96 * keep in mind that all operations are sent over the network.
97 * @return TelnetTerminal for the remote client's terminal
98 * @throws IOException If there was an underlying I/O exception
100 public TelnetTerminal
acceptConnection() throws IOException
{
101 Socket clientSocket
= serverSocket
.accept();
102 clientSocket
.setTcpNoDelay(true);
103 return new TelnetTerminal(clientSocket
, charset
);
107 * Closes the server socket, accepting no new connection. Any call to acceptConnection() after this will fail.
108 * @throws IOException If there was an underlying I/O exception
110 public void close() throws IOException
{
111 serverSocket
.close();