0
我有一個遊戲的服務器/客戶端模型,但它始終卡在ObjectInputStream
初始化。Java Socket停留在新ObjectInputStream()
這裏是代碼爲客戶端的啓動方法:
public void start(){
try {
Socket s = new Socket("127.0.0.1", 24680);
Thread.sleep(1000);
ois = new ObjectInputStream(s.getInputStream()); // stuck here
oos = new ObjectOutputStream(s.getOutputStream());
startGame();
} catch (Exception e) {
e.printStackTrace();
}
}
}
這裏是服務器代碼:提前
try {
InputStream fis = socket.getInputStream();
ObjectInputStream ois = new ObjectInputStream(fis);
while (true) {
ArrayList <Dot> lists = (ArrayList <Dot>) ois.readObject();
for (Socket s: sockets) {
ObjectOutputStream oos = new ObjectOutputStream(s.getOutputStream());
oos.writeObject(lists);
}
}
} catch (Exception e) {
e.printStackTrace();
}
謝謝!
netstat顯示連接嗎?創建服務器套接字的服務器代碼在哪裏?這裏沒有足夠的信息來幫助。 –
它可能是網絡連接問題嗎?先嚐試用telnet連接到你的服務器。 – ilj
你有三個在這。其中只有一個是正確的。你在'new ObjectInputStream()',而不是'getInputStream()'或'new ObjectOutputStream()'被阻止。 – EJP