DiagServer.java examples7/ 1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34: 35: 36: 37: 38: 39: 40: 41: 42: 43: 44: 45:
import java.io.*; import java.net.*; import sun.net.NetworkServer; /** * A program that echoes to standard output an HTTP * request, whether GET or POST. * * Usage: java DiagServer [port] * * If you spawn the server on a port already in use, you will be informed * "Server failed to start: java.net.BindException: Permission denied". * Note that the server’s default choice of ports is 80. * * Excerpted from * http://hotwired.lycos.com/webmonkey/99/36/index3a_page4.html?tw=backend. */ public class DiagServer extends NetworkServer { public static void main(String[] args) { int port = 80; try { port = Integer.parseInt(args[0]); } catch (Exception e) { //You might want to catch a NumberFormatException here, //or do some more sophisticated command line parsing. } try { DiagServer ds = new DiagServer(); ds.startServer(port); System.err.println("DiagServer started on port " + port); System.err.println(""); } catch (IOException ioe) { System.err.println("Server failed to start: " + ioe); } } //main public void serviceRequest() throws IOException { int byteCount = 0; while (clientIsOpen() && (byteCount <1000)) { byte b = (byte) clientInput.read();
1/2
DiagServer.java examples7/ 46: byteCount++; 47: System.out.write(b); 48: System.out.flush(); 49: } 50: } //serviceRequest 51: }
2/2
get.html
1/1
examples7/ 1: 2: .dtd"> 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25:
CSCI E-259