001 /* 002 * Licensed to the Apache Software Foundation (ASF) under one or more 003 * contributor license agreements. See the NOTICE file distributed with 004 * this work for additional information regarding copyright ownership. 005 * The ASF licenses this file to You under the Apache License, Version 2.0 006 * (the "License"); you may not use this file except in compliance with 007 * the License. You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017 package org.apache.commons.net.telnet; 018 019 import java.net.ServerSocket; 020 import java.net.Socket; 021 import java.io.InputStream; 022 import java.io.OutputStream; 023 import java.io.IOException; 024 025 /*** 026 * Simple TCP server. 027 * Waits for connections on a TCP port in a separate thread. 028 * <p> 029 * @author Bruno D'Avanzo 030 ***/ 031 public class TelnetTestSimpleServer implements Runnable 032 { 033 ServerSocket serverSocket = null; 034 Socket clientSocket = null; 035 Thread listener = null; 036 037 /*** 038 * test of client-driven subnegotiation. 039 * <p> 040 * @param port - server port on which to listen. 041 ***/ 042 public TelnetTestSimpleServer(int port) throws IOException 043 { 044 serverSocket = new ServerSocket(port); 045 046 listener = new Thread (this); 047 048 listener.start(); 049 } 050 051 /*** 052 * Run for the thread. Waits for new connections 053 ***/ 054 public void run() 055 { 056 boolean bError = false; 057 while(!bError) 058 { 059 try 060 { 061 clientSocket = serverSocket.accept(); 062 synchronized (clientSocket) 063 { 064 try 065 { 066 clientSocket.wait(); 067 } 068 catch (Exception e) 069 { 070 System.err.println("Exception in wait, "+ e.getMessage()); 071 } 072 try 073 { 074 clientSocket.close(); 075 } 076 catch (Exception e) 077 { 078 System.err.println("Exception in close, "+ e.getMessage()); 079 } 080 } 081 } 082 catch (IOException e) 083 { 084 bError = true; 085 } 086 } 087 088 try 089 { 090 serverSocket.close(); 091 } 092 catch (Exception e) 093 { 094 System.err.println("Exception in close, "+ e.getMessage()); 095 } 096 } 097 098 099 /*** 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 }