【发布时间】:2014-08-11 22:43:56
【问题描述】:
首先这里是我的可编译的projects。
IDE = Netbeans
我在一个项目中有服务器套接字,在第二个项目中有客户端套接字。
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);
}
最后是状态窗口:
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 读取数字(或者它没有写入),从不抛出异常,进程冻结并且不执行任何操作。我不知道我是否做错了什么。我搜索了整个网络,但找不到任何可用的答案。你能帮帮我吗?
更新:
gameoos.writeint(1);
gameoos.flush();
解决了问题
【问题讨论】:
标签: java sockets objectinputstream objectoutputstream