| 1 | /* |
| 2 | * Jexer - Java Text User Interface |
| 3 | * |
| 4 | * License: LGPLv3 or later |
| 5 | * |
| 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. |
| 9 | * |
| 10 | * Copyright (C) 2015 Kevin Lamonte |
| 11 | * |
| 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. |
| 16 | * |
| 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. |
| 21 | * |
| 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 |
| 26 | * 02110-1301 USA |
| 27 | * |
| 28 | * @author Kevin Lamonte [kevin.lamonte@gmail.com] |
| 29 | * @version 1 |
| 30 | */ |
| 31 | package jexer.net; |
| 32 | |
| 33 | import java.io.IOException; |
| 34 | import java.net.InetAddress; |
| 35 | import java.net.ServerSocket; |
| 36 | import java.net.Socket; |
| 37 | import java.net.SocketException; |
| 38 | |
| 39 | /** |
| 40 | * This class provides a ServerSocket that return TelnetSocket's in accept(). |
| 41 | */ |
| 42 | public final class TelnetServerSocket extends ServerSocket { |
| 43 | |
| 44 | // ServerSocket interface ------------------------------------------------- |
| 45 | |
| 46 | /** |
| 47 | * Creates an unbound server socket. |
| 48 | * |
| 49 | * @throws IOException if an I/O error occurs |
| 50 | */ |
| 51 | public TelnetServerSocket() throws IOException { |
| 52 | super(); |
| 53 | } |
| 54 | |
| 55 | /** |
| 56 | * Creates a server socket, bound to the specified port. |
| 57 | * |
| 58 | * @param port the port number, or 0 to use a port number that is |
| 59 | * automatically allocated. |
| 60 | * @throws IOException if an I/O error occurs |
| 61 | */ |
| 62 | public TelnetServerSocket(final int port) throws IOException { |
| 63 | super(port); |
| 64 | } |
| 65 | |
| 66 | /** |
| 67 | * Creates a server socket and binds it to the specified local port |
| 68 | * number, with the specified backlog. |
| 69 | * |
| 70 | * @param port the port number, or 0 to use a port number that is |
| 71 | * automatically allocated. |
| 72 | * @param backlog requested maximum length of the queue of incoming |
| 73 | * connections. |
| 74 | * @throws IOException if an I/O error occurs |
| 75 | */ |
| 76 | public TelnetServerSocket(final int port, |
| 77 | final int backlog) throws IOException { |
| 78 | |
| 79 | super(port, backlog); |
| 80 | } |
| 81 | |
| 82 | /** |
| 83 | * Create a server with the specified port, listen backlog, and local IP |
| 84 | * address to bind to. |
| 85 | * |
| 86 | * @param port the port number, or 0 to use a port number that is |
| 87 | * automatically allocated. |
| 88 | * @param backlog requested maximum length of the queue of incoming |
| 89 | * connections. |
| 90 | * @param bindAddr the local InetAddress the server will bind to |
| 91 | * @throws IOException if an I/O error occurs |
| 92 | */ |
| 93 | public TelnetServerSocket(final int port, final int backlog, |
| 94 | final InetAddress bindAddr) throws IOException { |
| 95 | |
| 96 | super(port, backlog, bindAddr); |
| 97 | } |
| 98 | |
| 99 | /** |
| 100 | * Listens for a connection to be made to this socket and accepts it. The |
| 101 | * method blocks until a connection is made. |
| 102 | * |
| 103 | * @return the new Socket |
| 104 | * @throws IOException if an I/O error occurs |
| 105 | */ |
| 106 | @Override |
| 107 | public Socket accept() throws IOException { |
| 108 | if (isClosed()) { |
| 109 | throw new SocketException("Socket is closed"); |
| 110 | } |
| 111 | if (!isBound()) { |
| 112 | throw new SocketException("Socket is not bound"); |
| 113 | } |
| 114 | |
| 115 | Socket socket = new TelnetSocket(); |
| 116 | implAccept(socket); |
| 117 | return socket; |
| 118 | } |
| 119 | |
| 120 | } |