diff --git a/.classpath b/.classpath new file mode 100644 index 0000000..d54800d --- /dev/null +++ b/.classpath @@ -0,0 +1,10 @@ + + + + + + + + + + diff --git a/.project b/.project new file mode 100644 index 0000000..da2e28f --- /dev/null +++ b/.project @@ -0,0 +1,17 @@ + + + NWP + + + + + + org.eclipse.jdt.core.javabuilder + + + + + + org.eclipse.jdt.core.javanature + + diff --git a/.settings/org.eclipse.jdt.core.prefs b/.settings/org.eclipse.jdt.core.prefs new file mode 100644 index 0000000..e256158 --- /dev/null +++ b/.settings/org.eclipse.jdt.core.prefs @@ -0,0 +1,12 @@ +eclipse.preferences.version=1 +org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled +org.eclipse.jdt.core.compiler.codegen.targetPlatform=11 +org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve +org.eclipse.jdt.core.compiler.compliance=11 +org.eclipse.jdt.core.compiler.debug.lineNumber=generate +org.eclipse.jdt.core.compiler.debug.localVariable=generate +org.eclipse.jdt.core.compiler.debug.sourceFile=generate +org.eclipse.jdt.core.compiler.problem.assertIdentifier=error +org.eclipse.jdt.core.compiler.problem.enumIdentifier=error +org.eclipse.jdt.core.compiler.release=enabled +org.eclipse.jdt.core.compiler.source=11 diff --git a/bin/j1/Hello.class b/bin/j1/Hello.class new file mode 100644 index 0000000..a1decad --- /dev/null +++ b/bin/j1/Hello.class Binary files differ diff --git a/bin/j1/HelloNW.class b/bin/j1/HelloNW.class new file mode 100644 index 0000000..8d3beae --- /dev/null +++ b/bin/j1/HelloNW.class Binary files differ diff --git a/bin/j1/PrintArgs.class b/bin/j1/PrintArgs.class new file mode 100644 index 0000000..bfc191e --- /dev/null +++ b/bin/j1/PrintArgs.class Binary files differ diff --git a/bin/j1/ReadFile.class b/bin/j1/ReadFile.class new file mode 100644 index 0000000..80e0634 --- /dev/null +++ b/bin/j1/ReadFile.class Binary files differ diff --git a/bin/j1/ReadFile2.class b/bin/j1/ReadFile2.class new file mode 100644 index 0000000..f81cf3c --- /dev/null +++ b/bin/j1/ReadFile2.class Binary files differ diff --git a/bin/j1/ReadWrite.class b/bin/j1/ReadWrite.class new file mode 100644 index 0000000..5536c14 --- /dev/null +++ b/bin/j1/ReadWrite.class Binary files differ diff --git a/bin/j1/TestThread.class b/bin/j1/TestThread.class new file mode 100644 index 0000000..6fdf06a --- /dev/null +++ b/bin/j1/TestThread.class Binary files differ diff --git a/bin/j1/WriteFile.class b/bin/j1/WriteFile.class new file mode 100644 index 0000000..a9c61e1 --- /dev/null +++ b/bin/j1/WriteFile.class Binary files differ diff --git a/bin/j1/myThread.class b/bin/j1/myThread.class new file mode 100644 index 0000000..fba74c6 --- /dev/null +++ b/bin/j1/myThread.class Binary files differ diff --git a/bin/j2/EchoClient.class b/bin/j2/EchoClient.class new file mode 100644 index 0000000..52d0a38 --- /dev/null +++ b/bin/j2/EchoClient.class Binary files differ diff --git a/bin/j2/EchoServer.class b/bin/j2/EchoServer.class new file mode 100644 index 0000000..e7783aa --- /dev/null +++ b/bin/j2/EchoServer.class Binary files differ diff --git a/bin/j2/NetClock.class b/bin/j2/NetClock.class new file mode 100644 index 0000000..a9bcf7e --- /dev/null +++ b/bin/j2/NetClock.class Binary files differ diff --git a/bin/j2/Phttpd.class b/bin/j2/Phttpd.class new file mode 100644 index 0000000..6bc16a8 --- /dev/null +++ b/bin/j2/Phttpd.class Binary files differ diff --git a/bin/j2/ReadNet.class b/bin/j2/ReadNet.class new file mode 100644 index 0000000..d0cf329 --- /dev/null +++ b/bin/j2/ReadNet.class Binary files differ diff --git a/bin/j2/WRNet.class b/bin/j2/WRNet.class new file mode 100644 index 0000000..a22fa4e --- /dev/null +++ b/bin/j2/WRNet.class Binary files differ diff --git a/bin/j3/CtrlListen.class b/bin/j3/CtrlListen.class new file mode 100644 index 0000000..d681c0d --- /dev/null +++ b/bin/j3/CtrlListen.class Binary files differ diff --git a/bin/j3/MyFTP.class b/bin/j3/MyFTP.class new file mode 100644 index 0000000..2d43585 --- /dev/null +++ b/bin/j3/MyFTP.class Binary files differ diff --git a/bin/j4/Clock.class b/bin/j4/Clock.class new file mode 100644 index 0000000..2d5c5ec --- /dev/null +++ b/bin/j4/Clock.class Binary files differ diff --git a/bin/j4/ClockClient.class b/bin/j4/ClockClient.class new file mode 100644 index 0000000..c0a8c9d --- /dev/null +++ b/bin/j4/ClockClient.class Binary files differ diff --git a/bin/j4/ClockImpl.class b/bin/j4/ClockImpl.class new file mode 100644 index 0000000..9deb319 --- /dev/null +++ b/bin/j4/ClockImpl.class Binary files differ diff --git a/bin/j4/ClockServer.class b/bin/j4/ClockServer.class new file mode 100644 index 0000000..d535c2a --- /dev/null +++ b/bin/j4/ClockServer.class Binary files differ diff --git a/bin/j5/DPiClient.class b/bin/j5/DPiClient.class new file mode 100644 index 0000000..94fb11a --- /dev/null +++ b/bin/j5/DPiClient.class Binary files differ diff --git a/bin/j5/Pi.class b/bin/j5/Pi.class new file mode 100644 index 0000000..a3f7ad3 --- /dev/null +++ b/bin/j5/Pi.class Binary files differ diff --git a/bin/j5/PiClient.class b/bin/j5/PiClient.class new file mode 100644 index 0000000..61f6b98 --- /dev/null +++ b/bin/j5/PiClient.class Binary files differ diff --git a/bin/j5/PiImpl.class b/bin/j5/PiImpl.class new file mode 100644 index 0000000..450eafb --- /dev/null +++ b/bin/j5/PiImpl.class Binary files differ diff --git a/bin/j5/PiServer.class b/bin/j5/PiServer.class new file mode 100644 index 0000000..6147b0f --- /dev/null +++ b/bin/j5/PiServer.class Binary files differ diff --git a/bin/j5/Result.class b/bin/j5/Result.class new file mode 100644 index 0000000..04ec30a --- /dev/null +++ b/bin/j5/Result.class Binary files differ diff --git a/bin/j5/Sample.class b/bin/j5/Sample.class new file mode 100644 index 0000000..4bd9141 --- /dev/null +++ b/bin/j5/Sample.class Binary files differ diff --git a/bin/j5/index.html b/bin/j5/index.html new file mode 100644 index 0000000..363e964 --- /dev/null +++ b/bin/j5/index.html @@ -0,0 +1,6 @@ +No.1 ファイル資料 DPiClient.java 未参照 登録日:2019/09/18 - +No.2 ファイル資料 Pi.java 未参照 登録日:2019/09/18 - +No.3 ファイル資料 PiImpl.java 未参照 登録日:2019/09/18 - +No.4 ファイル資料 PiServer.java 未参照 登録日:2019/09/18 - +No.5 ファイル資料 Sample.java 未参照 登録日:2019/09/18 - +No.6 ファイル資料 PiClient.java diff --git a/bin/j5/launchPiServer.class b/bin/j5/launchPiServer.class new file mode 100644 index 0000000..fed707e --- /dev/null +++ b/bin/j5/launchPiServer.class Binary files differ diff --git a/bin/j6/Chat.class b/bin/j6/Chat.class new file mode 100644 index 0000000..393a2d6 --- /dev/null +++ b/bin/j6/Chat.class Binary files differ diff --git a/bin/j6/ChatServer.class b/bin/j6/ChatServer.class new file mode 100644 index 0000000..1e47b8a --- /dev/null +++ b/bin/j6/ChatServer.class Binary files differ diff --git a/bin/j6/ListenPacket.class b/bin/j6/ListenPacket.class new file mode 100644 index 0000000..d58a01d --- /dev/null +++ b/bin/j6/ListenPacket.class Binary files differ diff --git a/bin/j6/StreamConnector.class b/bin/j6/StreamConnector.class new file mode 100644 index 0000000..02333e1 --- /dev/null +++ b/bin/j6/StreamConnector.class Binary files differ diff --git a/bin/j6/T1.class b/bin/j6/T1.class new file mode 100644 index 0000000..954f1a0 --- /dev/null +++ b/bin/j6/T1.class Binary files differ diff --git a/bin/j6/clientProc.class b/bin/j6/clientProc.class new file mode 100644 index 0000000..34d1848 --- /dev/null +++ b/bin/j6/clientProc.class Binary files differ diff --git a/bin/j7/SeaGameClient$1.class b/bin/j7/SeaGameClient$1.class new file mode 100644 index 0000000..67ee938 --- /dev/null +++ b/bin/j7/SeaGameClient$1.class Binary files differ diff --git a/bin/j7/SeaGameClient$2.class b/bin/j7/SeaGameClient$2.class new file mode 100644 index 0000000..18bb686 --- /dev/null +++ b/bin/j7/SeaGameClient$2.class Binary files differ diff --git a/bin/j7/SeaGameClient$3.class b/bin/j7/SeaGameClient$3.class new file mode 100644 index 0000000..708e7c1 --- /dev/null +++ b/bin/j7/SeaGameClient$3.class Binary files differ diff --git a/bin/j7/SeaGameClient$4.class b/bin/j7/SeaGameClient$4.class new file mode 100644 index 0000000..6d3704e --- /dev/null +++ b/bin/j7/SeaGameClient$4.class Binary files differ diff --git a/bin/j7/SeaGameClient$5.class b/bin/j7/SeaGameClient$5.class new file mode 100644 index 0000000..54f6261 --- /dev/null +++ b/bin/j7/SeaGameClient$5.class Binary files differ diff --git a/bin/j7/SeaGameClient$6.class b/bin/j7/SeaGameClient$6.class new file mode 100644 index 0000000..e28fe96 --- /dev/null +++ b/bin/j7/SeaGameClient$6.class Binary files differ diff --git a/bin/j7/SeaGameClient$7.class b/bin/j7/SeaGameClient$7.class new file mode 100644 index 0000000..cf55137 --- /dev/null +++ b/bin/j7/SeaGameClient$7.class Binary files differ diff --git a/bin/j7/SeaGameClient.class b/bin/j7/SeaGameClient.class new file mode 100644 index 0000000..29f486a --- /dev/null +++ b/bin/j7/SeaGameClient.class Binary files differ diff --git a/bin/j7/SeaGameServer$1.class b/bin/j7/SeaGameServer$1.class new file mode 100644 index 0000000..d85bdd6 --- /dev/null +++ b/bin/j7/SeaGameServer$1.class Binary files differ diff --git a/bin/j7/SeaGameServer.class b/bin/j7/SeaGameServer.class new file mode 100644 index 0000000..a473b3a --- /dev/null +++ b/bin/j7/SeaGameServer.class Binary files differ diff --git a/bin/j7/Ship.class b/bin/j7/Ship.class new file mode 100644 index 0000000..2262ab1 --- /dev/null +++ b/bin/j7/Ship.class Binary files differ diff --git a/bin/j7/clientProc.class b/bin/j7/clientProc.class new file mode 100644 index 0000000..b6142a9 --- /dev/null +++ b/bin/j7/clientProc.class Binary files differ diff --git a/src/j1/Hello.class b/src/j1/Hello.class new file mode 100644 index 0000000..d1ab13f --- /dev/null +++ b/src/j1/Hello.class Binary files differ diff --git a/src/j1/Hello.java b/src/j1/Hello.java new file mode 100644 index 0000000..ef4dc83 --- /dev/null +++ b/src/j1/Hello.java @@ -0,0 +1,8 @@ +package j1; + +// Hello.java +public class Hello{ + public static void main(String[] args){ + System.out.println("Hello!"); + } +} diff --git a/src/j1/HelloNW.class b/src/j1/HelloNW.class new file mode 100644 index 0000000..d6f93aa --- /dev/null +++ b/src/j1/HelloNW.class Binary files differ diff --git a/src/j1/HelloNW.java b/src/j1/HelloNW.java new file mode 100644 index 0000000..6823346 --- /dev/null +++ b/src/j1/HelloNW.java @@ -0,0 +1,11 @@ +package j1; + +// HelloNW.java +public class HelloNW{ + public static void main(String[] args){ + System.out.println("Hello!"); + System.out.print("Welcome to ") ; + System.out.print("nerwork programming ") ; + System.out.println("in Java!"); + } +} diff --git a/src/j1/PrintArgs.class b/src/j1/PrintArgs.class new file mode 100644 index 0000000..5d08adc --- /dev/null +++ b/src/j1/PrintArgs.class Binary files differ diff --git a/src/j1/PrintArgs.java b/src/j1/PrintArgs.java new file mode 100644 index 0000000..bf459f6 --- /dev/null +++ b/src/j1/PrintArgs.java @@ -0,0 +1,15 @@ +package j1; + +// PrintArgs.java + +// Stringは文字列を表す. +// String[]は文字列を要素とする配列を表す. +// <配列変数名>.length は,配列の要素の数を表す. +// なお,< と >で囲まれた日本語はJavaの構文の要素を示す. + +public class PrintArgs{ + public static void main(String[] args){ + for(int i = 0; i< args.length; i += 1) + System.out.println("args["+ i+ "] = "+ args[i]); + } +} diff --git a/src/j1/ReadFile.class b/src/j1/ReadFile.class new file mode 100644 index 0000000..d72edd6 --- /dev/null +++ b/src/j1/ReadFile.class Binary files differ diff --git a/src/j1/ReadFile.java b/src/j1/ReadFile.java new file mode 100644 index 0000000..cfb0ff7 --- /dev/null +++ b/src/j1/ReadFile.java @@ -0,0 +1,42 @@ +package j1; + +// File: ReadFile.java +// ファイルの内容を読み取り,そのまま画面に出力する +// 使い方: java Readfile <ファイル名> +import java.io.FileInputStream; +import java.io.FileNotFoundException; +import java.io.IOException; + +public class ReadFile { + public static void main(String[] args){ + byte[] buff = new byte[1024]; + FileInputStream infile = null; + + try{ + infile = new FileInputStream(args[0]) ; + } + catch(FileNotFoundException e){ // ファイル準備の失敗 + + System.err.println("Error: File not found.") ; + System.exit(1) ; + } + + while (true) { + try { + int n = infile.read(buff); + System.out.write(buff, 0, n) ; + } + catch(Exception e){ // 読み出し終了 + break; // 繰り返しを終了 + } + } + + try{ + infile.close() ; + } + catch(IOException e){ + System.err.println("Error(close).") ; + System.exit(1) ; + } + } +} diff --git a/src/j1/ReadFile2.java b/src/j1/ReadFile2.java new file mode 100644 index 0000000..3335399 --- /dev/null +++ b/src/j1/ReadFile2.java @@ -0,0 +1,42 @@ +package j1; + +import java.io.FileInputStream; +import java.io.IOException; + +// File: ReadFile2.java +// ファイルの内容を読み取り,そのまま画面に出力する +// 使い方: java Readfile2 <ファイル名> + +public class ReadFile2 { + public static void main(String[] args){ + byte[] buff = new byte[1024]; + FileInputStream infile = null; + + try{ + infile = new java.io.FileInputStream(args[0]) ; + } + catch(java.io.FileNotFoundException e){ // ファイル準備の失敗 + + System.err.println("Error: File not found.") ; + System.exit(1) ; + } + + while (true) { + try { + int n = infile.read(buff); + System.out.write(buff, 0, n) ; + } + catch(IOException e){ // 読み出し終了 + break; // 繰り返しを終了 + } + } + + try{ + infile.close() ; + } + catch(java.io.IOException e){ + System.err.println("Error(close).") ; + System.exit(1) ; + } + } +} diff --git a/src/j1/ReadWrite.class b/src/j1/ReadWrite.class new file mode 100644 index 0000000..e8f580d --- /dev/null +++ b/src/j1/ReadWrite.class Binary files differ diff --git a/src/j1/ReadWrite.java b/src/j1/ReadWrite.java new file mode 100644 index 0000000..91b3191 --- /dev/null +++ b/src/j1/ReadWrite.java @@ -0,0 +1,18 @@ +package j1; + +public class ReadWrite { + public static void main(String[] args){ + byte[] buff = new byte[1024]; + while (true) { + try { + int n = System.in.read(buff); + System.out.write(buff, 0, n); + System.out.println("文字数は "+n); + if (n==1) break; // Enter(改行)だけだと終了 + } + catch(Exception e){ + System.exit(1); + } + } + } +} \ No newline at end of file diff --git a/src/j1/TestThread.class b/src/j1/TestThread.class new file mode 100644 index 0000000..097f1ee --- /dev/null +++ b/src/j1/TestThread.class Binary files differ diff --git a/src/j1/TestThread.java b/src/j1/TestThread.java new file mode 100644 index 0000000..f284039 --- /dev/null +++ b/src/j1/TestThread.java @@ -0,0 +1,34 @@ +package j1; + +//----------------------------------------------------------- +public class TestThread{ + public static void main(String[] args){ + System.out.print("TestThread"); + + myThread mt1 = new myThread("1 ", 1300); + myThread mt2 = new myThread("2 ", 2900); + + mt1.start(); + mt2.start(); + } +}; +//----------------------------------------------------------- +class myThread extends Thread { + String s; + int t; + + myThread(String s, int t) { + this.s = s; + this.t = t; + } + public void run() { + while(true) { + try { + System.out.print(this.s); + sleep(t); // tミリ秒動作休止 + } catch(InterruptedException e) { + break; + } + } // end while + } // end of run +} // end of class myThread diff --git a/src/j1/WriteFile.class b/src/j1/WriteFile.class new file mode 100644 index 0000000..0475dbe --- /dev/null +++ b/src/j1/WriteFile.class Binary files differ diff --git a/src/j1/WriteFile.java b/src/j1/WriteFile.java new file mode 100644 index 0000000..a9eab9e --- /dev/null +++ b/src/j1/WriteFile.java @@ -0,0 +1,43 @@ +package j1; + +// WriteFile.java + +// キーボードからの入力を受け取り,そのまま画面に出力する. +// また,ファイルに入力を順に格納する. +// 使い方java Writefile ファイル名 +// プログラムを終了するには,行の先頭にピリオド.を入力する. + +import java.io.*; +public class WriteFile { + public static void main(String[] args){ + byte[] buff = new byte[1024]; + boolean cont = true ; + FileOutputStream outfile = null; // ファイル出力用オブジェクト + try{ + outfile = new FileOutputStream(args[0]) ; + } + catch(FileNotFoundException e){ // ファイル準備の失敗 + System.err.println("File not found") ; + System.exit(1) ; + } + // 行頭でピリオドが入力されるまで繰り返す. + while (cont) { + try { + int n = System.in.read(buff); + System.out.write(buff, 0, n) ; + if(buff[0] == '.') cont = false ; + else outfile.write(buff,0,n) ; + } + catch(Exception e){ + System.exit(1); // プログラムの終了 + } + } + try{ + outfile.close() ; + } + catch(IOException e){ + System.err.println("Error in closing a file") ; + System.exit(1) ; + } + } +} \ No newline at end of file diff --git a/src/j1/myThread.class b/src/j1/myThread.class new file mode 100644 index 0000000..e72f171 --- /dev/null +++ b/src/j1/myThread.class Binary files differ diff --git a/src/j2/EchoClient.class b/src/j2/EchoClient.class new file mode 100644 index 0000000..b9cd5be --- /dev/null +++ b/src/j2/EchoClient.class Binary files differ diff --git a/src/j2/EchoClient.java b/src/j2/EchoClient.java new file mode 100644 index 0000000..9f82632 --- /dev/null +++ b/src/j2/EchoClient.java @@ -0,0 +1,47 @@ +package j2; + +import java.io.*; +import java.net.*; +public class EchoClient { + public static void main(String[] args) { + // ソケットや入出力用のストリームの宣言 + Socket echoSocket = null; + DataOutputStream os = null; + BufferedReader is = null; + + // ポート9999番を開く + try { + echoSocket = new Socket("localhost", 9999); + os = new DataOutputStream(echoSocket.getOutputStream()); + is = new BufferedReader(new InputStreamReader(echoSocket.getInputStream())); + } catch (UnknownHostException e) { + System.err.println("Don't know about host: localhost"); + } catch (IOException e) { + System.err.println("Couldn't get I/O for the connection to: localhost"); + } + + // サーバーにメッセージを送る + if (echoSocket != null && os != null && is != null) { + try { + // メッセージを送ります + os.writeBytes("HELLO\n"); + os.flush(); + + // サーバーからのメッセージを受け取り画面に表示します + String responseLine; + if ((responseLine = is.readLine()) != null) { + System.out.println("Server: " + responseLine); + } + + // 開いたソケットなどをクローズ + os.close(); + is.close(); + echoSocket.close(); + } catch (UnknownHostException e) { + System.err.println("Trying to connect to unknown host: " + e); + } catch (IOException e) { + System.err.println("IOException: " + e); + } + } + } +} \ No newline at end of file diff --git a/src/j2/EchoServer.class b/src/j2/EchoServer.class new file mode 100644 index 0000000..0528efe --- /dev/null +++ b/src/j2/EchoServer.class Binary files differ diff --git a/src/j2/EchoServer.java b/src/j2/EchoServer.java new file mode 100644 index 0000000..9aad251 --- /dev/null +++ b/src/j2/EchoServer.java @@ -0,0 +1,41 @@ +package j2; + +import java.io.*; +import java.net.*; + +public class EchoServer { + public static void main(String args[]) { + // ソケットや入出力用のストリームの宣言 + ServerSocket echoServer = null; + String line; + BufferedReader is; + PrintStream os; + Socket clientSocket = null; + + // ポート9999番を開く + try { + echoServer = new ServerSocket(9999); + } + catch (IOException e) { + System.out.println(e); + } + + // クライアントからの要求を受けるソケットを開く + try { + clientSocket = echoServer.accept(); + System.out.println("accept"); + is = new BufferedReader(new InputStreamReader(clientSocket.getInputStream())); + os = new PrintStream(clientSocket.getOutputStream()); + + // クライアントからのメッセージを待ち、受け取ったメッセージをそのまま返す + while (true) { + line = is.readLine(); + System.out.println("(Server) " +line); + os.println(line); + } + } + catch (IOException e) { + System.out.println(e); + } + } +} diff --git a/src/j2/NetClock.class b/src/j2/NetClock.class new file mode 100644 index 0000000..ab3f7e9 --- /dev/null +++ b/src/j2/NetClock.class Binary files differ diff --git a/src/j2/NetClock.java b/src/j2/NetClock.java new file mode 100644 index 0000000..3fd1165 --- /dev/null +++ b/src/j2/NetClock.java @@ -0,0 +1,58 @@ +package j2; +// 時刻を答えるサーバプログラムNetclock.java +// このプログラムはポート番号6000番で動作するサーバです +// クライアントからの接続に対し,時刻を返します +// このプログラムを停止させるにはコントロールCを入力してください +// 使い方java Netclock + +import java.io.DataOutputStream; +// ライブラリの利用 +import java.io.IOException; +import java.net.InetSocketAddress; +import java.net.ServerSocket; +import java.net.Socket; +import java.util.Date; + +// Netclockクラス +class NetClock{ + public static void main(String args[]){ + ServerSocket servsock = null ;// サーバ用ソケット + Socket sock ;// ソケットの読み書き用オブジェクト + DataOutputStream writer ;// 出力ストリーム + String outstr ;// 出力データを格納する文字列 + int i ;//出力の繰り返し制御用変数 + Date d ;// 日付時刻処理用オブジェクト + + try{ + // サーバソケットの作成 + servsock = new ServerSocket() ; + servsock.bind(new InetSocketAddress("127.0.0.1", 5555)); + // サーバ処理の繰り返し +// while(true){ + sock = servsock.accept() ;//接続受付 + writer = new DataOutputStream(sock.getOutputStream()) ; + System.out.println("accepted (client has connected)"); + // 出力用データの作成 + d = new Date() ; + outstr = "\n" + + "Hello, this is Netclock server." + + "\n" + d.toString() + "\n" + + "Thank you." + "\n"; + System.out.println("(Server-side message) "+ outstr); + // データの出力 + writer.writeBytes(outstr); + writer.flush(); +// for(i = 0; i < outstr.length(); ++i) +// out.write((int)outstr.charAt(i)) ; +// out.write('\n') ; + writer.close(); + // 接続終了 + sock.close() ; +// } + }catch(IOException e){ + System.out.println("*** Exception ***"); + System.out.println(e); + System.exit(1) ; + } + } +} \ No newline at end of file diff --git a/src/j2/Phttpd.class b/src/j2/Phttpd.class new file mode 100644 index 0000000..0c65f16 --- /dev/null +++ b/src/j2/Phttpd.class Binary files differ diff --git a/src/j2/Phttpd.java b/src/j2/Phttpd.java new file mode 100644 index 0000000..caf034d --- /dev/null +++ b/src/j2/Phttpd.java @@ -0,0 +1,78 @@ +package j2; +// いんちきHTTPサーバPhttpd.java +// このプログラムはポート番号80番で動作するサーバです +// 使い方java Phttp データファイル名 +// WWWクライアントからの接続に対し,引数で指定したファイルを返します + +// ライブラリの利用 +import java.io.BufferedReader; +import java.io.FileInputStream; +import java.io.IOException; +import java.io.InputStreamReader; +import java.io.OutputStream; +import java.net.ServerSocket; +import java.net.Socket; + +// Phttpdクラス +class Phttpd{ + public static void main(String args[]){ + // サーバソケット + ServerSocket servsock = null ; + Socket sock ; + + // 入出力 + OutputStream out ; + BufferedReader in ; + FileInputStream infile = null; + byte[] buff = new byte[1024]; + + // その他 + boolean cont = true ; + int i ; + + try{ + // サーバ用ソケットの作成(ポート番号8000番) + servsock = new ServerSocket(8000,300) ; + while(true){ + sock = servsock.accept() ;// 接続要求の受付 + // 接続先の表示 + System.out.println("Connection 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())); + out = sock.getOutputStream() ; + // とりあえず改行を2つ読み飛ばす + for(i = 0; i < 2;++i) + System.out.println(in.readLine() ); + // ファイルの出力 + cont = true ; + while(cont){ + // ファイルからの読み込みとネットワーク出力 + try{ + int n = infile.read(buff); + System.out.write(buff, 0, n); + out.write(buff,0,n) ; + } + catch(Exception e){ + cont = false ; + } + } + // 接続終了 + sock.close() ; + infile.close() ; + } + }catch(IOException e){ + System.exit(1) ; + } + } +} \ No newline at end of file diff --git a/src/j2/ReadNet.class b/src/j2/ReadNet.class new file mode 100644 index 0000000..15ddc64 --- /dev/null +++ b/src/j2/ReadNet.class Binary files differ diff --git a/src/j2/ReadNet.java b/src/j2/ReadNet.java new file mode 100644 index 0000000..a3db040 --- /dev/null +++ b/src/j2/ReadNet.java @@ -0,0 +1,65 @@ +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) ; + } + } +} \ No newline at end of file diff --git a/src/j2/WRNet.class b/src/j2/WRNet.class new file mode 100644 index 0000000..d2d3f2f --- /dev/null +++ b/src/j2/WRNet.class Binary files differ diff --git a/src/j2/WRNet.java b/src/j2/WRNet.java new file mode 100644 index 0000000..3e331f2 --- /dev/null +++ b/src/j2/WRNet.java @@ -0,0 +1,65 @@ +package j2; + +// WRNet.java +// ネットワーク上のサーバに接続し,データを送ります +// その後サーバからデータを受け取り,そのまま画面に出力します +// 使い方 java WRNet DNS名 ポート番号 +// 例 java WRNet localhost 8000 + +//ライブラリの利用 +import java.io.*; +import java.net.* ; + +// Wrnetクラス +public class WRNet { + // プログラムの本体main + public static void main(String[] args){ + byte[] buff = new byte[1024];//配列の定義 + Socket wrsocket = null ;// サーバ接続用ソケット + InputStream instr = null;// データ読み取り用オブジェクト + OutputStream outstr = null;// データ出力用オブジェクト + boolean cont = true ; + // 指定のポートに対して,ソケットを作成します + // 入出力のストリームを作り,データ読み出しを準備します + try{ + wrsocket + = new Socket(args[0],Integer.parseInt(args[1])) ; + instr = wrsocket.getInputStream() ; + outstr = wrsocket.getOutputStream() ; + } catch (Exception e) { + System.err.println("Network error.") ; + System.exit(1) ; + } + // + while (cont) { + try { + int n = System.in.read(buff); + if(buff[0] == '.') cont = false ; + else outstr.write(buff,0,n) ; + } + // 以下は例外処理です + catch(Exception e){ + // 例外時はプログラムを終了します + System.exit(1); + } + } + // データの終了まで,以下のループを繰り返します + cont = true ; + while (cont) { + try { + int n = instr.read(buff); + System.out.write(buff, 0, n) ; + } + catch(Exception e){ + // 読み出し終了時にループも終了 + cont = false ; + } + } + try { + instr.close() ; + } catch(Exception e) { + System.err.println("Close failed.") ; + System.exit(1) ; + } + } +} diff --git a/src/j3/MyFTP.java b/src/j3/MyFTP.java new file mode 100644 index 0000000..d7eb19b --- /dev/null +++ b/src/j3/MyFTP.java @@ -0,0 +1,341 @@ +package j3; + +// 機能 : ftp サーバと接続してファイル転送を行う +// 使い方: java MyFTP <サーバ名> +// 起動例: java MyFTP ftp.ne.jp + +import java.net.*; +import java.io.*; + +public class MyFTP { + Socket ctrlSocket; //制御用ソケット + public PrintWriter ctrlOutput; //制御出力用ストリーム + public BufferedReader ctrlInput;// 同入力用ストリーム + + final int CTRLPORT = 21 ;// ftp の制御用ポート + //------------------------------------------------------------------ + //アドレスとポート番号からソケットを作り制御用ストリームを作成 + public void openConnection(String host) + throws IOException,UnknownHostException { + ctrlSocket = new Socket(host, CTRLPORT); + ctrlOutput = new PrintWriter(ctrlSocket.getOutputStream()); + ctrlInput + = new BufferedReader(new + InputStreamReader(ctrlSocket.getInputStream())); + } // end of openConnection + //------------------------------------------------------------------ + //制御用のソケットを閉じる + public void closeConnection() throws IOException { + ctrlSocket.close() ; + } // end of closeConnection + //------------------------------------------------------------------ + // ftp サーバにログイン + public void doLogin() { + String loginName = "" ; + String password = "" ; + BufferedReader lineread + = new BufferedReader(new InputStreamReader(System.in)) ; + + try { + System.out.println("ログイン名を入力してください") ; + loginName = lineread.readLine() ; + // USERコマンドによるログイン + ctrlOutput.println("USER " + loginName) ; + ctrlOutput.flush() ; + // PASSコマンドによるパスワードの入力 + System.out.println("パスワードを入力してください") ; + password = lineread.readLine() ; + ctrlOutput.println("PASS " + password) ; + ctrlOutput.flush() ; + } catch(Exception e) { + e.printStackTrace(); + System.exit(1); + } + } // end of doLogin + //------------------------------------------------------------------ + // ftp サーバからログアウト + public void doQuit() { + try { + ctrlOutput.println("QUIT ") ;// QUITコマンドの送信 + ctrlOutput.flush() ; + } catch(Exception e) { + e.printStackTrace(); + System.exit(1); + } + } // end of doQuit + //------------------------------------------------------------------ + // ディレクトリを変更 + public void doCd() { + String dirName = "" ; + BufferedReader lineread + = new BufferedReader(new InputStreamReader(System.in)) ; + + try { + System.out.println("ディレクトリ名を入力してください") ; + dirName = lineread.readLine() ; + ctrlOutput.println("CWD " + dirName) ;// CWDコマンド + ctrlOutput.flush() ; + } catch(Exception e) { + e.printStackTrace(); + System.exit(1); + } + } // end of doCd + //------------------------------------------------------------------ + // ディレクトリ情報を取得 + public void doLs(){ + int n ; + byte[] buff = new byte[1024] ; + try { + // データ用コネクションを作成 + Socket dataSocket = dataConnection("LIST") ; + + // データ読み取り用ストリームを用意 + BufferedInputStream dataInput + = new BufferedInputStream(dataSocket.getInputStream()) ; + + // ディレクトリ情報を読む + while((n = dataInput.read(buff)) > 0){ + System.out.write(buff,0,n) ; + } + dataSocket.close() ; + } catch(Exception e) { + e.printStackTrace(); + System.exit(1); + } + } // end of doLs + //------------------------------------------------------------------ + // サーバとのデータ交換用にソケットを作ります + // また,サーバに対してportコマンドでポートを通知します + public Socket dataConnection(String ctrlcmd) + { + String cmd = "PORT " ; //PORTコマンドで送るデータの格納用変数 + int i ; + Socket dataSocket = null ;// データ転送用ソケット + try { + // 自分のアドレスを求める + byte[] address = InetAddress.getLocalHost().getAddress() ; + + // 適当なポート番号のサーバソケットを作成 + ServerSocket serverDataSocket = new ServerSocket(0,1) ; + + // PORTコマンド用の送信データを作成 + for(i = 0; i < 4; ++i) + cmd = cmd + (address[i] & 0xff) + "," ; + cmd = cmd + (((serverDataSocket.getLocalPort()) / 256) & 0xff) + + "," + + (serverDataSocket.getLocalPort() & 0xff) ; + + // PORTコマンドを制御用ストリームを通して送る + ctrlOutput.println(cmd) ; + ctrlOutput.flush() ; + + // 処理対象コマンド(LIST,RETR,およびSTOR)をサーバに送る + ctrlOutput.println(ctrlcmd) ; + ctrlOutput.flush() ; + + // サーバからの接続を受け付ける + dataSocket = serverDataSocket.accept() ; + serverDataSocket.close() ; + } catch(Exception e) { + e.printStackTrace(); + System.exit(1); + } + return dataSocket ; + } // end of dataConnection + //------------------------------------------------------------------ + // サーバ上のファイルを取得 + public void doGet() { + String fileName = "" ; + BufferedReader lineread + = new BufferedReader(new InputStreamReader(System.in)) ; + int n ; + byte[] buff = new byte[1024] ; + + try { + // サーバ上ファイルのファイル名を指定します + System.out.println("ファイル名を入力してください") ; + fileName = lineread.readLine() ; + + // クライアント上に受信用ファイルを準備 + FileOutputStream outfile = new FileOutputStream(fileName) ; + + // ファイル転送用データストリームを作成 + Socket dataSocket = dataConnection("RETR " + fileName) ; + BufferedInputStream dataInput + = new BufferedInputStream(dataSocket.getInputStream()) ; + + // サーバからデータを受け取り,ファイルに格納 + while((n = dataInput.read(buff)) > 0){ + outfile.write(buff,0,n) ; + } + dataSocket.close() ; + outfile.close() ; + } catch (Exception e) { + e.printStackTrace(); + System.exit(1); + } + } // end of doGet + //------------------------------------------------------------------ + // サーバへファイルを送ります + public void doPut() { + String fileName = "" ; + BufferedReader lineread + = new BufferedReader(new InputStreamReader(System.in)) ; + int n ; + byte[] buff = new byte[1024] ; + + try { + System.out.println("Input File Name: ") ; + fileName = lineread.readLine() ; + + // クライアント上のファイルの読み出し準備を行います + FileInputStream sendfile = new FileInputStream(fileName) ; + + // 転送用データストリームを用意します + Socket dataSocket = dataConnection("STOR " + fileName) ; + OutputStream outstr = dataSocket.getOutputStream(); + + // ファイルを読み出し,ネットワーク経由でサーバに送る + while((n = sendfile.read(buff)) > 0){ + outstr.write(buff,0,n) ; + } + dataSocket.close() ; + sendfile.close() ; + } catch (Exception e) { + e.printStackTrace(); + System.exit(1); + } + } // end of doPut + //------------------------------------------------------------------ + // Ftp のコマンドメニューを出力 + public void showMenu() + { + System.out.println("Input Command: ") ; + System.out.print("1(login)") ; + System.out.print(" 2(ls)") ; + System.out.print(" 3(cd)") ; + System.out.print(" 4(get)") ; + System.out.print(" 5(put)") ; + System.out.println(" 9(quit)") ; + } // end of showMenu + //------------------------------------------------------------------ + // コマンドを入力 + public String getCommand() + { + String buf = "" ; + BufferedReader lineread + = new BufferedReader(new InputStreamReader(System.in)) ; + while(buf.length() != 1){// 1文字の入力を受けるまで繰り返し + try { + buf = lineread.readLine() ; + } catch(Exception e) { + e.printStackTrace(); + System.exit(1); + } + } + return (buf) ; + } // end of getCommand + //------------------------------------------------------------------ + // コマンドに対応する各処理を呼び出す + public boolean execCommand(String command) + { + boolean cont = true ; + switch (Integer.parseInt(command)){ + case 1 : // login 処理 + doLogin() ; + break ; + case 2 : // サーバのディレクトリ表示処理 + doLs() ; + break ; + case 3 : // サーバの作業ディレクトリ変更処理 + doCd() ; + break ; + case 4 : // サーバからのファイル取得処理 + doGet() ; + break ; + case 5 : // サーバへのファイル転送処理 + doPut() ; + break ; + case 9 : // 処理の終了 + doQuit() ; + cont = false ; + break ; + default : //それ以外の入力 + System.out.println("番号を選択してください") ; + } + return(cont) ; + } // end of execCommand + //------------------------------------------------------------------ + // Ftp のコマンドメニューを出力して,各処理を呼び出します + public void main_proc() + throws IOException + { + boolean cont = true ; + try { + while(cont){ + // メニューを出力します + showMenu() ; + // コマンドを受け取り実行します + cont = execCommand(getCommand()) ; + } + } + catch (Exception e) { + System.err.print(e); + System.exit(1); + } + } // end of main_proc + //------------------------------------------------------------------ + // 制御ストリームの受信スレッドを開始します + public void getMsgs(){ + try { + CtrlListen listener = new CtrlListen(ctrlInput) ; + Thread listenerthread = new Thread(listener) ; + listenerthread.start() ; + } catch (Exception e) { + e.printStackTrace() ; + System.exit(1) ; + } + } // end of getMsgs + //------------------------------------------------------------------ + // TCPコネクションを開いて処理を開始します + public static void main(String[] arg){ + if (arg.length != 1) { + System.out.println("Usage: MyFTP "); + System.exit(1); + } + try { + MyFTP f = null; + f = new MyFTP(); + f.openConnection(arg[0]); // 制御用コネクションの設定 + f.getMsgs() ; // 受信スレッドの開始 +System.out.println("309"); + f.main_proc(); // ftp 処理 +System.out.println("311"); + + f.closeConnection() ; // コネクションのクローズ + System.exit(0) ; // プログラムの終了 + } catch (Exception e) { + e.printStackTrace(); + System.exit(1); + } + } // end of main +} // end of class MyFTP +//============================================================================= +// CtrlListen クラス +class CtrlListen implements Runnable{ + BufferedReader ctrlInput = null ; + // コンストラクタ読み取り先の指定 + public CtrlListen(BufferedReader in){ + ctrlInput = in ; + } + + public void run(){ + while(true){ + try { // 行を読み取り,標準出力にコピー + System.out.println(ctrlInput.readLine()) ; + } catch (Exception e){ + System.exit(1) ; + } + } + } +} // end of class CtrlListen diff --git a/src/j4/Clock.java b/src/j4/Clock.java new file mode 100644 index 0000000..f7f6b84 --- /dev/null +++ b/src/j4/Clock.java @@ -0,0 +1,16 @@ +package j4; +// RMIによるNetClockプログラムの実装例 +// (1)リモートサービスのインタフェース定義ファイル + +// Clock.java Clockインタフェース +// このインタフェースは、ClockImplクラスのためのインタフェースです +// ClockImplをはじめとして、NetClockのRMI版に必須のインタフェースです + +// ライブラリの利用 +import java.rmi.Remote; +import java.rmi.RemoteException ; + +// Clockインタフェース +public interface Clock extends Remote{ + String putTime() throws RemoteException; +} diff --git a/src/j4/ClockClient.java b/src/j4/ClockClient.java new file mode 100644 index 0000000..b91b8c7 --- /dev/null +++ b/src/j4/ClockClient.java @@ -0,0 +1,28 @@ +package j4; + +// RMIによるNetClockプログラムの実装例 +// (5)クライアントプロセス実装のクラスファイル + +// ClockClient.java +// このクラスは、クライアントプロセスのクラスです +// NetClockのRMI版システムにおけるクライアントの機能を記述します +// RMIレジストリにおける名前の取得や、サーバへの仕事の依頼を行います +// 使用方法 +// java ClockClient +// なお、クライアント起動の前に、サーバとレジストリを起動してください + +// ライブラリの利用 +import java.rmi.Naming; + +// ClockClientクラス +public class ClockClient{ + // mainメソッド + public static void main(String args[]){ + try{ + Clock c = (Clock)Naming.lookup("//localhost/ClockService") ; + System.out.println(c.putTime()) ; + }catch(Exception e){ + System.out.println(e) ; + } + } +} \ No newline at end of file diff --git a/src/j4/ClockImpl.java b/src/j4/ClockImpl.java new file mode 100644 index 0000000..bf27788 --- /dev/null +++ b/src/j4/ClockImpl.java @@ -0,0 +1,30 @@ +package j4; + + +// RMIによるNetClockプログラムの実装例 +// (2)リモートサービスの実装のクラスファイル +// ClockImpl.java +// リモートサービスの実装クラスです +// NetClockのRMI版システムにおける機能を記述 +// 日時を返すメソッドputTime()を提供 + +// import java.io.* ; +import java.rmi.RemoteException ; +import java.rmi.server.UnicastRemoteObject ; +import java.util.Date ; + +/// Clockインタフェースの実装 +public class ClockImpl extends UnicastRemoteObject implements Clock +{ + private static final long serialVersionUID = -7586060351291888878L; + //----------------------------- + public ClockImpl() throws RemoteException{ // コンストラクタ + super(); + } + //----------------------------- + public String putTime(){ // 現在の時刻を返す + Date d = new Date() ; + // System.out.println(d); + return d.toString() ; + } +} // end of ClockImpl diff --git a/src/j4/ClockServer.java b/src/j4/ClockServer.java new file mode 100644 index 0000000..0b40561 --- /dev/null +++ b/src/j4/ClockServer.java @@ -0,0 +1,33 @@ +package j4; + +// RMIによるNetClockプログラムの実装例 +// (4)サーバプロセス実装のクラスファイル + +// ClockServer.java +// このクラスは、サーバプロセスのクラスです +// NetClockのRMI版システムにおけるサーバの機能を記述します +// RMIレジストリにおける名前の登録や、サーバの起動を行います +// 使用方法 +// java ClockServer +// なお、サーバ起動の前に、レジストリを起動してください +// RMIレジストリの起動は以下のようにします +// rmiregistry + +// ライブラリの利用 +import java.rmi.Naming; +// ClockServerクラス +public class ClockServer { + // コンストラクタ + public ClockServer(){ + try{ + Clock c = new ClockImpl() ; + Naming.rebind("//localhost/ClockService",c) ; + } catch(Exception e) { + System.out.println(e); + } + } + // mainメソッド + public static void main(String args[]){ + new ClockServer() ; + } +} diff --git a/src/j5/DPiClient.java b/src/j5/DPiClient.java new file mode 100644 index 0000000..c2aca42 --- /dev/null +++ b/src/j5/DPiClient.java @@ -0,0 +1,116 @@ +package j5; +// RMIによる分散処理プログラムの実装例 +// (5)クライアントプロセス実装のクラスファイル + +// DPiClient.java +// このクラスは、クライアントプロセスのクラスです +// 分散処理システムにおけるクライアントの機能を記述します +// RMIレジストリにおける名前の取得や、サーバへの仕事の依頼を行います +// 使用方法 +// java DPiClient サーバ名1 サーバ名2 ・・・ +// なお、クライアント起動の前に、サーバとレジストリを起動してください + +// ライブラリの利用 +import java.rmi.Naming; + +//DPiClientクラス +public class DPiClient{ + + // mainメソッド + public static void main(String args[]){ + long result=0 ;//サーバの計算結果を格納する + long millis ;//経過時間 + long maxloopcount=10000000 ;//点の個数 + int i ;//サーバの数 + + //スレッドを構成するための配列を宣言する + // DPiClientでは、サーバの数だけスレッドを作成します + // 各スレッドはサーバの計算処理終了を待ち、 + // 終了後Resultクラスのオブジェクトに結果を報告します + launchPiServer l[] = new launchPiServer[args.length]; + Thread t[] = new Thread [args.length]; + + //処理の本体 + try{ + //処理時間計測開始 + millis = System.currentTimeMillis() ; + //引数で指定されたサーバに処理を依頼します + for(i=0;i java Chat + +import java.net.*; +import java.io.*; +//============================================================================== +public class Chat { +// 受信スレッドを作成・実行し,送信を担当 + + final byte TTL = 1; //同一セグメント内部のみ到達可能とする. + final String MULTICASTADDRESS = ("224.0.0.1") ; + // マルチキャストアドレス224.0.0.1は, + // ルータを超えない場合のアドレス. + int port = 6000; + // チャット用のポート番号,指定がなければ6000 番とします + byte[] buff = new byte[1024]; //送信用バッファ + String myname ="" ; // 利用者名 + int nameLength = 0; //利用者名の長さ + MulticastSocket soc = null; // マルチキャストソケット + InetAddress chatgroup = null; //チャット用アドレス + + // コンストラクタ利用者名などを設定します + public Chat(int portno){ + port = portno ; //ポート番号の設定 + BufferedReader lineread + = new BufferedReader(new InputStreamReader(System.in)) ; + System.out.println("Input your name: ") ; + try{ + myname = lineread.readLine() ; + }catch(Exception e){ + e.printStackTrace() ; + System.exit(1) ; + } + System.out.println("Welcome" + myname + "!") ; + myname = myname + ">" ; + nameLength = (myname.getBytes()).length ; + for(int i = 0;i < nameLength;++i) + buff[i] = (myname.getBytes())[i] ; + } + +//----------------------------------------------------------------------------- + public void makeMulticastSocket() { + // MULTICASTADDRESSに対してマルチキャストソケットを作成. + try{ + chatgroup + = InetAddress.getByName(MULTICASTADDRESS) ; + soc = new MulticastSocket(port) ; + soc.joinGroup(chatgroup) ; + } + catch(Exception e){ + e.printStackTrace() ; + System.exit(1); + } + } // end of makeMulticastSocket +//----------------------------------------------------------------------------- + public void startListener() { + // スレッド用クラスListenPacketのオブジェクトを生成し,起動. + try{ + ListenPacket lisner = + new ListenPacket(soc); + Thread lisner_thread = new Thread(lisner); + lisner_thread.start();//受信スレッドの開始 + } + catch(Exception e){ + e.printStackTrace() ; + System.exit(1); + } + } // end of startListener +//----------------------------------------------------------------------------- + public void sendMsgs() { + // マルチキャストパケットの送信を担当. + try{ + // 送信ループ + while(true){ + int n = System.in.read( + buff,nameLength ,1024 - nameLength ) ; + if(n > 0){ + DatagramPacket dp + = new DatagramPacket( + buff,n + nameLength,chatgroup,port) ; + soc.send(dp) ; + } + else break ;// ループ終了 + } + } + catch(Exception e){ + e.printStackTrace() ; + System.exit(1); + } + } // end of sendMsgs +//----------------------------------------------------------------------------- + public void quitGroup() { + // 接続を終了 + try{ + System.out.println("Connection Closed.") ; + soc.leaveGroup(chatgroup) ; + System.exit(0) ; + } + catch(Exception e){ + e.printStackTrace() ; + System.exit(1); + } + } +//----------------------------------------------------------------------------- + public static void main(String[] arg){ + Chat c = null ; + int portno = 6000 ; +// if(arg.length >= 1) portno = Integer.parseInt(arg[0]) ; + c = new Chat(portno) ; + c.makeMulticastSocket() ; + c.startListener() ; + c.sendMsgs() ; + c.quitGroup() ; + } +} // end of class Chat +//============================================================================== +// ListenPacketクラス +// マルチキャストパケットを受信します +class ListenPacket implements Runnable { + MulticastSocket s = null; + // コンストラクタマルチキャストスレッドを受け取ります + public ListenPacket(MulticastSocket soc){ + s = soc; + } + // 処理の本体 + public void run(){ + byte[] buff = new byte[1024] ; + try{ + while(true){ + DatagramPacket recv + = new DatagramPacket(buff,buff.length) ; + s.receive(recv) ; + if(recv.getLength() > 0){ + System.out.write(buff,0,recv.getLength()) ; + } + } + }catch(Exception e){ + e.printStackTrace() ; + System.exit(1) ; + } + } +} // end of class ListenPacket diff --git a/src/j6/ChatServer.java b/src/j6/ChatServer.java new file mode 100644 index 0000000..129cb8d --- /dev/null +++ b/src/j6/ChatServer.java @@ -0,0 +1,119 @@ +package j6; + +// チャットサーバChatServer.java +// このプログラムは,チャットのサーバプログラムです +// 使い方java ChatServer [ポート番号] +// ポート番号を省略すると,ポート番号6000 番を使います +// 起動の例java ChatServer +// 終了にはコントロールC を入力してください + +// このサーバへの接続にはTelnet.javaなどを使ってください +// 接続を止めたいときには,行頭でquitと入力してください + +// ライブラリの利用 +import java.io.*; +import java.net.*; +import java.util.*; + +// ChatServerクラス +public class ChatServer { + static final int DEFAULT_PORT = 6000;//ポート番号省略時は6000 番を使います + static ServerSocket serverSocket; + static Vector connections; + + // sendAllメソッド + // 各クライアントにメッセージを送ります + public static void sendAll(String t){ + if (connections != null){// コネクションがあれば実行します + for (Socket s : connections) { + try { + PrintWriter pw = new PrintWriter(s.getOutputStream()); + pw.println(t); + pw.flush(); + }catch (IOException ex){} + } + } + System.out.println(t); + } + + // addConnectionメソッド + // クライアントとの接続を追加します + public static void addConnection(Socket s){ + if (connections == null){ + connections = new Vector(); + } + connections.addElement(s); + } + + // deleteConnectionメソッド + // あるクライアントとのコネクションを削除します + public static void deleteConnection(Socket s){ + if (connections != null){ + connections.removeElement(s); + } + } + + // mainメソッド + // サーバソケットを作り,クライアントからの接続を待ち受けます + public static void main(String[] arg){ + int port = DEFAULT_PORT ; + if (arg.length > 0) port = Integer.parseInt(arg[0]) ; + try { + serverSocket = new ServerSocket(port); + }catch (IOException e){ + System.err.println(e); + System.exit(1); + } + while (true) { + try { + Socket cs = serverSocket.accept(); + addConnection(cs); + Thread ct = new Thread(new clientProc(cs)); + ct.start(); + }catch (IOException e){ + System.err.println(e); + } + } + } +} + +// clientProcクラス +// クライアント処理用スレッドのひな形です +class clientProc implements Runnable { + Socket s; + BufferedReader in; + PrintWriter out; + String name = null; + ChatServer server = null ; + + //コンストラクタ + public clientProc(Socket s) throws IOException { + this.s = s; + in = new BufferedReader(new InputStreamReader( + s.getInputStream())); + out = new PrintWriter(s.getOutputStream()); + } + + // スレッドの本体 + // 各クライアントとの接続処理を行います + public void run(){ + try { + while (name == null){ + out.print("お名前は?: "); + out.flush(); + name = in.readLine(); + } + String line = in.readLine(); + while (!"quit".equals(line)){ + ChatServer.sendAll(name + "> " +line); + line = in.readLine(); + } + ChatServer.deleteConnection(s); + s.close(); + }catch (IOException e){ + try { + s.close(); + }catch (IOException e2){} + } + } +} \ No newline at end of file diff --git a/src/j6/T1.java b/src/j6/T1.java new file mode 100644 index 0000000..1b86a39 --- /dev/null +++ b/src/j6/T1.java @@ -0,0 +1,101 @@ +package j6; + +// telnetの原形となるプログラムT1.java +// このプログラムは,指定されたアドレスのポートに標準入出力を接続します +// 使い方java T1 サーバアドレスポート番号 +// 起動の例java T1 kiku.fuis.fukui-u.ac.jp 80 +// 終了にはコントロールC を入力してください + +// ライブラリの利用 +import java.net.*; +import java.io.*; + +// T1クラス +// T1クラスは,ネットワーク接続の管理を行います +// StreamConnectorクラスを用いてスレッド処理を行います +public class T1 { + // ソケットの準備 + protected Socket serverSocket;//接続用ソケット + public OutputStream serverOutput;//ネットワーク出力用ストリーム + public BufferedInputStream serverInput;// 同入力用ストリーム + + // openConnectionメソッド + //アドレスとポート番号からソケットを作りストリームを作成します + public void openConnection(String host,int port) + throws IOException,UnknownHostException + { + serverSocket = new Socket(host, port); + serverOutput = serverSocket.getOutputStream(); + serverInput + = new BufferedInputStream(serverSocket.getInputStream()); + } + + // main_procメソッド + // ネットワークとのやりとりをするスレッドをスタートさせます + public void main_proc() + throws IOException + { + try { + // スレッド用クラスStreamConnectorのオブジェクトを生成します + StreamConnector stdin_to_socket = + new StreamConnector(System.in, serverOutput); + StreamConnector socket_to_stdout = + new StreamConnector(serverInput, System.out); + // スレッドを生成します + Thread input_thread = new Thread(stdin_to_socket); + Thread output_thread = new Thread(socket_to_stdout); + // スレッドを起動します + input_thread.start(); + output_thread.start(); + } + catch(Exception e){ + System.err.print(e); + System.exit(1); + } + } + + // mainメソッド + // TCP コネクションを開いて処理を開始します + public static void main(String[] arg){ + try { + T1 t = null; + t = new T1(); + t.openConnection(arg[0], Integer.parseInt(arg[1])); + t.main_proc(); + }catch(Exception e){ + e.printStackTrace(); + System.exit(1); + } + } +} + +// StreamConnectorクラス +// ストリームを受け取り,両者を結合してデータを受け渡します +// StreamConnectorクラスはスレッドを構成するためのクラスです + +class StreamConnector implements Runnable { + InputStream src = null; + OutputStream dist = null; + // コンストラクタ入出力ストリームを受け取ります + public StreamConnector(InputStream in, OutputStream out){ + src = in; + dist = out; + } + // 処理の本体 + // ストリームの読み書きを無限に繰り返します + public void run(){ + byte[] buff = new byte[1024]; + while (true) { + try { + int n = src.read(buff); + if (n > 0) + dist.write(buff, 0, n); + } + catch(Exception e){ + e.printStackTrace(); + System.err.print(e); + System.exit(1); + } + } + } +} \ No newline at end of file diff --git a/src/j7/SeaGameClient.java b/src/j7/SeaGameClient.java new file mode 100644 index 0000000..0733896 --- /dev/null +++ b/src/j7/SeaGameClient.java @@ -0,0 +1,279 @@ +package j7; +// File: SeaGameClient.java +// 海ゲームのクライアントプログラム +// Usage: java SeaGameClient +// 起動してloginボタンを押すと,接続先サーバの名前や利用者の名前を問い合わせてくる +// サーバ名と利用者名を入力する +// 続いてOK ボタンを押すと,ポート番号10000 番でサーバと接続 +// +// プログラムを停止するにはlogout ボタンを押す + +import java.awt.*; // グラフィックス +import java.awt.event.*;// イベント関連 +import java.net.*; // ネットワーク関連 +import java.io.*; +import java.util.*; + +public class SeaGameClient implements Runnable { + Frame f;// クライアント情報表示用ウィンドウ + Panel p;// 上下左右の移動ボタンと海の状態を表示するパネル + Canvas c;// 海の状態を表示するキャンバス + + //--------------------------------------------------------------------- + // GUI 画面の初期配置 + public SeaGameClient () { + Button b; + f = new Frame();//クライアント情報ウィンドウ全体の表示 + p = new Panel();//海表示部分と操作ボタンの表示 + p.setLayout(new BorderLayout()); + + // upボタンの作成 + b = new Button("up"); + b.addActionListener(new ActionListener(){ + // upボタンが押されたらupコマンドを送信 + public void actionPerformed(ActionEvent e){ + sendCommand("up"); + } + }); + p.add(b, BorderLayout.NORTH); + + // leftボタンの作成 + b = new Button("left"); + b.addActionListener(new ActionListener(){ + // leftボタンが押されたらleftコマンドを送信 + public void actionPerformed(ActionEvent e){ + sendCommand("left"); + } + }); + p.add(b, BorderLayout.WEST); + + // rightボタンの作成 + b = new Button("right"); + b.addActionListener(new ActionListener(){ + // rightボタンが押されたらrightコマンドを送信 + public void actionPerformed(ActionEvent e){ + sendCommand("right"); + } + }); + p.add(b, BorderLayout.EAST); + + // downボタンの作成 + b = new Button("down"); + b.addActionListener(new ActionListener(){ + // downボタンが押されたらdownコマンドを送信 + public void actionPerformed(ActionEvent e){ + sendCommand("down"); + } + }); + p.add(b, BorderLayout.SOUTH); + + // 海上の様子を表示するCanvasを作成 + c = new Canvas(); + c.setSize(256,256);// 大きさの設定 + // フレームに必要な部品の取り付け + p.add(c); + f.add(p); + + // フレームfにloginボタンの取り付け + b = new Button("login"); + b.addActionListener(new ActionListener(){ + public void actionPerformed(ActionEvent e){ + // loginボタンが押された場合の処理 + // サーバがセットされていなければlogin処理 + if(server == null) login(); + } + }); + f.add(b,BorderLayout.NORTH); + + // フレームfにlogoutボタンの取り付け + b = new Button("logout"); + b.addActionListener(new ActionListener(){ + public void actionPerformed(ActionEvent e){ + logout(); + } + }); + f.add(b,BorderLayout.SOUTH); + + // フレームfを表示します + f.setSize(335,345); + f.setVisible(true); + } // end of SeaGameClient + //-------------------------------------------------------------------- + // runメソッド/ 500ミリ秒ごとに画面を更新 + public void run(){ + while (true){ + try { + Thread.sleep(500); + }catch(Exception e){ + } + // repainメソッドを用いて,サーバ上の情報を画面に出力します + repaint(); + } + } // end of run + //--------------------------------------------------------------------- + // login処理関連のオブジェクト + int sx = 100; + int sy = 100; + TextField host, tf_name; + Dialog d; + + //--------------------------------------------------------------------- + // loginウィンドウを表示し,必要な情報を得る + // 実際のlogin処理は,realLoginメソッド + void login(){ + // ウィンドウの表示とデータの入力 + d = new Dialog(f, true); + host = new TextField(10) ; + tf_name = new TextField(10) ; + d.setLayout(new GridLayout(3,2)); + d.add(new Label("host:")); + d.add(host); + d.add(new Label("name:")); + d.add(tf_name); + Button b = new Button("OK"); + b.addActionListener(new ActionListener(){ + // 入力が完了したら,readlLoginメソッドを使ってサーバにloginします + public void actionPerformed(ActionEvent e){ + realLogin(host.getText(), tf_name.getText()); + d.dispose(); + } + }); + d.add(b); + d.setResizable(true); + d.setSize(200, 150); + d.setVisible(true); + (new Thread(this)).start(); + } // end of login + //--------------------------------------------------------------------- + // realLogin関連のオブジェクト + Socket server;// ゲームサーバとの接続ソケット + int port = 10000; // 接続ポート + BufferedReader in; // 入力ストリーム + PrintWriter out; // 出力ストリーム + String name; // ゲーム参加者の名前 + + //--------------------------------------------------------------------- + // サーバへのログイン処理 + void realLogin(String host, String name){ + try { + // サーバとの接続 + this.name = name; + server = new Socket(host, port); + in = new BufferedReader(new InputStreamReader( + server.getInputStream())); + out = new PrintWriter(server.getOutputStream()); + + // loginコマンドの送付 + out.println("login " + name); + out.flush(); + repaint(); + } catch(Exception e) { + e.printStackTrace(); + System.exit(1); + } + } // end of realLogin + //--------------------------------------------------------------------- + // サーバからのログアウト + void logout(){ + try { + // logoutコマンドの送付 + out.println("logout"); + out.flush(); + server.close(); + }catch (Exception e){ + ; + } + System.exit(0); + } // end of logout + //--------------------------------------------------------------------- + // サーバからゲームの情報を得て,クライアントの画面再描画 + void repaint(){ + // サーバにstatコマンドを送付し,盤面の様子などの情報を取得 + out.println("stat"); + out.flush(); + + try { + String line = in.readLine();// サーバからの入力の読み込み + Graphics g = c.getGraphics();// Canvas cに海の様子を表示 + + // 海の描画 (青い四角形) + g.setColor(Color.blue); + g.fillRect(0, 0, 256, 256); + + //ship_infoから始まる船の情報の先頭行を探す + while (!"ship_info".equalsIgnoreCase(line)) + line = in.readLine(); + + // 船の情報ship_infoの表示 + // ship_infoはピリオドのみの行で終了 + line = in.readLine(); + while (!".".equals(line)){ + StringTokenizer st = new StringTokenizer(line); + String obj_name = st.nextToken().trim(); // 名前を読み取ります + + + // 自分の船は赤(red)でし,他人の船は緑(green)で表示 + if (obj_name.equals(name)) //自分の船 + g.setColor(Color.red); + else // 他人の船 + g.setColor(Color.green); + + // 船の位置座標を読み取る + int x = Integer.parseInt(st.nextToken()) ; + int y = Integer.parseInt(st.nextToken()) ; + + + g.fillOval(x - 10, 256 - y - 10, 20, 20); // 船を表示 + g.drawString(st.nextToken(),x+10,256-y+10) ; // 得点を船の右下に表示 + g.drawString(obj_name,x+10,256-y-10) ; // 名前を船の右上に表示 + + line = in.readLine(); + } // end while (!".".equals(line)) + + // energy_infoから始まる,燃料タンクの情報の待ち受け + while (!"energy_info".equalsIgnoreCase(line)) + line = in.readLine(); + + // 燃料タンクの情報energy_infoの表示 + line = in.readLine(); + while (!".".equals(line)){ // ピリオドのみの行でない間,繰り返す + StringTokenizer st = new StringTokenizer(line); + + // 燃料タンクの位置座標を読み取る + int x = Integer.parseInt(st.nextToken()) ; + int y = Integer.parseInt(st.nextToken()) ; + + // 燃料タンクを白抜きの赤丸で示す + g.setColor(Color.red); + g.fillOval(x - 5, 256 - y - 5, 10, 10); + g.setColor(Color.white); + g.fillOval(x - 3, 256 - y - 3, 6, 6); + + line = in.readLine(); + } // end while (!".".equals(line)) + } catch (Exception e) { + e.printStackTrace(); + System.exit(1); + } + } // end of repaint + //--------------------------------------------------------------------- + // サーバへコマンドを送信 + void sendCommand(String s){ + if ("up".equals(s)){ + out.println("up"); + }else if ("down".equals(s)){ + out.println("down"); + }else if ("left".equals(s)){ + out.println("left"); + }else if ("right".equals(s)){ + out.println("right"); + } + out.flush(); + } + + // mainメソッド + // SeaGameClientを起動します + public static void main(String[] arg){ + new SeaGameClient(); + } +} // end of class SeaGameClient diff --git a/src/j7/SeaGameServer.java b/src/j7/SeaGameServer.java new file mode 100644 index 0000000..31beed4 --- /dev/null +++ b/src/j7/SeaGameServer.java @@ -0,0 +1,320 @@ +package j7; + +// 「海ゲーム」サーバプログラムSeaGameServer.java +// このプログラムは,海ゲームのサーバプログラムです +// 使い方java SeaGameServer +// 起動すると,ポート番号10000 番に対するクライアントからの接続を待ちます +// プログラムを停止するにはコントロールC を入力してください +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStreamReader; +import java.io.PrintWriter; +import java.net.ServerSocket; +import java.net.Socket; +import java.util.Hashtable; +import java.util.Random; +import java.util.StringTokenizer; +import java.util.Vector; + +public class SeaGameServer { + static final int DEFAULT_PORT = 10000; + //SeaGameServer接続用ポート番号 + static ServerSocket serverSocket; + static Vector connections; + //クライアントとのコネクションを保持するVectorオブジェクト + static Vector energy_v; // 燃料タンクの位置情報リスト + static Hashtable userTable = null; + // クライアント関連情報登録用テーブル + static Random random = null; + + // addConnectionメソッド + // クライアントとの接続をVectorオブジェクトconnectionsに登録します + public static void addConnection(Socket s){ + if (connections == null){//初めてのコネクションの場合は, + connections = new Vector();// connectionsを作成します + } + connections.addElement(s); + } + + // deleteConnectionメソッド + // クライアントとの接続をconnectionsから削除します + public static void deleteConnection(Socket s){ + if (connections != null){ + connections.removeElement(s); + } + } + + // loginUserメソッド + // loginコマンドの処理として,利用者の名前や船の位置を登録します + public static void loginUser(String name){ + if (userTable == null){// 登録用テーブルがなければ作成します + userTable = new Hashtable(); + } + if (random == null){// 乱数の準備をします + random = new Random(); + } + // 船の初期位置を乱数で決定します + int ix = Math.abs(random.nextInt()) % 256; + int iy = Math.abs(random.nextInt()) % 256; + + // クライアントの名前や船の位置を表に登録します + userTable.put(name, new Ship(ix, iy)); + // サーバ側の画面にもクライアントの名前を表示します + System.out.println("login:" + name); + System.out.flush(); + } + + // logoutUserメソッド + // クライアントのログアウトを処理します + public static void logoutUser(String name){ + // サーバ側画面にログアウトするクライアントの名前を表示します + System.out.println("logout:" + name); + System.out.flush(); + // 登録用テーブルから項目を削除します + userTable.remove(name); + } + + // leftメソッド + // ある特定の船を左に動かして,燃料タンクが拾えるかどうか判定します + // 判定にはcalculationメソッドを使います + public static void left(String name){ + Ship ship = (Ship) userTable.get(name); + ship.left(); + calculation(); + } + + // rightメソッド + // ある特定の船を右に動かして,燃料タンクが拾えるかどうか判定します + // 判定にはcalculationメソッドを使います + public static void right(String name){ + Ship ship = (Ship) userTable.get(name); + ship.right(); + calculation(); + } + + // upメソッド + // ある特定の船を上に動かして,燃料タンクが拾えるかどうか判定します + // 判定にはcalculationメソッドを使います + public static void up(String name){ + Ship ship = (Ship) userTable.get(name); + ship.up(); + calculation(); + } + + // downメソッド + // ある特定の船を下に動かして,燃料タンクが拾えるかどうか判定します + // 判定にはcalculationメソッドを使います + public static void down(String name){ + Ship ship = (Ship) userTable.get(name); + ship.down(); + calculation(); + } + + // calculationメソッド + // 燃料タンクと船の位置関係を調べて,燃料タンクが拾えるかどうか判定します + static void calculation(){ + if (userTable != null && energy_v != null){ + // すべてのクライアントについて判定します + for (String user : userTable.keySet()) { + // 判定するクライアントの名前と船の位置を取り出します + Ship ship = (Ship) userTable.get(user); + // 燃料タンクすべてについて,船との位置関係を調べます + for (int[] e : energy_v) { + // 燃料タンクの位置と船の位置を調べ,距離を計算します + int x = e[0] - ship.x; + int y = e[1] - ship.y; + double r = Math.sqrt(x * x + y * y); + // 距離"10"以内なら燃料タンクを取り込みます + if (r < 10) { + energy_v.removeElement(e); + ship.point++; + } + } + } + } + } + //--------------------------------------------------------------------- + // STATコマンドを処理 + // クライアントに船の情報(ship_info)と, + // 海上を漂流している燃料タンクの情報を(energy_info)を送信 + public static void statInfo(PrintWriter pw){ + // 船の情報(ship_info)の送信 + pw.println("ship_info"); + if (userTable != null){ + for (String user : userTable.keySet()) { + Ship ship = (Ship) userTable.get(user); + pw.println(user + " " + ship.x + " " + + ship.y + " " + ship.point); + } + } + pw.println(".");// ship_infoの終了 + // 燃料タンクの情報(energy_info)の送信 + pw.println("energy_info"); + if (energy_v != null){ + // すべての燃料タンクの位置情報をクライアントに送信します + for (int[] e : energy_v) { + pw.println(e[0] + " " + e[1]); + } + } + pw.println(".");// enegy_infoの終了 + pw.flush(); + } + //--------------------------------------------------------------------- + // 燃料タンクを1つだけ海上にランダムに配置 + public static void putEnergy(){ + if (energy_v == null){// 初めて配置する場合の処理 + energy_v = new Vector(); + } + if (random == null){// 初めて乱数を使う場合の処理 + random = new Random(); + } + // 乱数で位置を決めて海上に配置します + int[] e = new int[2]; + e[0] = Math.abs(random.nextInt()) % 256; + e[1] = Math.abs(random.nextInt()) % 256; + + energy_v.addElement(e); + } // end of putEnergy + //--------------------------------------------------------------------- + // サーバソケットの作成とクライアント接続の処理 + // および適当なタイミングでの燃料タンクの逐次追加処理 + public static void main(String[] arg){ + try {// サーバソケットの作成 + serverSocket = new ServerSocket(DEFAULT_PORT); + }catch (IOException e){ + System.err.println("can't create server socket."); + System.exit(1); + } + // 燃料タンクを順に追加するスレッドetを作ります + Thread et = new Thread(){ + public void run(){ + while(true){ + try { + sleep(10000);// スレッドetを10000ミリ秒休止させます + }catch(InterruptedException e){ + break; + } + // 海上に1つ燃料タンクを配置します + SeaGameServer.putEnergy(); + } + } + }; + // etをスタートします + et.start(); + // ソケットの受付と,クライアント処理プログラムの開始処理を行います + while (true) {// 無限ループ + try { + Socket cs = serverSocket.accept(); + addConnection(cs);// コネクションを登録します + // クライアント処理スレッドを作成します + Thread ct = new Thread(new clientProc(cs)); + ct.start(); + }catch (IOException e){ + System.err.println("client socket or accept error."); + } + } + } // end of main +} // end of class putEnergy +//============================================================================= +class clientProc implements Runnable { + Socket s; // クライアント接続用ソケット + BufferedReader in; // 入力ストリーム + PrintWriter out; // 出力ストリーム + String name = null; // クライアントの名前 + + //-------------------------------------------------------------------- + // ソケットを使って入出力ストリームを作成します + public clientProc(Socket s) throws IOException { + this.s = s; + in = new BufferedReader(new InputStreamReader( + s.getInputStream())); + out = new PrintWriter(s.getOutputStream()); + } + //-------------------------------------------------------------------- + public void run(){ + try { + //LOGOUTコマンド受信まで繰り返します + while (true) { + // クライアントからの入力を読み取ります + String line = in.readLine(); + // nameが空の場合にはLOGINコマンドのみを受け付けます + if (name == null){ + StringTokenizer st = new StringTokenizer(line); + String cmd = st.nextToken(); + if ("login".equalsIgnoreCase(cmd)){ + name = st.nextToken(); + SeaGameServer.loginUser(name); + }else{ + // LOGINコマンド以外は無視 + } + }else{ + // nameが空でない場合はログイン済み.コマンドを受け付ける. + StringTokenizer st = new StringTokenizer(line); + String cmd = st.nextToken();// コマンドの取り出し + // コマンドに対応する処理 + if ("STAT".equalsIgnoreCase(cmd)){ + SeaGameServer.statInfo(out); + } else if ("UP".equalsIgnoreCase(cmd)){ + SeaGameServer.up(name); + } else if ("DOWN".equalsIgnoreCase(cmd)){ + SeaGameServer.down(name); + } else if ("LEFT".equalsIgnoreCase(cmd)){ + SeaGameServer.left(name); + } else if ("RIGHT".equalsIgnoreCase(cmd)){ + SeaGameServer.right(name); + } else if ("LOGOUT".equalsIgnoreCase(cmd)){ + SeaGameServer.logoutUser(name); + // LOGOUTコマンドの場合には繰り返しを終了 + break; + } + } + } + // 登録情報を削除し,接続を切断 + SeaGameServer.deleteConnection(s); + s.close(); + } catch (IOException e) { + try { + s.close(); + } catch (IOException e2){} + } + } // end of run +} // enf of class clientProc +//============================================================================= +// 船の位置と,獲得した燃料タンクの数を管理 +class Ship { + + int x, y; // 船の位置座標 + int point = 0; // 獲得した燃料タンクの個数 + + //------------------------------------------------------------------- + // 初期位置をセットします + public Ship(int x, int y){ + this.x = x; + this.y = y; + } + //------------------------------------------------------------------- + // 船を左に動かす + public void left(){ + x -= 10; + if (x < 0) x += 256; // 左の辺は右の辺につながっている + } + //------------------------------------------------------------------- + // 船を右に動かす + public void right(){ + x += 10; + x %= 256; // 右の辺は左の辺につながっている + } + //------------------------------------------------------------------- + // 船を上に動かす + public void up(){ + y += 10; + y %= 256; // 上の辺は下の辺につながっている + } + //------------------------------------------------------------------- + // 船を下に動かす + public void down(){ + y -= 10; + if (y < 0) y += 256; // 下の辺は上の辺につながっている + + } +} // end of class Ship diff --git a/src/output.txt b/src/output.txt new file mode 100644 index 0000000..4db2703 --- /dev/null +++ b/src/output.txt @@ -0,0 +1,2 @@ + +asfd diff --git a/src/run1-1.sh b/src/run1-1.sh new file mode 100755 index 0000000..555d814 --- /dev/null +++ b/src/run1-1.sh @@ -0,0 +1,22 @@ +#!/bin/bash + +# javac によるコンパイル +javac j1/Hello.java + +# java によるHello.class の実行 (ファイル名の .classは省略する) +java j1/Hello + +# Java Class fileの中身を見てみる +hexdump -C j1/Hello.class + +# 一旦シェルスクリプトの実行をストップ。 +echo "type Return(Enter) to run next command / CTRL+C to quit" +read input_str + +# javac によるコンパイル +javac j1/HelloNW.java + +# java コマンドによるHelloNW.class の実行 +java j1/HelloNW + + diff --git a/src/run1-2.sh b/src/run1-2.sh new file mode 100755 index 0000000..9d5cc50 --- /dev/null +++ b/src/run1-2.sh @@ -0,0 +1,14 @@ +#!/bin/bash + +javac j1/PrintArgs.java + +java j1/PrintArgs ABC 123 + +echo "type return to continue / CTRL+C to quit" +read input_str + +javac j1/TestThread.java + +java j1/TestThread + + diff --git a/src/run1-3.sh b/src/run1-3.sh new file mode 100755 index 0000000..c6ba8a5 --- /dev/null +++ b/src/run1-3.sh @@ -0,0 +1,30 @@ +#!/bin/bash + +javac j1/ReadFile.java + +java j1/ReadFile j1/ReadFile.java + +echo "type return to continue / CTRL+C to quit" +read input_str + +javac j1/WriteFile.java + +head -n 10 j1/WriteFile.java +echo "type return to continue / CTRL+C to quit" +read input_str + +java j1/WriteFile output.txt +sleep 2 +echo "出力ファイルの中身を表示します" +cat output.txt + + +echo "type return to continue / CTRL+C to quit" +read input_str + +javac j1/ReadWrite.java + +echo "なにか入力してEnterを押すと、エコーバックされる。CTRL+C to quit" +java j1/ReadWrite + + diff --git a/src/run2-1.sh b/src/run2-1.sh new file mode 100755 index 0000000..a08441f --- /dev/null +++ b/src/run2-1.sh @@ -0,0 +1,13 @@ +#!/bin/bash + +javac j2/*.java + +echo "Now, open other terminal, and execute Client (ReadNet)" +echo ">>>> $ java j2/ReadNet"; +echo " ^^^^^^^^^^^^^^^"; +echo "start Server (NetClock)" +java j2/EchoServer + +#echo "type return to exit" +#read input_str +