Merge commit '77d3a60869e7a780c6ae069e51530e1eacece5e2'
[fanfix.git] / src / jexer / net / TelnetServerSocket.java
CommitLineData
daa4106c 1/*
ea91242c
KL
2 * Jexer - Java Text User Interface
3 *
e16dda65 4 * The MIT License (MIT)
ea91242c 5 *
a69ed767 6 * Copyright (C) 2019 Kevin Lamonte
ea91242c 7 *
e16dda65
KL
8 * Permission is hereby granted, free of charge, to any person obtaining a
9 * copy of this software and associated documentation files (the "Software"),
10 * to deal in the Software without restriction, including without limitation
11 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
12 * and/or sell copies of the Software, and to permit persons to whom the
13 * Software is furnished to do so, subject to the following conditions:
ea91242c 14 *
e16dda65
KL
15 * The above copyright notice and this permission notice shall be included in
16 * all copies or substantial portions of the Software.
ea91242c 17 *
e16dda65
KL
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
21 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
23 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
24 * DEALINGS IN THE SOFTWARE.
ea91242c
KL
25 *
26 * @author Kevin Lamonte [kevin.lamonte@gmail.com]
27 * @version 1
28 */
29package jexer.net;
30
ea91242c 31import java.io.IOException;
ea91242c
KL
32import java.net.InetAddress;
33import java.net.ServerSocket;
34import java.net.Socket;
9b1afdde 35import java.net.SocketException;
ea91242c
KL
36
37/**
38 * This class provides a ServerSocket that return TelnetSocket's in accept().
39 */
051e2913 40public class TelnetServerSocket extends ServerSocket {
ea91242c 41
d36057df
KL
42 // ------------------------------------------------------------------------
43 // Variables --------------------------------------------------------------
44 // ------------------------------------------------------------------------
45
46
47 // ------------------------------------------------------------------------
48 // Constructors -----------------------------------------------------------
49 // ------------------------------------------------------------------------
ea91242c
KL
50
51 /**
52 * Creates an unbound server socket.
9b1afdde
KL
53 *
54 * @throws IOException if an I/O error occurs
ea91242c
KL
55 */
56 public TelnetServerSocket() throws IOException {
57 super();
58 }
59
60 /**
61 * Creates a server socket, bound to the specified port.
9b1afdde
KL
62 *
63 * @param port the port number, or 0 to use a port number that is
64 * automatically allocated.
65 * @throws IOException if an I/O error occurs
ea91242c
KL
66 */
67 public TelnetServerSocket(final int port) throws IOException {
68 super(port);
69 }
70
71 /**
72 * Creates a server socket and binds it to the specified local port
73 * number, with the specified backlog.
9b1afdde
KL
74 *
75 * @param port the port number, or 0 to use a port number that is
76 * automatically allocated.
77 * @param backlog requested maximum length of the queue of incoming
78 * connections.
79 * @throws IOException if an I/O error occurs
ea91242c
KL
80 */
81 public TelnetServerSocket(final int port,
82 final int backlog) throws IOException {
83
84 super(port, backlog);
85 }
86
87 /**
88 * Create a server with the specified port, listen backlog, and local IP
89 * address to bind to.
9b1afdde
KL
90 *
91 * @param port the port number, or 0 to use a port number that is
92 * automatically allocated.
93 * @param backlog requested maximum length of the queue of incoming
94 * connections.
95 * @param bindAddr the local InetAddress the server will bind to
96 * @throws IOException if an I/O error occurs
ea91242c
KL
97 */
98 public TelnetServerSocket(final int port, final int backlog,
99 final InetAddress bindAddr) throws IOException {
100
101 super(port, backlog, bindAddr);
102 }
103
d36057df
KL
104 // ------------------------------------------------------------------------
105 // ServerSocket -----------------------------------------------------------
106 // ------------------------------------------------------------------------
107
ea91242c
KL
108 /**
109 * Listens for a connection to be made to this socket and accepts it. The
110 * method blocks until a connection is made.
9b1afdde
KL
111 *
112 * @return the new Socket
113 * @throws IOException if an I/O error occurs
ea91242c
KL
114 */
115 @Override
116 public Socket accept() throws IOException {
9b1afdde
KL
117 if (isClosed()) {
118 throw new SocketException("Socket is closed");
119 }
120 if (!isBound()) {
121 throw new SocketException("Socket is not bound");
122 }
123
124 Socket socket = new TelnetSocket();
125 implAccept(socket);
126 return socket;
ea91242c 127 }
9b1afdde 128
ea91242c 129}