2014-06-21 80 views
0

首先這裏是我編輯的projects客戶端/服務器套接字。不讀或寫

IDE = Netbeans的

我有一個項目的ServerSocket和客戶Socket的第二個項目。

ServerSocket的項目的代碼段:

showStatus(String status); is method which appends text to statusWindow (JTextArea); 
Socket gameClientSocket,ObjectOutputStream gameoos and ObjectInputStream gameois is declared outside code fragment 

代碼:

private void configureSockets() { 
     try{ 
     properties.showStatus("GAME_THREAD-waiting someone to accept"); 
     gameClientSocket = gameSocket.accept(); 

     properties.showStatus("GAME_THREAD-Accepted"); 
     properties.showStatus("GAME_THREAD-getting outputsstreams"); 

     gameoos= new ObjectOutputStream(gameClientSocket.getOutputStream()); 
     gameoos.flush(); 
     properties.setGameStream(gameoos); 


     properties.showStatus("GAME_THREAD-getting inputstreams"); 
     gameois=new ObjectInputStream(gameClientSocket.getInputStream()); 
     properties.showStatus("GAME_THREAD-testing connections ,\nwe must receive int 1 "); 
     properties.showStatus("GAME_THREAD- received "+gameois.readInt()); 
     properties.showStatus("GAME_THREAD-tested"); 



     }catch(IOException ex){ 
      properties.showStatus(ex.getMessage());} 
     } 

以及初始化:

gameSocket = new ServerSocket(GAME_PORT); 

ClientSocket的項目的代碼段:

System.out.println("GAME_THREAD-configuring gameSocket "); 
       properties.showStatus("GAME_THREAD- configuring gameSocket "); 
       if(gameSocket==null){ 
       gameSocket = new Socket("localhost",GAME_PORT); 
       System.out.println("GAME_THREAD- getting Streams"); 
       properties.showStatus("GAME_THREAD- getting Streams "); 

       gameoos = new ObjectOutputStream(gameSocket.getOutputStream()); 
       gameoos.flush(); 
       gameois = new ObjectInputStream(gameSocket.getInputStream()); 

       properties.showStatus("GAME_THREAD-testing sending "); 
       gameoos.writeInt(1); 
       properties.showStatus("GAME_THREAD-seccessfully sent "); 

       properties.showStatus("GAME_THREAD- setting Streams to gameWindow "); 
       System.out.println("GAME_THREAD-setting Streams to gameWindow"); 
       properties.setGameStream(gameoos); 
      } 

在這裏到底是狀態的Windows:

GAME_THREAD - blocking game Window 
GAME_THREAD- configuring gameSocket 
GAME_THREAD- getting Streams 
GAME_THREAD-testing sending 
GAME_THREAD-seccessfully sent 
GAME_THREAD- setting Streams to gameWindow 

和服務器項目狀態窗口:

GAME_THREAD-Accepted 
GAME_THREAD-getting outputsstreams 
GAME_THREAD-getting inputstreams 
GAME_THREAD-testing connections , 
we must receive int 1 

問題:

我不能從ObjectInputStream讀取數(或者它不寫),異常不會被拋出,過程凍結,不做任何事情。我不知道我是否做錯了什麼。我搜索了整個網頁,但找不到任何可用的答案。你可以幫幫我嗎?

UPDATE:

gameoos.writeint(1); 
gameoos.flush(); 

解決發生

回答

0

凍結,因爲你正在嘗試做的主線程,我相信,這是一個可怕的計劃上的網絡連接問題 - 你需要把他們在第二個線程。您可以使用ExecutorService的線程池http://www.journaldev.com/1069/java-thread-pool-example-using-executors-and-threadpoolexecutor,也可以運行自己的線程。

線程需要運行Runnable接口的實現。

public class MyThread implements Runnable 
{ 
    @Override 
    public void run() 
    { 
     //do stuff 
    } 
} 

public static void main(String[] args) 
{ 
    Thread thread = new Thread(new MyThread()); 
    thread.start(); 
    //...do other stuff and not end the app here 
} 

但我不認爲你可以發送對象就是這樣,你需要將它們轉換爲byte []和重新組合:send a serializable object over socket

我個人的建議是乾脆溝ObjectStreams,並使用一個框架,允許通過,因爲它們是發送對象,像KryoNet框架: https://github.com/EsotericSoftware/kryonet

用於使用這將是這樣的一個例子的代碼:

Server server = new Server(); 
    Kryo kryo = server.getKryo(); 
    kryo.register(float[].class); 
    server.start(); 
    server.bind(2300, 2301); 
    server.addListener(new Listener() { 
    public void received(Connection connection, Object object) 
    { 
     if(object instanceof float[]) 
     { 
     float[] array = (float[])object; 
     for(int i = 0; i < array.length; i++) 
     { 
      System.out.println("" + array[i]); 
     } 
     }   
    }}); 
    Client client = new Client(); 
    Kryo kryo = client.getKryo(); 
    kryo.register(float[].class); 
    client.addListener(new Listener() { 
    public void connected(Connection connection) 
    { 
     connection.sendTCP(new float[] {5, 6, 7, 8}); 
    } 
    }; 
    client.connect(5000, "127.0.0.1」, 2300, 2301); 

而KryoNet已經在後臺線程上運行網絡連接,所以你根本不需要搞砸。

+0

感謝您的答案,請檢查我的項目在頂部。 這是一個很高的項目,所以我很有限。我必須使用 套接字進行網絡連接。 我在單獨的線程中嘗試此操作。 我只需要測試連接。 – katamarani4

相關問題