1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
27
28
29
30
31 public class TelnetTestSimpleServer implements Runnable
32 {
33 ServerSocket serverSocket = null;
34 Socket clientSocket = null;
35 Thread listener = null;
36
37
38
39
40
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
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
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
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
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
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 }