2012-03-03 112 views
1

我最近開始試圖建立服務器 - 客戶端連接。我能夠做出一對一的連接,沒有任何問題,但現在我試圖製作一個接受多個客戶端的服務器,並且我遇到了一個問題,我無法讓服務器在連接時監聽連接一個成立...我不知道我是否說清楚了,但這裏是我的代碼:Java;試圖製作一個接受多個客戶端連接的服務器

-The主循環等待連接:

public class ChatMultiServer { 

public static void main(String []args){ 

    int socknum = 124; 
    ServerSocket serverSocket; 
    Socket clientSocket; 

    while(true){ 

    ////opens socket 
    try{ 
     System.out.println("Opening port..."); 
     new ServerSocket(124).close(); 
     serverSocket = new ServerSocket(socknum); 
    }catch(Exception e){System.out.println("Error 101 = failed to bind to port "+socknum+"."); break;} 
    //////accepts connection 
    try{ 
     System.out.println("Waiting for connections..."); 
     clientSocket = serverSocket.accept(); 
    }catch(Exception e){System.out.println("Error 102 = failed to accept port "+socknum+"."); break;} 
    ///// 
    try{ 
     System.out.println("Initializing thread..."); 
     new Thread(new CMSThread(clientSocket)); 
    }catch(Exception e){System.out.println("Error 103 = failed to create thread."); break;} 
    try{ 
     serverSocket.close(); 
    }catch(Exception e){System.out.println("Error 105 = failed to close socket.");} 
    } 
} 

}

-The線程句柄連接:

public class CMSThread extends Thread{ 
Socket socket; 
BufferedReader in; 
PrintWriter out; 
String username; 
char EOF = (char)0x00; 
public CMSThread(Socket s){ 
    socket = s; 
    run(); 
} 
public void run(){ 
    try{ 
    System.out.println("Setting up streams..."); 
    in = (new BufferedReader(new InputStreamReader(socket.getInputStream()))); 
    out = new PrintWriter(socket.getOutputStream()); 
    }catch(Exception e){System.out.println("Error 204 = failed to get streams");} 
    try{ 
     out.print("Welcome! you can quit at any tyme by writing EXIT.\nLet me introduce myself, I'm 'testprogram 1', but that doesn't really matter since you'll do the talking.\nWhat's your name?"+EOF); 
     out.flush(); 
     username = in.readLine(); 
     out.print("<b>"+username+"</b>, that's a nice name.\nWell, i'll shut up now. Have fun talking to yourself while whoever is running the server observes your conversation.\n"+EOF); 
     out.flush();  
    }catch(Exception e){System.out.println("Are you effin kidding me!? -.- whatever... Error 666 = failed to chat.");} 
} 

}

我的問題再一次是,當服務器獲得與客戶端的連接(我爲客戶端使用actionscript只是因爲它更容易製作GUI),它只是等待,直到線程完成運行再次啓動循環。我試圖在線程處理聊天的同時使其循環。

我在想也許我需要爲循環以及處理連接的線程做一個線程,但我不知道如何去做這件事...請讓我知道如果我的假設是對的,如果是的話,對答案的一些指導會很好。

PS:我很抱歉,如果我的代碼是有點亂,或者如果這是一個愚蠢的問題,我還沒有在一段時間做了一個Java程序...

回答

2

你是不是真正的開始新主題 - 您只是直接撥打run()。據我所見,這意味着您將在創建每個CMSThread對象的主線程中執行run()

要啓動主題,您必須致電thread.start()

此外,我不知道爲什麼你要在另一個線程中包裝你的CMSThread - CMSThread擴展了線程,因此它可以在它自己的權利開始。包裝線程也未被啓動。

因此,你需要:

 new CMSThread(clientSocket).start(); 

CMSThread

+0

Alrigh的構造函數刪除run()電話,我即將在另一個線程的東西包裝很抱歉,這是從以前的代碼我改變顯然我忘了改變那部分。當我嘗試調用start()時,它告訴我方法「start」未定義,我試圖創建一個空白的start()方法,但它仍然像以前一樣工作。 -i確實刪除了run()調用。 – Ian 2012-03-03 21:13:14

+0

我的意思是'Thread.start()'。假設'CMSThread'擴展了'java.lang.Thread',而不是其他某個Thread類,那麼它繼承'start()'方法請參閱[javadoc](http://docs.oracle.com/javase/6 /docs/api/java/lang/Thread.html#start%28%29)。和Java [線程教程](http://docs.oracle.com/javase/tutorial/essential/concurrency/runthread.html) – DNA 2012-03-03 21:22:08

+0

哦,好吧,我想我現在已經瞭解了一切。它的工作原理。非常感謝,這確實有助於:D – Ian 2012-03-03 21:36:43

相關問題