fork download
  1. // クライアントプログラム
  2.  
  3. import java.io.*;
  4. import java.util.*;
  5. import java.net.*;
  6. import javax.swing.*;
  7.  
  8. class MessageWindow extends JFrame {
  9. JTextArea textArea;
  10. MessageWindow() {
  11. super("Messages");
  12. textArea = new JTextArea(25, 80);
  13. textArea.setEditable(false);
  14. add(textArea);
  15. pack();
  16. setVisible(true);
  17. }
  18.  
  19. public void printMessage(String message) {
  20. textArea.append(message+"\n");
  21. }
  22. }
  23.  
  24.  
  25. public class ChatCl {
  26.  
  27. byte[] buf = new byte[ChatParam.MSIZE];
  28. byte[] send = new byte[ChatParam.MSIZE];
  29. byte[] cbuf;
  30. String str;
  31.  
  32. public static void main(String[] args) {
  33. try {
  34. new ChatCl().start(args);
  35. } catch(Exception e) {
  36. System.out.println(e);
  37. }
  38. }
  39.  
  40. public void start(String[] args) {
  41.  
  42. if (args.length < 2) {
  43. System.out.println("引数: サーバ名 ユーザ名");
  44. System.exit(1);
  45. }
  46.  
  47. try {
  48. /* サーバへの接続処理 */
  49. String hostName = args[0];
  50. Socket sock = new Socket(hostName, ChatParam.PORT);
  51. String name = args[1];
  52. InputStream is = sock.getInputStream();
  53. OutputStream os = sock.getOutputStream();
  54.  
  55. System.out.println("[Input]----------------------------------");
  56.  
  57. MessageWindow mwin = new MessageWindow();
  58.  
  59. Thread messageReceiveProcess = new Thread(new Runnable(){
  60. @Override
  61. public void run() {
  62. try {
  63. while(true){
  64. // メッセージを受信して、bufの中に保存
  65. int count = is.read(buf);
  66. if (count > 0) {
  67. switch (buf[0]) {
  68. case ChatParam.LEAVE:
  69. str = new String(buf, 1, ChatParam.MSIZE - 1);
  70. mwin.printMessage(str.substring(0, str.indexOf(0)));
  71. break;
  72. case ChatParam.FULL:
  73. System.out.println("現在人数制限に達しています");
  74. break;
  75. case ChatParam.JOIN:
  76. case ChatParam.MSG:
  77. str = new String(buf, 1, ChatParam.MSIZE - 1);
  78. mwin.printMessage(str.substring(0, str.indexOf(0)));
  79. }
  80. }
  81. }
  82. }catch(Exception e){
  83. System.out.println(e);
  84. }
  85. }
  86. });
  87.  
  88. // 最初に参加要求(+ユーザ名)の送信
  89. send[0] = ChatParam.JOIN;
  90. cbuf = name.getBytes();
  91. System.arraycopy(cbuf, 0, send, 1, (cbuf.length < ChatParam.NSIZE)?cbuf.length:ChatParam.NSIZE);
  92. os.write(send);
  93.  
  94. // 以下は他の参加者からのメッセージを受信する部分ですが、現状は
  95. // flagをfalseにして処理をスキップしています。
  96. messageReceiveProcess.start();
  97.  
  98. // 以下の部分はメッセージを送信する部分で、他の参加者からのメッセージ
  99. // を受信する部分とは並列にする必要があります。
  100. while (true) {
  101. send[0] = ChatParam.MSG;
  102. str = name + ":" + input.readLine() + "\0";
  103. // mwin.printMessage(str);
  104. cbuf = str.getBytes();
  105. System.arraycopy(cbuf, 0, send, 1, (cbuf.length < ChatParam.MSIZE - 1)?cbuf.length:(ChatParam.MSIZE - 1));
  106.  
  107. /* send内のデータを送信 */
  108. os.write(send);
  109. }
  110.  
  111. /* 通信終了処理 */
  112. // sock.close();
  113.  
  114. } catch (Exception e) {
  115. System.out.println("connection error");
  116. System.exit(1);
  117. }
  118. }
  119. }
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
Main.java:25: error: class ChatCl is public, should be declared in a file named ChatCl.java
public class ChatCl {
       ^
Main.java:27: error: cannot find symbol
  byte[] buf = new byte[ChatParam.MSIZE];
                        ^
  symbol:   variable ChatParam
  location: class ChatCl
Main.java:28: error: cannot find symbol
  byte[] send = new byte[ChatParam.MSIZE];
                         ^
  symbol:   variable ChatParam
  location: class ChatCl
Main.java:50: error: cannot find symbol
      Socket sock = new Socket(hostName, ChatParam.PORT);
                                         ^
  symbol:   variable ChatParam
  location: class ChatCl
Main.java:69: error: cannot find symbol
                  case ChatParam.LEAVE:
                       ^
  symbol: variable ChatParam
Main.java:70: error: cannot find symbol
                    str = new String(buf, 1, ChatParam.MSIZE - 1);
                                             ^
  symbol: variable ChatParam
Main.java:73: error: cannot find symbol
                  case ChatParam.FULL:
                       ^
  symbol: variable ChatParam
Main.java:76: error: cannot find symbol
                  case ChatParam.JOIN:
                       ^
  symbol: variable ChatParam
Main.java:77: error: cannot find symbol
                  case ChatParam.MSG:
                       ^
  symbol: variable ChatParam
Main.java:78: error: cannot find symbol
                    str = new String(buf, 1, ChatParam.MSIZE - 1);
                                             ^
  symbol: variable ChatParam
Main.java:90: error: cannot find symbol
      send[0] = ChatParam.JOIN;
                ^
  symbol:   variable ChatParam
  location: class ChatCl
Main.java:92: error: cannot find symbol
      System.arraycopy(cbuf, 0, send, 1, (cbuf.length < ChatParam.NSIZE)?cbuf.length:ChatParam.NSIZE);
                                                        ^
  symbol:   variable ChatParam
  location: class ChatCl
Main.java:92: error: cannot find symbol
      System.arraycopy(cbuf, 0, send, 1, (cbuf.length < ChatParam.NSIZE)?cbuf.length:ChatParam.NSIZE);
                                                                                     ^
  symbol:   variable ChatParam
  location: class ChatCl
Main.java:102: error: cannot find symbol
        send[0] = ChatParam.MSG;
                  ^
  symbol:   variable ChatParam
  location: class ChatCl
Main.java:106: error: cannot find symbol
        System.arraycopy(cbuf, 0, send, 1, (cbuf.length < ChatParam.MSIZE - 1)?cbuf.length:(ChatParam.MSIZE - 1));
                                                          ^
  symbol:   variable ChatParam
  location: class ChatCl
Main.java:106: error: cannot find symbol
        System.arraycopy(cbuf, 0, send, 1, (cbuf.length < ChatParam.MSIZE - 1)?cbuf.length:(ChatParam.MSIZE - 1));
                                                                                            ^
  symbol:   variable ChatParam
  location: class ChatCl
16 errors
stdout
Standard output is empty