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    *      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  //    @Override
55      public void run()
56      {
57          boolean bError = false;
58          while(!bError)
59          {
60              try
61              {
62                  clientSocket = serverSocket.accept();
63                  synchronized (clientSocket)
64                  {
65                      try
66                      {
67                          clientSocket.wait();
68                      }
69                      catch (Exception e)
70                      {
71                          System.err.println("Exception in wait, "+ e.getMessage());
72                      }
73                      try
74                      {
75                          clientSocket.close();
76                      }
77                      catch (Exception e)
78                      {
79                          System.err.println("Exception in close, "+ e.getMessage());
80                      }
81                  }
82              }
83              catch (IOException e)
84              {
85                  bError = true;
86              }
87          }
88  
89          try
90          {
91              serverSocket.close();
92          }
93          catch (Exception e)
94          {
95              System.err.println("Exception in close, "+ e.getMessage());
96          }
97      }
98  
99  
100     /***
101      * Disconnects the client socket
102      ***/
103     public void disconnect()
104     {
105         if (clientSocket == null) {
106             return;
107         }
108         synchronized (clientSocket)
109         {
110             try
111             {
112                 clientSocket.notify();
113             }
114             catch (Exception e)
115             {
116                 System.err.println("Exception in notify, "+ e.getMessage());
117             }
118         }
119     }
120 
121     /***
122      * Stop the listener thread
123      ***/
124     public void stop()
125     {
126         listener.interrupt();
127         try
128         {
129             serverSocket.close();
130         }
131         catch (Exception e)
132         {
133             System.err.println("Exception in close, "+ e.getMessage());
134         }
135     }
136 
137     /***
138      * Gets the input stream for the client socket
139      ***/
140     public InputStream getInputStream() throws IOException
141     {
142         if(clientSocket != null)
143         {
144             return(clientSocket.getInputStream());
145         }
146         else
147         {
148             return(null);
149         }
150     }
151 
152     /***
153      * Gets the output stream for the client socket
154      ***/
155     public OutputStream getOutputStream() throws IOException
156     {
157         if(clientSocket != null)
158         {
159             return(clientSocket.getOutputStream());
160         }
161         else
162         {
163             return(null);
164         }
165     }
166 }