| 1 package com.quadcap.net.server; 2 3 40 41 import java.io.ByteArrayOutputStream ; 42 import java.io.File ; 43 import java.io.FileInputStream ; 44 import java.io.InputStream ; 45 import java.io.OutputStream ; 46 import java.io.IOException ; 47 import java.io.PrintStream ; 48 49 import java.net.*; 50 51 import com.quadcap.io.IO; 52 53 import com.quadcap.util.Debug; 54 55 60 public class SimpleWorker extends Worker { 61 byte[] response; 62 byte[] request; 63 64 public void init(Server server, Object context) { 65 File f = new File ("d:/butler/java/com/quadcap/http/server22a/index.html"); 66 super.init(server, context); 67 ByteArrayOutputStream bos = new ByteArrayOutputStream (); 68 PrintStream ps = new PrintStream (bos); 69 ps.println("Mime-Version: 1.0"); 70 ps.println("Server: Quadcap Web Server 1.0dev"); 71 ps.println("Content-Type: text/html"); 72 ps.println("Content-Length: " + f.length()); 73 ps.println(""); 74 ps.flush(); 75 try { 76 FileInputStream fis = new FileInputStream (f); 77 IO.copyStream(fis, bos); 78 } catch (IOException e) { 79 } 80 81 response = bos.toByteArray(); 82 request = new byte[4096]; 83 } 84 85 public void doSession() throws Exception { 86 int cnt = win.read(request, 0, 28); 87 wout.write(response); 88 } 89 90 public static void main(String args[]) { 91 try { 92 SimpleWorker w = new SimpleWorker(); 93 w.init(null, null); 94 final byte[] request = w.request; 95 final byte[] response = w.response; 96 final ServerSocket socket = new ServerSocket(80, 64); 97 while (true) { 98 Socket s = socket.accept(); 99 InputStream in = s.getInputStream(); 100 OutputStream out = s.getOutputStream(); 101 in.read(request, 0, 28); 102 out.write(response); 103 s.close(); 104 } 105 } catch (Throwable t) { 106 } 107 } 108 } 109 | Popular Tags |