package j2; // ReadNet.java // ネットワーク上のサーバからデータを受け取り,そのまま画面に出力します // 使い方java ReadNet DNS 名ポート番号 // 例java ReadNet kiku.fuis.fukui-u.ac.jp 6000 //ライブラリの利用 import java.io.BufferedReader; import java.io.InputStream; import java.io.InputStreamReader; import java.net.Socket; // Readnetクラス public class ReadNet { // プログラムの本体main public static void main(String[] args){ byte[] buff = new byte[1024];//配列の定義 Socket csock = null ;// サーバ接続用ソケット BufferedReader reader = null; InputStream instr = null;// データ読み取り用オブジェクト boolean cont = true ; // 指定のポートに対して,ソケットを作成します // オブジェクトinstrを作り,データ読み出しを準備します try{ // readsocket = new Socket(args[0], Integer.parseInt(args[1])) ; csock = new Socket("127.0.0.1", 5555) ; reader = new BufferedReader(new InputStreamReader(csock.getInputStream())); } catch(Exception e){ System.err.println("ネットワークエラーです") ; System.exit(1) ; } // データの終了まで,以下のループを繰り返します String line = null; // while (true) { try { line = reader.readLine(); // if (line != null) { System.out.println(line); // } } // System.out.println(reader.readLine()); // // 読み込み // int n = instr.read(buff); // // System.outへの書き出し // System.out.write(buff, 0, n) ; // 以下は例外処理です catch(Exception e){ // 読み出し終了時にループも終了します cont = false ; } // コネクションを閉じます try{ reader.close() ; } catch(Exception e){ // ネットワーククローズ失敗です System.err.println("ネットワークのエラーです") ; System.exit(1) ; } } }