fork download
  1. // サーバープログラム
  2.  
  3. import java.io.*;
  4. import java.util.*;
  5. import java.net.*;
  6.  
  7. class UserInfo {
  8. Socket sock;
  9. String name;
  10. };
  11.  
  12. public class ChatSv {
  13.  
  14. ServerSocket ssock = null;
  15. Socket asock = null;
  16. byte[] buf = new byte[ChatParam.MSIZE];
  17. UserInfo[] users = new UserInfo[ChatParam.MAX_USERS];
  18. String str = null;
  19.  
  20. /* ある参加者からメッセージが送信されてきたら、それ以外の参加者にそのメッセージを転送する */
  21. class UserProcess extends Thread {
  22.  
  23. UserInfo userInfo;
  24.  
  25. UserProcess(UserInfo userInfo){
  26. this.userInfo = userInfo;
  27. }
  28.  
  29. public void run() {
  30. try {
  31. while(true){
  32. if (userInfo.sock != null) {
  33. int count = userInfo.is.read(buf);
  34. if (count > 0) {
  35. sendAll(makeMessage(ChatParam.MSG, new String(buf)));
  36. }
  37. }
  38. }
  39. }catch(Exception e){
  40. /* 誰かが抜けた(接続が切れた)場合には他の参加者に抜けたことを伝える */
  41. sendAll(makeMessage(ChatParam.LEAVE, userInfo.name + "さんが抜けました"));
  42. }
  43. try {
  44. userInfo.sock.close();
  45. userInfo.sock = null;
  46. } catch(Exception e) {
  47. System.out.println(e);
  48. }
  49. }
  50.  
  51. }
  52.  
  53. public static void main(String[] args) {
  54. try{
  55. new ChatSv().start();
  56. } catch(Exception e) {
  57. System.out.println(e);
  58. }
  59. }
  60.  
  61. public void start() throws Exception {
  62.  
  63. ssock = new ServerSocket(ChatParam.PORT);
  64.  
  65. for (int i = 0; i < ChatParam.MAX_USERS; i++) {
  66. users[i] = new UserInfo();
  67. users[i].sock = null;
  68. };
  69.  
  70. // このループの仕事は接続受付だけにする
  71. while (true) {
  72. try {
  73. /* クライアントからの接続待ち準備 */
  74. asock = ssock.accept();
  75. /* クライアントからの接続受け付け */
  76. UserInfo newUser = addUser(asock);
  77.  
  78. if (newUser != null) {
  79. /* 接続してきたクライアントに参加OKを伝える */
  80. newUser.os.write(makeMessage(ChatParam.MSG, "チャットに参加しました"));
  81. new UserProcess(newUser).start();
  82. /* すでに参加しているクライアントに新しい参加者を伝える */
  83. sendAll(makeMessage(ChatParam.JOIN, newUser.name + "さんが加わりました"));
  84. } else {
  85. /* すでに最大人数に達しているので参加不可をクライアントに伝えて切断処理を行う */
  86. asock.getOutputStream().write(makeMessage(ChatParam.FULL, "最大人数に達しているため参加できませんでした"));
  87. Thread.sleep(500);
  88. asock.close();
  89. }
  90.  
  91. // 少なくともこれ以降のクライアントとの通信は並列に行う必要あり
  92. // (新たな参加者受け付けと参加済みのクライアントからのメッセージ
  93. // 受信は順序が決まっていないため)
  94. Thread.currentThread().sleep(100);
  95.  
  96. } catch(Exception e) {
  97. e.printStackTrace();
  98. }
  99. }
  100. /* 通信終了処理(上が無限ループなので意味はないが) */
  101. // ssock.close();
  102. }
  103.  
  104. public synchronized byte[] makeMessage(byte param, String message) {
  105. byte[] result = new byte[ChatParam.MSIZE];
  106. byte[] messageBytes = (message+"\0").getBytes();
  107. result[0] = param;
  108. System.arraycopy(messageBytes, 0, result, 1, (messageBytes.length < ChatParam.MSIZE-1 ) ? messageBytes.length : ChatParam.MSIZE-1);
  109. return result;
  110. }
  111.  
  112. public synchronized UserInfo addUser(Socket s) throws Exception {
  113. for (int i = 0; i < ChatParam.MAX_USERS; i++) {
  114. if (users[i].sock == null) {
  115. // まだ最大人数に達していないので参加OK
  116. users[i].sock = s;
  117. users[i].is = s.getInputStream();
  118. users[i].os = s.getOutputStream();
  119. users[i].is.read(buf);
  120. users[i].name = new String(buf, 1, ChatParam.NSIZE).trim();
  121. return users[i];
  122. }
  123. }
  124. return null;
  125. }
  126.  
  127. public synchronized void sendAll (byte[] message) {
  128. if (message == null) {
  129. return;
  130. }
  131. for (int i = 0; i < ChatParam.MAX_USERS; i++) {
  132. if (users[i].sock != null) {
  133. try {
  134. users[i].os.write(message);
  135. }catch(Exception e){
  136. System.out.println(e);
  137. }
  138. }
  139. }
  140. }
  141. }
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
Main.java:14: error: class ChatSv is public, should be declared in a file named ChatSv.java
public class ChatSv {
       ^
Main.java:18: error: cannot find symbol
	byte[] buf = new byte[ChatParam.MSIZE];
	                      ^
  symbol:   variable ChatParam
  location: class ChatSv
Main.java:19: error: cannot find symbol
	UserInfo[] users = new UserInfo[ChatParam.MAX_USERS];
	                                ^
  symbol:   variable ChatParam
  location: class ChatSv
Main.java:37: error: cannot find symbol
							sendAll(makeMessage(ChatParam.MSG, new String(buf)));
							                    ^
  symbol:   variable ChatParam
  location: class ChatSv.UserProcess
Main.java:43: error: cannot find symbol
				sendAll(makeMessage(ChatParam.LEAVE, userInfo.name + "????????"));
				                    ^
  symbol:   variable ChatParam
  location: class ChatSv.UserProcess
Main.java:65: error: cannot find symbol
		ssock = new ServerSocket(ChatParam.PORT);
		                         ^
  symbol:   variable ChatParam
  location: class ChatSv
Main.java:67: error: cannot find symbol
		for (int i = 0; i < ChatParam.MAX_USERS; i++) {
		                    ^
  symbol:   variable ChatParam
  location: class ChatSv
Main.java:82: error: cannot find symbol
					newUser.os.write(makeMessage(ChatParam.MSG, "???????????"));
					                             ^
  symbol:   variable ChatParam
  location: class ChatSv
Main.java:85: error: cannot find symbol
					sendAll(makeMessage(ChatParam.JOIN, newUser.name + "?????????"));
					                    ^
  symbol:   variable ChatParam
  location: class ChatSv
Main.java:88: error: cannot find symbol
					asock.getOutputStream().write(makeMessage(ChatParam.FULL, "??????????????????????"));
					                                          ^
  symbol:   variable ChatParam
  location: class ChatSv
Main.java:107: error: cannot find symbol
		byte[] result = new byte[ChatParam.MSIZE];
		                         ^
  symbol:   variable ChatParam
  location: class ChatSv
Main.java:110: error: cannot find symbol
		System.arraycopy(messageBytes, 0, result, 1, (messageBytes.length < ChatParam.MSIZE-1 ) ? messageBytes.length : ChatParam.MSIZE-1);
		                                                                    ^
  symbol:   variable ChatParam
  location: class ChatSv
Main.java:110: error: cannot find symbol
		System.arraycopy(messageBytes, 0, result, 1, (messageBytes.length < ChatParam.MSIZE-1 ) ? messageBytes.length : ChatParam.MSIZE-1);
		                                                                                                                ^
  symbol:   variable ChatParam
  location: class ChatSv
Main.java:115: error: cannot find symbol
		for (int i = 0; i < ChatParam.MAX_USERS; i++) {
		                    ^
  symbol:   variable ChatParam
  location: class ChatSv
Main.java:122: error: cannot find symbol
				users[i].name = new String(buf, 1, ChatParam.NSIZE).trim(); 
				                                   ^
  symbol:   variable ChatParam
  location: class ChatSv
Main.java:133: error: cannot find symbol
		for (int i = 0; i < ChatParam.MAX_USERS; i++) {
		                    ^
  symbol:   variable ChatParam
  location: class ChatSv
16 errors
stdout
Standard output is empty