diff --git a/src/j2/EchoClient.java b/src/j2/EchoClient.java index 67f5782..81ca094 100644 --- a/src/j2/EchoClient.java +++ b/src/j2/EchoClient.java @@ -9,16 +9,19 @@ public class EchoClient { public static void main(String[] args) { // ソケットや入出力用のストリームの宣言 - Socket echoSocket = null; - DataOutputStream os = null; - BufferedReader is = null; + Socket socket = null; + DataOutputStream dos = null; + BufferedReader isbr = null; + BufferedReader inputbr = null; + BufferedReader stdinbr = null; // ポート9999番を開く String host = "localhost"; try { - echoSocket = new Socket(host, 9999); - os = new DataOutputStream(echoSocket.getOutputStream()); - is = new BufferedReader(new InputStreamReader(echoSocket.getInputStream())); + socket = new Socket(host, 9999); + dos = new DataOutputStream(socket.getOutputStream()); + isbr = new BufferedReader(new InputStreamReader(socket.getInputStream())); + stdinbr = new BufferedReader(new InputStreamReader(System.in)); } catch (UnknownHostException e) { System.err.println("Don't know about host: "+host); } catch (IOException e) { @@ -26,26 +29,29 @@ } // サーバーにメッセージを送る - if (echoSocket != null && os != null && is != null) { + if (socket != null && dos != null && isbr != null) { try { // メッセージを送ります - os.writeBytes("HELLO\n"); + dos.writeBytes("HELLO\n"); for(String a: args) { - os.writeBytes(a+"\n"); + dos.writeBytes(a+"\n"); } -// os.flush(); // サーバーからのメッセージを受け取り画面に表示します String line; - while ((line = is.readLine()) != null) { + while ((line = isbr.readLine()) != null) { System.out.println("Server: " + line); if (line.equals("quit")) break; + + String s = stdinbr.readLine(); + System.out.println(s); + dos.writeBytes(s+"\n"); } // 開いたソケットなどをクローズ - os.close(); - is.close(); - echoSocket.close(); + dos.close(); + isbr.close(); + socket.close(); } catch (UnknownHostException e) { System.err.println("Trying to connect to unknown host: " + e); } catch (IOException e) { diff --git a/src/j2/ThreadEchoClient.java b/src/j2/ThreadEchoClient.java index ccf9e2a..90387ae 100644 --- a/src/j2/ThreadEchoClient.java +++ b/src/j2/ThreadEchoClient.java @@ -9,18 +9,18 @@ public class ThreadEchoClient { public static void main(String[] args) { // ソケットや入出力用のストリームの宣言 - Socket echoSocket = null; - DataOutputStream os = null; - BufferedReader is = null; - BufferedReader inputbr = null; + Socket socket = null; + DataOutputStream dos = null; + BufferedReader isbr = null; + BufferedReader stdinbr = null; // ポート9999番を開く String host = "localhost"; try { - echoSocket = new Socket(host, 9999); - os = new DataOutputStream(echoSocket.getOutputStream()); - is = new BufferedReader(new InputStreamReader(echoSocket.getInputStream())); - inputbr = new BufferedReader(new InputStreamReader(System.in)); + socket = new Socket(host, 9999); + dos = new DataOutputStream(socket.getOutputStream()); + isbr = new BufferedReader(new InputStreamReader(socket.getInputStream())); + stdinbr = new BufferedReader(new InputStreamReader(System.in)); } catch (UnknownHostException e) { System.err.println("Don't know about host: "+host); } catch (IOException e) { @@ -28,25 +28,25 @@ } // サーバーにメッセージを送る - if (echoSocket != null && os != null && is != null) { + if (socket != null && dos != null && isbr != null) { try { // メッセージを送ります - os.writeBytes("HELLO\n"); + dos.writeBytes("HELLO\n"); // サーバーからのメッセージを受け取り画面に表示します String line; - while ((line = is.readLine()) != null) { + while ((line = isbr.readLine()) != null) { System.out.println("Server: " + line); if (line.equals("quit")) break; - String s = inputbr.readLine(); + String s = stdinbr.readLine(); System.out.println(s); - os.writeBytes(s+"\n"); + dos.writeBytes(s+"\n"); } // 開いたソケットなどをクローズ - os.close(); - is.close(); - echoSocket.close(); + dos.close(); + isbr.close(); + socket.close(); } catch (UnknownHostException e) { System.err.println("Trying to connect to unknown host: " + e); } catch (IOException e) {