package j3; // いんちきHTTPサーバPhttpd.java (pseudo-HTTP-Daemon) pseudoの発音はスード // このプログラムはポート番号8000番で動作するサーバです // 使い方: java j3.Phttpd ファイル名 // WWWクライアントからの接続に対して、引数で指定したファイルを返します。 // ライブラリの利用 import java.io.BufferedReader; import java.io.DataOutputStream; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStreamReader; import java.net.ServerSocket; import java.net.Socket; // Phttpdクラス public class Phttpd { public static void main(String args[]) { // サーバソケット ServerSocket servsock = null; Socket sock; // 入出力 DataOutputStream dostr; BufferedReader in; FileInputStream infile = null; byte[] buff = new byte[1024]; // その他 boolean cont = true; if (args.length==0) //引数がない場合、以下のファイルを返すことにする args = new String[] { "j3/index.html" }; System.out.println("このサーバは"+args[0]+"を返します。"); try { // サーバ用ソケットの作成(ポート番号8000番) servsock = new ServerSocket(8000); while (true) { sock = servsock.accept();// 接続要求の受付 // 接続先の表示 System.out.println("---\nConnection Requst from: " + (sock.getInetAddress())); // オブジェクトinfileを作り,ファイルを準備します try { infile = new FileInputStream(args[0]); } catch (Exception e) { // ファイル準備の失敗 System.err.println("ファイルがありません"); System.exit(1); } // 読み書き用オブジェクトの生成 in = new BufferedReader(new InputStreamReader(sock.getInputStream())); dostr = new DataOutputStream(sock.getOutputStream()); // read headers StringBuffer request = new StringBuffer(); String line; while ((line = in.readLine()) != null) { System.out.println(line); request.append(line + "\r\n"); if (line.length() < 1) break; } // Response Headerの出力 String CRLF = "\r\n"; String response = "HTTP/1.1 200" + CRLF + "Content-type: text/html; charset=UTF-8" + CRLF + CRLF; dostr.write(response.getBytes()); // Response Bodyの出力 cont = true; while (cont) { // ファイルからの読み込みとネットワーク出力 try { int n = infile.read(buff); dostr.write(buff, 0, n); } catch (Exception e) { // end of file cont = false; } } // おまけ:レスポンスヘッダを表示 dostr.write(request.toString().getBytes()); // 接続終了 sock.close(); infile.close(); } } catch (IOException e) { System.out.println("異常終了:ポート番号の重複の可能性あり"); System.exit(1); // 異常終了は1 } } }