2014-09-23 61 views
0

我在這個學期的入門網絡課,我們正在與服務器/客戶端聊天室合作。這是我的服務器程序到目前爲止,我無法弄清楚爲什麼我在標題中出現錯誤。它顯示了第17行,第33行和第53行的錯誤,這些行是我命名新類的所有行。 對我的任何幫助表示讚賞!爲什麼我得到這個錯誤:「在JCreator中出現」error:{expected「?

import java.io.*; 
import java.net.*; 

public class Server 
{ 

    public static void main(String[] args) throws Exception 
    { 
     new MyServer().start(); //Creates new Server for Clients to connect 
    } 


} 

public class MyServer() extends Thread 
{ 
    ServerSocket SS = new ServerSocket(11200); 
    Socket S; 
    ClientManager CM = new ClientManager(); 

    public void run() 
    { 
     while(true) 
     { 
      S = SS.Accept; //Endless loop allowing clients to repeatedly connect 
      CM.Add(); //Calls the Add method in ClientManager, which adds the Client to the Array 
     } 
    } 
} 

public class MyClient() extends Thread 
{ 
    MyClient Client = new MyClient(Socket, CM); 
    Scanner S; 

    public void run() 
    { 
     while(true) 
     { 
      S = new Scanner(System.in); 
      CM.SendToAllClients(S); //Calls the method that will send each client the received message 
     } 
    } 

    public void Send(String S) 
    { 
     PrintWriter.println(S); 
    }  
} 

public class ClientManager() 
{ 
    MyClient[] X = new MyClient[15]; 
    int num = 0; 

    public synchronized void Add(MyClient C) 
    { 
     X[num] = C; 
     num++; 
     C.start(); 
    } 

    public synchronized void SendToAllClient(String S) 
    { 
     for(i = 0;i < num;i++); 
     { 
      X[i].Send(S); 
     } 
    } 
} 
+3

'公共類的MyServer()擴展Thread'應該是'公共類的MyServer延伸Thread'? – Whymarrh 2014-09-23 22:15:16

+1

另外,「MyClient」和「MyServer」應該可能是''執行Runnable''而不是''extends Thread'' – 2014-09-23 22:18:17

+0

作爲提示,您需要啓動方法和變量名稱小寫字母。我不只是這麼說,因爲這是「正常的風格」;它會在後續的代碼可讀性方面提供幫助。 – 2014-09-24 07:06:28

回答

0

不要使用()你的類名之後:

public class MyServer extends Thread {...} 

public class MyClient extends Thread {...} 

public class ClientManager {...} 
+0

這清除了我之前遇到的問題,但是介紹了更多內容,我將不得不仔細閱讀。謝謝! – Sean 2014-09-23 22:26:06

相關問題