2016-09-29 26 views
0

我正在用Java編寫我的遊戲。這是一款多人遊戲,整個通信正在通過服務器。每個客戶端< - >服務器套接字鏈接在單獨的威脅中工作。我在java套接字中反序列化我的arraylist對象失敗

服務器類持有的客戶在這個數組中:

public ClientServerThread clients[]; 

我還儲藏室保存玩家和遊戲對象

public ArrayList<GameRoom> channels; 

我在服務器上創建一個遊戲,併發送整場比賽物CO 2客戶端(主機和客人)是這樣的:

   Message carry on info about players and their choices about army. 
        */ 
       msgOut = new Message(Message.START_GAME, "SERVER", Message.OK, "ROOM: " + gameRoom.getName()); 
       /* 
       Creating a game 
       */ 
       ArrayList<Player> players = gameRoom.getPlayers(); 
       //Assiging players 
       //Creating game - generate map, deal cards - setup army etc. 
       Game game = new Game(players); 
       //setting game in gameRoom on server. 
       gameRoom.setGame(game); 
       //Setting msg to carry whole game 
       msgOut.setGame(game); 
       //Sending response to both clients in room; 
       server.announceInRoom(gameRoom, msgOut); 

我仔細檢查調試 - 遊戲對象廁所很好。 主持人和嘉賓都應該獲得帶有整個數組列表的遊戲對象,在這種情況下,它將是單位軍隊。

控制檯調試(看軍隊尺寸):

server.send() {type='START_GAME', sender='SERVER', content='OK', recipient='ROOM: dddGame:Host Player:adads,Nation:1,**Army size:8** Guest Player:dcfvv,Nation:7,**Army size:8** Map: [email protected]'} PortID: 53780 
server.send() {type='START_GAME', sender='SERVER', content='OK', recipient='ROOM: dddGame:Host Player:adads,Nation:1,Army size:8 Guest Player:dcfvv,Nation:7,Army size:8 Map: [email protected]'} PortID: 53781 

在客戶端,我有消息處理

   case Message.START_GAME: 
        if(msg.getContentP() == Message.OK) 
        { 

        Game game = msg.getGame(); 

        /* Create and display the form */ 
        java.awt.EventQueue.invokeLater(new Runnable() { 
         public void run() { 
          try { 
           if(currentPlayer.isHost()) 
           { 
            clientGame = new GameWindow(game , SocketClient.this, CreateRoomWindow.AS_HOST); 
           } 
           else 
           { 
            clientGame = new GameWindow(game, SocketClient.this, CreateRoomWindow.AS_GUEST); 
           } 

           clientGame.setVisible(true); 
           roomWindow.setVisible(false); 
          } catch (Exception ex) { 
           Logger.getLogger(GameWindow.class.getName()).log(Level.SEVERE, null, ex); 
          } 
         } 
        }); 
        } 
        break; 

問題是,在反序列化後,我不明白整場比賽的對象, 主機客戶端缺少ArrayList hostarmy和ArrayList guestArmy

控制檯調試:(查看軍隊規模)

Outgoing : {type='START_GAME', sender='adads', content='Start Game', recipient='SERVER'} 
Incoming : {type='START_GAME', sender='SERVER', content='OK', recipient='ROOM: dddGame:Host Player:adads,Nation:1,**Army size:0** Guest Player:dcfvv,Nation:7,**Army size:0** Map: [email protected]'} 

奇怪的來賓客戶端獲得的ArrayList guestArmy但沒有主機軍:/

Incoming : {type='START_GAME', sender='SERVER', content='OK', recipient='ROOM: dddGame:Host Player:adads,Nation:1,**Army size:0** Guest Player:dcfvv,Nation:7,**Army size:8** Map: [email protected]'} 

我設置每個參與類的serialVersionUID。即將用完的想法

我錯過了什麼?

+0

您可以發佈您的類'Message',也表明你是怎麼通過插座給他們? – haifzhan

+0

你可以發佈你的遊戲類嗎? – MGorgon

+0

讓我們不要經歷大量代碼,其中大部分代碼與您的問題無關,而是最好簡化問題並創建併發布有效的[mcve]。 –

回答

0

我是線程和訪問/引用內存分配的東西。

我學的這篇文章:Java concurency

和synchronized關鍵字沒有幫助我很多。

我深入挖掘並決定在發送給客戶端之前嘗試克隆遊戲對象。我已經紅了那篇文章:Java deep copies techniques,並決定使用UnoptimizedDeepCopy,因爲它在同步函數上運行 - 並且工作。

現在我的代碼看起來像這樣

   Game game = new Game(players2); 
       Game game2 = (Game) UnoptimizedDeepCopy.copy (game); 
       msgOut.setGame(game2); 
相關問題