import java.io.*; import java.nio.*; import java.nio.channels.*; import java.net.*; class OneConnection_D extends OneConnection_C { OneConnection_D(Socket sock) throws Exception { super(sock); } void sendThruChannel(String fname) throws Exception { File f = new File(fname); FileInputStream fin = new FileInputStream(f); int len = (int) f.length(); FileChannel fc = fin.getChannel(); System.out.println("allocating buff"); ByteBuffer myBB = ByteBuffer.allocate(len); int bytesRead = fc.read(myBB); myBB.flip(); System.out.println("getting sock channel"); SocketChannel sc = sock.getChannel(); int bytesWritten = sc.write(myBB); System.out.println("wrote "+bytesWritten+" bytes into sock channel"); sc.close(); System.out.println("closed sock channel"); } } public class HTTPServer5 { public static void main(String a[]) throws Exception { final int httpd = 80; ServerSocketChannel ssc = ServerSocketChannel.open(); InetSocketAddress isa = new InetSocketAddress("127.0.0.1", httpd); ssc.socket().bind(isa); System.out.println("have opened port 80 locally, " + ssc.socket() ); System.out.println("waiting for accept"); SocketChannel sockch = ssc.accept(); System.out.println("client has made socket connection"); System.out.flush(); Socket sock = sockch.socket(); System.out.println("obtained socket from socket channel "); OneConnection_D client = new OneConnection_D(sock); String filename = client.getRequest(); client.sendThruChannel("/tmp/"+filename); } }