001: /*
002: * Copyright 2003-2004 The Apache Software Foundation
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package org.apache.commons.net.telnet;
017:
018: import java.net.ServerSocket;
019: import java.net.Socket;
020: import java.io.InputStream;
021: import java.io.OutputStream;
022: import java.io.IOException;
023:
024: /***
025: * Simple TCP server.
026: * Waits for connections on a TCP port in a separate thread.
027: * <p>
028: * @author Bruno D'Avanzo
029: ***/
030: public class TelnetTestSimpleServer implements Runnable {
031: ServerSocket serverSocket = null;
032: Socket clientSocket = null;
033: Thread listener = null;
034:
035: /***
036: * test of client-driven subnegotiation.
037: * <p>
038: * @param port - server port on which to listen.
039: ***/
040: public TelnetTestSimpleServer(int port) throws IOException {
041: serverSocket = new ServerSocket(port);
042:
043: listener = new Thread(this );
044:
045: listener.start();
046: }
047:
048: /***
049: * Run for the thread. Waits for new connections
050: ***/
051: public void run() {
052: boolean bError = false;
053: while (!bError) {
054: try {
055: clientSocket = serverSocket.accept();
056: synchronized (clientSocket) {
057: try {
058: clientSocket.wait();
059: } catch (Exception e) {
060: System.err.println("Exception in wait, "
061: + e.getMessage());
062: }
063: try {
064: clientSocket.close();
065: } catch (Exception e) {
066: System.err.println("Exception in close, "
067: + e.getMessage());
068: }
069: }
070: } catch (IOException e) {
071: bError = true;
072: }
073: }
074:
075: try {
076: serverSocket.close();
077: } catch (Exception e) {
078: System.err.println("Exception in close, " + e.getMessage());
079: }
080: }
081:
082: /***
083: * Disconnects the client socket
084: ***/
085: public void disconnect() {
086: synchronized (clientSocket) {
087: try {
088: clientSocket.notify();
089: } catch (Exception e) {
090: System.err.println("Exception in notify, "
091: + e.getMessage());
092: }
093: }
094: }
095:
096: /***
097: * Stop the listener thread
098: ***/
099: public void stop() {
100: listener.interrupt();
101: try {
102: serverSocket.close();
103: } catch (Exception e) {
104: System.err.println("Exception in close, " + e.getMessage());
105: }
106: }
107:
108: /***
109: * Gets the input stream for the client socket
110: ***/
111: public InputStream getInputStream() throws IOException {
112: if (clientSocket != null) {
113: return (clientSocket.getInputStream());
114: } else {
115: return (null);
116: }
117: }
118:
119: /***
120: * Gets the output stream for the client socket
121: ***/
122: public OutputStream getOutputStream() throws IOException {
123: if (clientSocket != null) {
124: return (clientSocket.getOutputStream());
125: } else {
126: return (null);
127: }
128: }
129: }
|