首先這裏是我編輯的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();
解決發生
感謝您的答案,請檢查我的項目在頂部。 這是一個很高的項目,所以我很有限。我必須使用 套接字進行網絡連接。 我在單獨的線程中嘗試此操作。 我只需要測試連接。 – katamarani4