import java.net.*; import java.io.* ; class LineChild extends Thread { Socket RemoteHostConnect ; SynchCount count ; PrintStream logOut ; String logPrefix ; public LineChild( Socket s, SynchCount c, PrintStream p) { RemoteHostConnect = s ; count = c ; logOut = p ; } // It's not the greatest format, // but it's what I'm using in the C server private String makeLogPrefix () { String p ; int i ; p = RemoteHostConnect.getInetAddress().toString() ; i = p.indexOf((int) '/') ; if (i != -1 ) p = p.substring(0, i) ; p = p + " (" + Integer.toString(RemoteHostConnect.getPort()) + ")" + " " ; p = p.substring(0, 38) + " -- " ; return(p) ; } private String makeLogString(String s) { String t ; int i = s.length() ; if (i>20) t = s.substring(i-20) ; else t = s ; return ("[" + t + "]") ; } private void printLogRecord(String s) { logOut.println( logPrefix + s) ; logOut.flush() ; } public void run() { BufferedReader RemoteInput ; PrintWriter RemoteOutput ; logPrefix = makeLogPrefix() ; printLogRecord("connected (" + getName() + ")") ; try { RemoteInput = new BufferedReader( new InputStreamReader( RemoteHostConnect.getInputStream())) ; RemoteOutput = new PrintWriter(RemoteHostConnect.getOutputStream(),true); } catch ( IOException ioe ) { printLogRecord("Cannot create I/O streams" + ioe.toString()) ; return ; } try { String inString = RemoteInput.readLine() ; while (inString != null) { printLogRecord(makeLogString(inString)) ; RemoteOutput.println(count.Increment()) ; inString = RemoteInput.readLine() ; } } catch ( IOException ioe ) { printLogRecord("socket exception: " + ioe.toString()) ; return ; } printLogRecord("socket closed (" + getName() + ")") ; } }