    1   /*
    2    * Copyright 2003-2004 The Apache Software Foundation
    3    *
    4    * Licensed under the Apache License, Version 2.0 (the "License");
    5    * you may not use this file except in compliance with the License.
    6    * You may obtain a copy of the License at
    7    *
    8    *     http://www.apache.org/licenses/LICENSE-2.0
    9    *
    10   * Unless required by applicable law or agreed to in writing, software
    11   * distributed under the License is distributed on an "AS IS" BASIS,
    12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13   * See the License for the specific language governing permissions and
    14   * limitations under the License.
    15   */
    16  package org.apache.commons.net.telnet;
    17
    18  import java.net.ServerSocket;
    19  import java.net.Socket;
    20  import java.io.InputStream;
    21  import java.io.OutputStream;
    22  import java.io.IOException;
    23
    24  /****
    25   * Simple TCP server.
    26   * Waits for connections on a TCP port in a separate thread.
    27   * <p>
    28   * @author Bruno D'Avanzo
    29   ***/
    30  public class TelnetTestSimpleServer implements Runnable
    31  {
    32      ServerSocket serverSocket = null;
    33      Socket clientSocket = null;
    34      Thread listener = null;
    35
    36      /****
    37       * test of client-driven subnegotiation.
    38       * <p>
    39       * @param port - server port on which to listen.
    40       ***/
    41      public TelnetTestSimpleServer(int port) throws IOException
    42      {
    43          serverSocket = new ServerSocket(port);
    44
    45          listener = new Thread (this);
    46
    47          listener.start();
    48      }
    49
    50      /****
    51       * Run for the thread. Waits for new connections
    52       ***/
    53      public void run()
    54      {
    55          boolean bError = false;
    56          while(!bError)
    57          {
    58              try
    59              {
    60                  clientSocket = serverSocket.accept();
    61                  synchronized (clientSocket)
    62                  {
    63                      try
    64                      {
    65                          clientSocket.wait();
    66                      }
    67                      catch (Exception e)
    68                      {
    69                          System.err.println("Exception in wait, "+ e.getMessage());
    70                      }
    71                      try
    72                      {
    73                          clientSocket.close();
    74                      }
    75                      catch (Exception e)
    76                      {
    77                          System.err.println("Exception in close, "+ e.getMessage());
    78                      }
    79                  }
    80              }
    81              catch (IOException e)
    82              {
    83                  bError = true;
    84              }
    85          }
    86
    87          try
    88          {
    89              serverSocket.close();
    90          }
    91          catch (Exception e)
    92          {
    93              System.err.println("Exception in close, "+ e.getMessage());
    94          }
    95      }
    96
    97
    98      /****
    99       * Disconnects the client socket
    100      ***/
    101     public void disconnect()
    102     {
    103         synchronized (clientSocket)
    104         {
    105             try
    106             {
    107                 clientSocket.notify();
    108             }
    109             catch (Exception e)
    110             {
    111                 System.err.println("Exception in notify, "+ e.getMessage());
    112             }
    113         }
    114     }
    115
    116     /****
    117      * Stop the listener thread
    118      ***/
    119     public void stop()
    120     {
    121         listener.interrupt();
    122         try
    123         {
    124             serverSocket.close();
    125         }
    126         catch (Exception e)
    127         {
    128             System.err.println("Exception in close, "+ e.getMessage());
    129         }
    130     }
    131
    132     /****
    133      * Gets the input stream for the client socket
    134      ***/
    135     public InputStream getInputStream() throws IOException
    136     {
    137         if(clientSocket != null)
    138         {
    139             return(clientSocket.getInputStream());
    140         }
    141         else
    142         {
    143             return(null);
    144         }
    145     }
    146
    147     /****
    148      * Gets the output stream for the client socket
    149      ***/
    150     public OutputStream getOutputStream() throws IOException
    151     {
    152         if(clientSocket != null)
    153         {
    154             return(clientSocket.getOutputStream());
    155         }
    156         else
    157         {
    158             return(null);
    159         }
    160     }
    161 }