KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > net > telnet > TelnetTestSimpleServer


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 JavaDoc;
19 import java.net.Socket JavaDoc;
20 import java.io.InputStream JavaDoc;
21 import java.io.OutputStream JavaDoc;
22 import java.io.IOException JavaDoc;
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 JavaDoc
31 {
32     ServerSocket JavaDoc serverSocket = null;
33     Socket JavaDoc clientSocket = null;
34     Thread JavaDoc 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 JavaDoc
42     {
43         serverSocket = new ServerSocket JavaDoc(port);
44
45         listener = new Thread JavaDoc (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 JavaDoc e)
68                     {
69                         System.err.println("Exception in wait, "+ e.getMessage());
70                     }
71                     try
72                     {
73                         clientSocket.close();
74                     }
75                     catch (Exception JavaDoc e)
76                     {
77                         System.err.println("Exception in close, "+ e.getMessage());
78                     }
79                 }
80             }
81             catch (IOException JavaDoc e)
82             {
83                 bError = true;
84             }
85         }
86
87         try
88         {
89             serverSocket.close();
90         }
91         catch (Exception JavaDoc 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 JavaDoc 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 JavaDoc 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 JavaDoc getInputStream() throws IOException JavaDoc
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 JavaDoc getOutputStream() throws IOException JavaDoc
151     {
152         if(clientSocket != null)
153         {
154             return(clientSocket.getOutputStream());
155         }
156         else
157         {
158             return(null);
159         }
160     }
161 }
162
Popular Tags