当我初始化我的 objectinputstreams 时,在我初始化并由 objectoutputstreams 刷新之后,我得到了这个:
客户:
java.net.SocketTimeoutException:在 java.io 的 java.net.SocketInputStream.read(Unknown Source) 处的 java.net.SocketInputStream.socketRead0(Native Method) 处读取超时。 java.io.ObjectInputStream$PeekInputStream.read(Unknown Source) at java.io.ObjectInputStream$PeekInputStream.readFully(Unknown Source) at java.io.ObjectInputStream$BlockDataInputStream.readShort(Unknown Source) at java.io.ObjectInputStream.readStreamHeader(Unknown Source) at java.io.ObjectInputStream.(Unknown Source) at Start.refreshChangeLog(Start.java:87) at Start$2.windowOpened(Start.java:234) at java.awt.AWTEventMulticaster.windowOpened(Unknown Source) at java.awt。 javax.swing.JFrame 中的 Window.processWindowEvent(Unknown Source)。processWindowEvent(Unknown Source) at java.awt.Window.processEvent(Unknown Source) at java.awt.Component.dispatchEventImpl(Unknown Source) at java.awt.Container.dispatchEventImpl(Unknown Source) at java.awt.Window.dispatchEventImpl( Unknown Source) at java.awt.Component.dispatchEvent(Unknown Source) at java.awt.EventQueue.dispatchEventImpl(Unknown Source) at java.awt.EventQueue.access$200(Unknown Source) at java.awt.EventQueue$3.run( Unknown Source) at java.awt.EventQueue$3.run(Unknown Source) at java.security.AccessController.doPrivileged(Native Method) at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source) at java.security.ProtectionDomain$1.doIntersectionPrivilege (未知来源)在 java.awt.EventQueue$4.run(未知来源)在 java.awt。EventQueue$4.run(Unknown Source) at java.security.AccessController.doPrivileged(Native Method) at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source) at java.awt.EventQueue.dispatchEvent(Unknown Source) at java.awt。 EventDispatchThread.pumpOneEventForFilters(Unknown Source) at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source) at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source) at java.awt.EventDispatchThread.pumpEvents(Unknown Source) at java.awt.EventDispatchThread。 java.awt.EventDispatchThread.run(未知源)上的 pumpEvents(未知源)EventQueue.dispatchEvent(Unknown Source) at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source) at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source) at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source) at java.awt.EventDispatchThread。 java.awt.EventDispatchThread.pumpEvents(Unknown Source) at java.awt.EventDispatchThread.run(Unknown Source) 的 pumpEvents(Unknown Source)EventQueue.dispatchEvent(Unknown Source) at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source) at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source) at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source) at java.awt.EventDispatchThread。 java.awt.EventDispatchThread.pumpEvents(Unknown Source) at java.awt.EventDispatchThread.run(Unknown Source) 的 pumpEvents(Unknown Source)awt.EventDispatchThread.run(未知来源)awt.EventDispatchThread.run(未知来源)
服务器:
java.net.SocketException:在 java.io.ObjectInputStream$PeekInputStream.read(Unknown Source) at java.net.SocketInputStream.read(Unknown Source) at java.io. .ObjectInputStream$PeekInputStream.readFully(Unknown Source) at java.io.ObjectInputStream$BlockDataInputStream.readShort(Unknown Source) at java.io.ObjectInputStream.readStreamHeader(Unknown Source) at java.io.ObjectInputStream.(Unknown Source) at Client。 (Client.java:27) 在 ThreadAccept.run(ThreadAccept.java:23)
我的客户代码:
public void refreshChangeLog() {
Socket logSocket = null;
ObjectInputStream input = null;
ObjectOutputStream output = null;
try {
logSocket = new Socket(InetAddress.getByName(server), 21992);
logSocket.setSoTimeout(0);
output = new ObjectOutputStream(logSocket.getOutputStream());
output.flush();
input = new ObjectInputStream(logSocket.getInputStream());
output.writeObject("type:changeLog");
output.flush();
Object o = input.readObject();
while (o instanceof String && !((String) o).equals("done")) {
String msg = (String) o;
int index = msg.indexOf("|");
if (index > -1) {
changeLogMap.put(msg.substring(0, index), msg.substring(index + 1));
changeLogList.addItem(msg.substring(0, index));
}
o = input.readObject();
}
}catch (Exception e) {
e.printStackTrace();
loadLocalChangeLog();
}
if (changeLogList.getItemCount() > 0) {
changeLogList.setSelectedIndex(0);
File changelog = new File(path + "changelog");
if (!changelog.exists() || !changelog.isDirectory()) {
changelog.mkdirs();
}
for (String key : changeLogMap.keySet()) {
File changef = new File(changelog, key + ".txt");
if (!changef.exists()) {
try {
changef.createNewFile();
}catch (IOException e) {
e.printStackTrace();
}
}
if (changef.exists()) {
PrintWriter writer;
try {
writer = new PrintWriter(changef);
writer.write(changeLogMap.get(key));
}catch (IOException e) {
e.printStackTrace();
}
}
}
}
try {
if (output != null) {
output.close();
output = null;
}
if (input != null) {
input.close();
input = null;
}
if (logSocket != null) {
logSocket.close();
logSocket = null;
}
}catch (IOException e1) {
e1.printStackTrace();
}
}`
还有我的服务器代码:
socket = new ServerSocket(21992);
new Client.ThreadUpdateLobby();
while (Start.frame != null && socket != null && !socket.isClosed()) {
Socket clientSocket = socket.accept();
clientSocket.setSoTimeout(0);
Client client = new Client(clientSocket);
if (client.isValid) {
synchronized (clients) {
clients.add(client);
}
}
Thread.sleep(50L);
}`
更多服务器代码:
this.socket = socket;
try {
this.socket = socket;
output = new ObjectOutputStream(socket.getOutputStream());
output.flush();
input = new ObjectInputStream(socket.getInputStream());
listener = new ThreadListen(this, input);
isValid = true;
}catch (Exception e) {
e.printStackTrace();
try {
if (output != null) {
output.close();
output = null;
}
if (input != null) {
input.close();
input = null;
}
if (socket != null) {
socket.close();
this.socket = null;
}
}catch (IOException e1) {
e1.printStackTrace();
}
isValid = false;
}`
我一直很茫然,显然标题没有冲洗?因此输入流只是坐着等待。问题似乎出在客户端。另外,只有当我使用我的外部网络 ip(98.232.202.119) 时才会发生这种情况,但如果我使用“127.0.0.1”或“192.168.2.50”没有问题我的端口被转发,我在线测试了它们。