/* * [EchoServer.java] * * Summary: a mindlessly simple HTTP server. * * Copyright: (c) 2004-2017 Roedy Green, Canadian Mind Products, http://mindprod.com * * Licence: This software may be copied and used freely for any purpose but military. * http://mindprod.com/contact/nonmil.html * * Requires: JDK 1.8+ * * Created with: JetBrains IntelliJ IDEA IDE http://www.jetbrains.com/idea/ * * Version History: * 1.0 2004-04-03 initial version * 1.1 2005-01-01 copyright update * 1.2 2006-01-01 copyright update * 1.3 2006-03-06 improve docs. */ package com.mindprod.echoserver; import java.io.IOException; import java.io.InputStream; import java.net.ServerSocket; import java.net.Socket; import static java.lang.System.*; /** * a mindlessly simple HTTP server. *

* It just dumps what it receives on the server console. It does not * even echo back to the sender in an HTML sandwich. Send things to it with http://localhost:808/xxxx May be use freely * * @author Roedy Green, Canadian Mind Products * @version 1.3 2006-03-06 improve docs. * @since 2004-04-03 */ public final class EchoServer { /** * How to display Cr control characters. Set to 15 to dislay as Crs. 3=heart in IBMOEM font. */ private static final char SHOW_CR_AS = 3; private static final int FIRST_COPYRIGHT_YEAR = 2004; /** * Port the server expects data on. */ private static final int PORT = 8081; /** * undisplayed copyright notice */ private static final String EMBEDDED_COPYRIGHT = "Copyright: (c) 2004-2017 Roedy Green, Canadian Mind Products, http://mindprod.com"; private static final String RELEASE_DATE = "2006-03-06"; /** * embedded version string. */ private static final String VERSION_STRING = "1.3"; /** * main runs until you Ctrl-C kill it echoing messages that it receives on the port. * * @param args not used. */ public static void main( String[] args ) { int streamCount = 0; try { out.println( "EchoServer waiting on port " + PORT + " ..." ); ServerSocket echo = new ServerSocket( PORT ); while ( true ) { Socket socket = echo.accept(); ( new EchoClientThread( ++streamCount, socket ) ).start(); } } catch ( IOException e ) { err.println( e ); System.exit( 1 ); } } /** * Inner class, one per stream. *

* created with Intellij Idea * * @author Roedy Green, Canadian Mind Products */ static final class EchoClientThread extends Thread { private final Socket socket; private final int id; /** * Contructor * * @param id Unique number to identify this thread. * @param socket opened Socket no receive on. */ public EchoClientThread( int id, Socket socket ) { this.id = id; this.socket = socket; } /** * Thread that reads the stream and closes the socket for one message. */ public void run() { out.println( ">>starting socket " + id ); try { InputStream in = socket.getInputStream(); int c; while ( ( c = in.read() ) >= 0 ) { if ( c == '\r' ) { c = SHOW_CR_AS/* show Cr as heart in IBMOEM font */; } // echo to console out.print( ( char ) c ); } out.println( ">>closing socket " + id ); in.close(); socket.close(); } catch ( IOException e ) { err.println( e.getMessage() ); } } } }