2012-05-28 133 views
0

好吧有一個問題,我有一個服務器,這個服務器是多線程的,它允許多個客戶端連接到它。當客戶端連接到服務器時,它將被引入線程以完成其任務,以此類推其他客戶端。多線程服務器爲每個個人客戶端

基本上客戶端所做的是在屏幕上繪製一個'圖形'方塊,當你在屏幕上移動這個方塊時(通過鍵盤事件,箭頭鍵)它會將x和y cordonate發送到服務器放置在與開始連接中給予客戶端的客戶端號碼對應的索引的數組中。

我的問題是沒有發送x和y線到服務器,無論有多少客戶端我連接他們都工作,併發送到那裏x和y線到服務器存儲,當然,當玩家做出新的舉動,陣列中的前一個x和y被新的x和y線覆蓋。

我的問題是繪製每個客戶(不包括它的客戶端)到其他客戶端畫面...

所以基本上,如果我將我的廣場我的客戶的屏幕,將每個人的屏幕上移動的(這就是連接到服務器當然)。我在想每個客戶端都有一個多線程服務器,這意味着每個連接有兩個線程。但是如果我在兩個線程中同時來回發送東西,套接字將會變得混亂。另外,我希望客戶不斷地收到這個x和y線的數組,而不僅僅是當它移動時,以便它在每個人進行移動時都會更新每個人。而且我還希望服務器不斷地將x和y線陣列發送給客戶端,同時另外接收新的x和y並對陣列進行正確的更改。

我會將基本結構發佈到我擁有的基礎結構上,如果有人能指出我朝着正確的方向,是否必須製作另一個服務器套接字並接受客戶端中的另一個套接字?

因此對如何去執行每個連接的多線程感到困惑。謝謝! :)

客戶端:

//some methods are taken out 
// All imports imported here 
public class ClientSquares extends JFrame implements Runnable{ 
    //variables here 
    public void run(){ 
     try{ 
      while(true){ 
       //other calls here (just for client) 
       if(online == true && (x != oldX || y != oldY)){ 
        //only sends its X and Y cords when the player makes a move. 
        checkAbility check = new checkAbility(x, y, clientNum, alpha); 
        check.checking(); 
        //the Checking method in checkAbilities class just assigns a client   
        //number to this client, which is stored in a int variable called 
        //clientNum, once its assigned then it can send its x and Y cords 
       } 
       Thread.sleep(3); 
      } 
     }catch(Exception e){ 
     System.err.println("Error in implemented Runnable's method Run"); 
     } 
    } 

//paint method stuff here, paints the the clients square, and everyone else (according 
//to the array received from the server. 

    public static void main(String [] args){ 
     //thread for client started here 
    } 
    public ClientSquares(){ 
    //standard jFrame here, creates standard x and y cords here 

    //This is how im making my connection with the server right now.. further 
    //communication (in getting client number, and x and y cords are done in a 
    //different class) 
    try{ 
     sock = new Socket("localhost",4000); 
     OS = new DataOutputStream(sock.getOutputStream()); 
     IS = new DataInputStream(sock.getInputStream()); 
     OIS = new ObjectInputStream(sock.getInputStream()); 
     roomNum = IS.readInt(); 
     players = new int[roomNum + 1][2]; 
     defaultPlayerArray(); 
     online = true; 
    }catch(Exception e){ 
     System.err.println("Cannot connect to the Server, check and make sure that the" + 
         " correct ip is entered, and also check if port 777 is open" + 
         " you will now enter offline mode."); 
     online = false; 
    } 
    } 
    class AL extends KeyAdapter{ 
     //Key events here 
    } 
} 

服務器端:

//import heres 
public class ServerRunner{ 
    //variables here, and this class is called somewhere else to start the server, 
    //which 
    //has the server socket, and a couple more variables 
    public ServerRunner(Server2 ser){ 
     serv2 = ser; 
     players = new Socket[ServerRunner.roomNum + 1]; 
     xy = new int[roomNum + 1][2]; 
     gui = new GUI(); 
     string = new String[0]; 
    } 
    //start method is called in the class above, which passes it a server2 object 
    //(itself) 
    public void start(){ 
     try{ 
      serv2.dataServ = new ServerSocket(4000); 
      serv2.listening = true; 
      defaultArray(); 
      gui.main(string); //the GUI class just draws the players for the server to 
           //see 
      System.out.println("Listening for Connections..."); 
     }catch(Exception e){ 
      System.err.println("The port number 777 and 778 are not open"); 
     } 
     while(serv2.listening){ 
      try{ 
       //the data class gives the client a client number, and stores the x & y 
       //cord 
       new data(serv2.dataServ.accept()).start(); 
      }catch(Exception e){ 
       System.err.println("Error in Server Runner"); 
      } 
     } 
    } 
    public void defaultArray(){ 
     for(int i = 0; i < xy.length; i++){ 
      xy[i][0] = -1; 
     } 
    } 
} 

謝謝你幫我解決這個問題! :)

編輯: 在所有我有一個即時通訊使用的套接字,但我需要使用此套接字(線程)做多件事情,我如何去做這個沒有得到數據混淆,我應該只是做另一個serverSocket?

客戶端會將其x和y線發送到服務器以存儲在一個數組中,但也會收到一個包含其他球員x和y線的陣列,以便同時在其屏幕上繪製。 。看到這是線程到位的地方,但如果不是不可能用一個套接字很難做到的話

+0

請在文本中加入一些換行符;) –

+3

他只是想表達他是多麼困惑,他也讓我們感到困惑:) – Ixx

+0

@丹尼爾H:你認爲什麼樣的信息對於客戶和服務器? –

回答

0

服務器中每個連接有兩個線程。一個聽客戶端更新;對於每個客戶端更新它收到它做適當的處理,並在最後它發出的內部狀態已被改變的事件。

另一個線程監聽'internal state modification'事件,當它收到一個事件時,將新狀態發送給客戶端。

現在,您需要認真實施整個事件,因爲您有多個發射器,一個事件和多個接收器。

客戶端必須再次擁有兩個線程,一個發送消息到服務器,另一個接收更新的狀態;同步它們應該很容易。

而且,不這樣做......

IS = new DataInputStream(sock.getInputStream()); 
OIS = new ObjectInputStream(sock.getInputStream()); 

...因爲在讀取和OIS互換可能導致的問題。

+0

我還是非常困惑你在那裏說什麼...即時消息混淆瞭如何去做這個..... –

0

另一種方法是在服務器上註冊每個客戶端。然後,每個客戶端都必須打開自己的服務器端口。每當客戶端向服務器發送新的座標時,服務器必須將這個新的座標發送給發送該座標的客戶端以外的每個客戶端。

相關問題