diff --git a/src/j2/EchoServer.java b/src/j2/EchoServer.java index 543efee..1c3a1bb 100644 --- a/src/j2/EchoServer.java +++ b/src/j2/EchoServer.java @@ -13,11 +13,12 @@ if (args.length < 2) { args = new String[2]; args[0] = "127.0.0.1"; - args[1] = "9999"; // ポート9999番を開く + args[1] = "9999"; // ポート9999番を開く } new EchoServer(args[0], args[1]); } - public EchoServer(String bindaddress, String strport){ + + public EchoServer(String bindaddress, String strport) { // ソケットや入出力用のストリームの宣言 ServerSocket echoServer = null; Socket clientSocket = null; @@ -28,15 +29,15 @@ int port = Integer.parseInt(strport); try { echoServer = new ServerSocket(); - // クライアントからの要求を受けるソケットを開く - echoServer.bind(new InetSocketAddress(bindaddress, port)); // サーバソケットにバインドする=ポートでの待ち受け開始 + // クライアントからの要求を受けるソケットを開く + echoServer.bind(new InetSocketAddress(bindaddress, port)); // サーバソケットにバインドする=ポートでの待ち受け開始 } catch (IOException e) { System.err.println(e); - System.err.println("バインド失敗。ポート番号 "+port+" (Bind-address: "+bindaddress+" 向け) がすでにつかわれているようです"); + System.err.println("バインド失敗。ポート番号 " + port + " (Bind-address: " + bindaddress + " 向け) がすでにつかわれているようです"); System.err.println("ポート番号またはバインドアドレスを変更してください。"); System.exit(1); } - System.out.println("EchoServer ["+bindaddress+":"+port+"] started."); + System.out.println("EchoServer [" + bindaddress + ":" + port + "] started."); System.out.println("停止するには CTRL+C をおしてください"); // サーバ側の処理の繰り返し diff --git a/src/j3/Phttpd.java b/src/j3/Phttpd.java index 4cfa9e5..f429199 100644 --- a/src/j3/Phttpd.java +++ b/src/j3/Phttpd.java @@ -1,94 +1,95 @@ -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[] { "src/j3/index.html" }; // 相対パスで指定。windowsの場合、user.dir=NWP - // args = new String[] { "j3/index.html" };//Linuxの場合、通常はcd NWP/src しているはず - System.out.println("このサーバは" + args[0] + "を返します。"); - System.out.println(System.getProperty("user.dir")); - 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 - } - } -} +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[] { "src/j3/index.html" }; // 相対パスで指定。windowsの場合、user.dir=NWP + args = new String[] { "j3/index.html" };//Linuxの場合、通常はcd NWP/src しているはず + System.out.println("このサーバは" + args[0] + "を返します。"); + System.out.println(System.getProperty("user.dir")); + 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 + } + } +} + diff --git a/src/j3/Thttpd.java b/src/j3/Thttpd.java index 6471935..4e7e6cc 100644 --- a/src/j3/Thttpd.java +++ b/src/j3/Thttpd.java @@ -28,13 +28,15 @@ ServerSocket servsock = null; Socket sock; + System.out.println(args.length); - if (args.length == 0) // 引数がない場合、以下のファイルを返すことにする - args = new String[] { "src/j3/index.html", "8000" }; // 相対パスで指定。windowsの場合、user.dir=NWP - // args = new String[] { "j3/index.html" ,"8000"};//Linuxの場合、通常はcd NWP/srcしているはず + if (args.length == 0 || args[0].length() < 1) { // 引数がない場合、以下のファイルを返すことにする + args = new String[] { "j3/index.html", "8000" };// Linuxの場合、通常はcd NWP/srcしているはず + // args = new String[] { "src/j3/index.html", "8000" }; // + // windowsの場合、user.dir=NWP + } int portnum = Integer.parseInt(args[1]); - System.out.println("このサーバは" + args[0] + "を返します。ポート番号は "+portnum+" です。"); - // System.out.println(System.getProperty("user.dir")); + System.out.println("このサーバは" + args[0] + "を返します。ポート番号は " + portnum + " です。"); showUrls(portnum); try { @@ -51,9 +53,9 @@ } /** - * ネットワークインタフェースを調べて、10.104ではじまるIPアドレスについて表示する + * ネットワークインタフェースを調べて、接続可能なIPアドレスについて表示する */ - public static void showUrls(int pnum){ + public static void showUrls(int pnum) { Enumeration interfaces; try { interfaces = NetworkInterface.getNetworkInterfaces(); @@ -64,11 +66,11 @@ InetAddress i = (InetAddress) addresses.nextElement(); String ip = i.getHostAddress(); String[] seg = ip.split("\\."); // .で文字列分割 - if (seg.length==4) //分割したセグメントが4つあればIPv4 - System.out.println("http://"+ip+":"+pnum); + if (seg.length == 4) // 分割したセグメントが4つあればIPv4 + System.out.println("http://" + ip + ":" + pnum); } } - } catch (SocketException e1) { + } catch (SocketException e1) { e1.printStackTrace(); }