1 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 30 public class TelnetTestSimpleServer implements Runnable 31 { 32 ServerSocket serverSocket = null; 33 Socket clientSocket = null; 34 Thread listener = null; 35 36 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 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 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 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 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 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 } 162 | Popular Tags |