fork download
  1. package object_ex;
  2.  
  3. import java.io.IOException;
  4. import java.io.ObjectInputStream;
  5. import java.io.ObjectOutputStream;
  6. import java.net.BindException;
  7. import java.net.ServerSocket;
  8. import java.net.Socket;
  9. import java.net.SocketException;
  10. import java.util.ArrayList;
  11.  
  12. public class Chat_Server {
  13. ServerSocket server=null;
  14. //ObjectInputStream input=null;
  15.  
  16. boolean isStart=false;//服務端是否啟動
  17. boolean isConnect=false;//客戶端是否連接上
  18. object_data data = new object_data();
  19.  
  20. ArrayList<Client_Thread> clients = new ArrayList<Client_Thread>();
  21. public static void main(String[] args) {
  22. new Chat_Server().startServer();
  23. }
  24.  
  25. private void startServer(){
  26. System.out.println("server is running");
  27. try{
  28. server=new ServerSocket(8888);
  29. isStart=true;
  30. }catch(BindException e){
  31. System.out.println("port has been used");
  32. System.exit(0);
  33. }catch(IOException e){
  34. e.printStackTrace();
  35. System.exit(0);
  36. }
  37. try {
  38. while(isStart){
  39. Socket client=server.accept();
  40. //client成功連接後,為該client創立一個獨立的thread來接收消息
  41. Client_Thread client_thread=
  42. new Client_Thread(client);
  43. new Thread(client_thread).start();//啟動thread
  44. clients.add(client_thread);//加入thread
  45. }
  46. }catch(Exception e){
  47. e.printStackTrace();
  48. }
  49. finally{
  50. try {
  51. server.close();
  52. } catch (IOException e) {
  53. // TODO Auto-generated catch block
  54. e.printStackTrace();
  55. }
  56. }
  57. }
  58.  
  59. /*
  60.   * 每當一個client連接到時,都會創立一個如下的thread
  61.   */
  62. class Client_Thread implements Runnable{
  63.  
  64. //在此內保留socket、inputStream
  65. private Socket Sock=null;
  66. private ObjectInputStream in=null;
  67. private ObjectOutputStream output=null;
  68. private boolean isConnect=false;
  69. Client_Thread(Socket s){
  70. this.Sock=s;
  71. try {
  72. in=new ObjectInputStream(s.getInputStream());
  73. output=new ObjectOutputStream(s.getOutputStream());
  74. isConnect=true;
  75. } catch (IOException e) {
  76. // TODO Auto-generated catch block
  77. System.out.println("Client_Thread");
  78. e.printStackTrace();
  79. }
  80. }
  81.  
  82. public void send(String str){
  83. try {
  84. data.setchat(str);
  85. output.writeObject(data);
  86. output.flush();
  87. } catch (IOException e) {
  88. // TODO Auto-generated catch block
  89. e.printStackTrace();
  90. }
  91. }
  92.  
  93. public void run() {
  94. System.out.println("server is watting(伺服器等待中)");
  95. String str = null;
  96. try {
  97. while(isConnect){
  98. data = (object_data)in.readObject();
  99. str = data.getchat();
  100. //str = (String)in.readObject();
  101. System.out.println(str);
  102.  
  103. for(int i=0;i<clients.size();i++){
  104. Client_Thread c=clients.get(i);
  105. c.send(str);
  106. }
  107. }
  108. } catch (SocketException e) {
  109. System.out.println("client has been closed");
  110. } catch (Exception e) {
  111. // TODO Auto-generated catch block
  112. e.printStackTrace();
  113. }
  114. finally{
  115. try {
  116. if(in!=null) in.close();
  117. if(output!=null) output.close();
  118. Sock.close();
  119. } catch (IOException e) {
  120. // TODO Auto-generated catch block
  121. e.printStackTrace();
  122. }
  123. }
  124. }
  125. }
  126. }
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
Main.java:12: error: class Chat_Server is public, should be declared in a file named Chat_Server.java
public class Chat_Server {
       ^
Main.java:18: error: cannot find symbol
  object_data data = new object_data();
  ^
  symbol:   class object_data
  location: class Chat_Server
Main.java:18: error: cannot find symbol
  object_data data = new object_data();
                         ^
  symbol:   class object_data
  location: class Chat_Server
Main.java:98: error: cannot find symbol
            data = (object_data)in.readObject();
                    ^
  symbol:   class object_data
  location: class Chat_Server.Client_Thread
4 errors
stdout
Standard output is empty