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    *      http://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.net.ServerSocket;
20  import java.net.Socket;
21  import java.io.InputStream;
22  import java.io.OutputStream;
23  import java.io.IOException;
24  
25  /***
26   * Simple TCP server.
27   * Waits for connections on a TCP port in a separate thread.
28   * <p>
29   * @author Bruno D'Avanzo
30   ***/
31  public class TelnetTestSimpleServer implements Runnable
32  {
33      ServerSocket serverSocket = null;
34      Socket clientSocket = null;
35      Thread listener = null;
36  
37      /***
38       * test of client-driven subnegotiation.
39       * <p>
40       * @param port - server port on which to listen.
41       ***/
42      public TelnetTestSimpleServer(int port) throws IOException
43      {
44          serverSocket = new ServerSocket(port);
45  
46          listener = new Thread (this);
47  
48          listener.start();
49      }
50  
51      /***
52       * Run for the thread. Waits for new connections
53       ***/
54      public void run()
55      {
56          boolean bError = false;
57          while(!bError)
58          {
59              try
60              {
61                  clientSocket = serverSocket.accept();
62                  synchronized (clientSocket)
63                  {
64                      try
65                      {
66                          clientSocket.wait();
67                      }
68                      catch (Exception e)
69                      {
70                          System.err.println("Exception in wait, "+ e.getMessage());
71                      }
72                      try
73                      {
74                          clientSocket.close();
75                      }
76                      catch (Exception e)
77                      {
78                          System.err.println("Exception in close, "+ e.getMessage());
79                      }
80                  }
81              }
82              catch (IOException e)
83              {
84                  bError = true;
85              }
86          }
87  
88          try
89          {
90              serverSocket.close();
91          }
92          catch (Exception e)
93          {
94              System.err.println("Exception in close, "+ e.getMessage());
95          }
96      }
97  
98  
99      /***
100      * Disconnects the client socket
101      ***/
102     public void disconnect()
103     {
104         synchronized (clientSocket)
105         {
106             try
107             {
108                 clientSocket.notify();
109             }
110             catch (Exception e)
111             {
112                 System.err.println("Exception in notify, "+ e.getMessage());
113             }
114         }
115     }
116 
117     /***
118      * Stop the listener thread
119      ***/
120     public void stop()
121     {
122         listener.interrupt();
123         try
124         {
125             serverSocket.close();
126         }
127         catch (Exception e)
128         {
129             System.err.println("Exception in close, "+ e.getMessage());
130         }
131     }
132 
133     /***
134      * Gets the input stream for the client socket
135      ***/
136     public InputStream getInputStream() throws IOException
137     {
138         if(clientSocket != null)
139         {
140             return(clientSocket.getInputStream());
141         }
142         else
143         {
144             return(null);
145         }
146     }
147 
148     /***
149      * Gets the output stream for the client socket
150      ***/
151     public OutputStream getOutputStream() throws IOException
152     {
153         if(clientSocket != null)
154         {
155             return(clientSocket.getOutputStream());
156         }
157         else
158         {
159             return(null);
160         }
161     }
162 }