/** To change this template, choose tools | templates * and open the template in the editor. */package net. programe. ten; import COM. macfaq. io. *; import Java. io. *; import java.net. *;/***** @ author Arthur */public class clienttester {public static void main (string [] ARGs) {int Port = 80; socket conn = NULL; try {// create a server with port number and queue length of 1 and set it to 1. this server can only receive one connection to serversocket Server = new serversocket (P ORT, 1); system. out. println ("On port" + server. getlocalport () + "listener"); While (true) {// start to receive client connection requests and put the conn returned by each connection into the thread s conn = server. accept (); try {system. out. println ("starting at" + conn + "connection"); // used to read the message thread input = new inputthread (Conn. getinputstream (); input. start (); // used to send a message to the client thread out = new outputthread (Conn. getoutputstream (); out. start (); // Add the main thread to wait for the input and output to end try {input. join (); out. join ();} Catch (interruptedexception ex) {ex. printstacktrace () ;}} catch (ioexception ex) {ex. printstacktrace () ;}// end while} catch (ioexception ex) {ex. printstacktrace ();} finally {If (Conn! = NULL) {try {Conn. close ();} catch (ioexception ex) {ex. printstacktrace () ;}}// end finally} // end main}/*** defines an input stream thread, used to read messages sent from a client * @ author Arthur */class inputthread extends thread {inputstream in; Public inputthread (inputstream in) {This. in = in ;}@ override public void run () {try {While (true) {// read one byte at a time int I = in. read (); // If read, jump out of the loop if (I =-1) {break;} system. out. println (I );} // End while} catch (socketexception ex) {ex. printstacktrace ();} catch (ioexception ex) {system. err. println (Ex);} Try {In. close ();} catch (ioexception ex) {}} // end run}/*** defines an output stream thread, used to send messages to a client * @ author Arthur */class outputthread extends thread {private writer out; Public outputthread (outputstream out) {This. out = new outputstreamwriter (out) ;}@ override public void run () {// used String line entered by the storage console; // obtain data from the console bufferedreader in = new safebufferedreader (New inputstreamreader (system. in); While (true) {try {line = in. readline (); // end the loop with a question mark if (line. equals ("? ") {Break;} // sends the console data out. write (LINE + "\ r \ n"); // refresh out. flush ();} catch (ioexception ex) {ex. printstacktrace () ;}// end while try {out. close ();} catch (ioexception ex) {}} // end run}