首先这里是我编辑的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