我使用java.net包中,由於某種原因在客戶端和服務器之間的套接字關閉或者被損壞寫了一個對等網絡的網絡遊戲中的Java。我稱之爲「點對點」,因爲其中一個客戶端也運行一個服務器類,它只是一個我編寫的接受Socket連接的自定義類 - 一個來自另一臺計算機,另一臺來自客戶端計算機。 正在使用連接來使用ObjectOutputStream和ObjectInputStream來回移動對象。的ObjectOutputStream通過TCP套接字
插座錯誤不規則發生。出現問題前,連接通常會打開5-10分鐘。有時會在新遊戲開始時關閉,有時會在遊戲進行時關閉。
- 我應該使用keepAlive保持連接打開嗎?
- 沒有人對我應該如何去解決此問題有何建議?
這裏是代碼生成錯誤:
/**
* Continually checks for messages to be read in. Then it
* sends the messages to processMessage if a message is available.
*/
public void listenForMessages() {
int availableBytes;
Object obj = null;
try {
availableBytes = socketStream.available();
if (availableBytes >5) {
obj = in.readObject();
message = (ClientMessage) (obj);
processMessage();
}
}
catch(java.io.StreamCorruptedException utoh) {
System.out.println("Read failed: " + utoh);
try {
System.out.println("Number of Bytes available: " + socketStream.available());
utoh.printStackTrace();
System.out.println("Attempting to close socked.");
socketStream.close();
}
catch(IOException ioe) {
System.out.println("Unable to close: " + ioe.toString());
}
}
catch(IOException e) {
System.out.println("Read failed" + e);
}
以下是錯誤消息和堆棧跟蹤:
Read failed: java.io.StreamCorruptedException: invalid type code: 00
Number of Bytes available: 100
有兩個插座(兩個流)活性,每個客戶。當其他的插座拋出了同樣的錯誤,我看到無效類型代碼:FF
堆棧跟蹤
java.io.StreamCorruptedException: invalid type code: 00
at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1355)
at java.io.ObjectInputStream.defaultReadFields(ObjectInputStream.java:1946)
at java.io.ObjectInputStream.readSerialData(ObjectInputStream.java:1870)
at java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:1752)
at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1328)
at java.io.ObjectInputStream.readObject(ObjectInputStream.java:350)
at thornworks.quiz.PlayerInterface.listenForMessages(PlayerInterface.java:227)
at thornworks.quiz.PlayerInterface.actionPerformed(PlayerInterface.java:214)
at javax.swing.Timer.fireActionPerformed(Timer.java:291)
at javax.swing.Timer$DoPostEvent.run(Timer.java:221)
at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:209)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:641)
at java.awt.EventQueue.access$000(EventQueue.java:84)
at java.awt.EventQueue$1.run(EventQueue.java:602)
at java.awt.EventQueue$1.run(EventQueue.java:600)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:87)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:611)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:269)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:184)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:174)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:169)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:161)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:122)
難以回答無碼問題。 – vulkanino 2012-03-08 15:51:37
「套接字錯誤發生不規則」:*什麼*'套接字錯誤'?把它貼在這裏,用堆棧跟蹤。 – EJP 2012-03-09 00:15:38
我發佈了一些代碼和堆棧跟蹤。希望有所幫助。 – Thorn 2012-03-09 15:48:31