View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      https://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.commons.net.telnet;
18  
19  import java.io.IOException;
20  import java.io.InputStream;
21  import java.io.OutputStream;
22  import java.net.ServerSocket;
23  import java.net.Socket;
24  
25  import org.apache.commons.io.IOUtils;
26  
27  /**
28   * Simple TCP server. Waits for connections on a TCP port in a separate thread.
29   */
30  public class TelnetTestSimpleServer implements Runnable {
31      ServerSocket serverSocket;
32      Socket clientSocket;
33      Thread listener;
34  
35      /*
36       * test of client-driven subnegotiation. <p>
37       *
38       * @param port   server port on which to listen.
39       * @throws IOException on error
40       */
41      public TelnetTestSimpleServer(final int port) throws IOException {
42          serverSocket = new ServerSocket(port);
43  
44          listener = new Thread(this);
45  
46          listener.start();
47      }
48  
49      public void disconnect() {
50          if (clientSocket == null) {
51              return;
52          }
53          synchronized (clientSocket) {
54              try {
55                  clientSocket.notify();
56              } catch (final Exception e) {
57                  System.err.println("Exception in notify, " + e.getMessage());
58              }
59          }
60      }
61  
62      public InputStream getInputStream() throws IOException {
63          if (clientSocket != null) {
64              return clientSocket.getInputStream();
65          }
66          return null;
67      }
68  
69      public OutputStream getOutputStream() throws IOException {
70          if (clientSocket != null) {
71              return clientSocket.getOutputStream();
72          }
73          return null;
74      }
75  
76      @Override
77      public void run() {
78          boolean bError = false;
79          while (!bError) {
80              try {
81                  clientSocket = serverSocket.accept();
82                  synchronized (clientSocket) {
83                      try {
84                          clientSocket.wait();
85                      } catch (final Exception e) {
86                          System.err.println("Exception in wait, " + e.getMessage());
87                      }
88                      IOUtils.close(clientSocket, e -> System.err.println("Exception in close, " + e.getMessage()));
89                  }
90              } catch (final IOException e) {
91                  bError = true;
92              }
93          }
94          IOUtils.closeQuietly(serverSocket, e -> System.err.println("Exception in close, " + e.getMessage()));
95      }
96  
97      public void stop() {
98          listener.interrupt();
99          IOUtils.closeQuietly(serverSocket, e -> System.err.println("Exception in close, " + e.getMessage()));
100     }
101 }