我有一个相当复杂的项目,归结为通过对象流进行通信的简单客户端/服务器。
对于两个连续的连接(我连接一次,工作,断开连接,然后再次连接,工作和断开连接),一切都完美无缺。客户端连接,执行其业务,然后关闭。服务器成功关闭对象输出流和套接字,没有 IO 错误。
当我尝试第三次连接时,连接似乎已通过(ServerSocket.accept() 方法通过并成功创建了 ObjectOutputStream)。然而,没有数据被传递。inputStream.readUnshared() 方法只是阻塞。
我采取了以下内存预防措施:
- 当需要关闭套接字时,所有正在运行的线程都会停止,并且所有对象都被清空。
- 每次调用 writeUnshared() 方法后,都会刷新并重置 ObjectOutputBuffer。
有没有人遇到过类似的问题,或者有没有人有什么建议?恐怕我的项目比较大,所以复制代码是有问题的。
该项目归结为:
服务器主
ServerSocket serverSocket = new ServerSocket(port);
while (true) {
new WorkThread(serverSocket.accept()).start();
}
工作线程(服务器)
public void run() {
ObjectInputBuffer inputBuffer = new ObjectInputBuffer(new BufferedInputStream(socket.getInputStream()));
while (running) {
try {
Object myObject = inputBuffer.readUnshared();
// do work is not specified in this sample
doWork(myObject);
} catch (IOException e) {
running = false;
}
}
try {
inputBuffer.close();
socket.close();
} catch (Exception e) {
System.out.println("Could not close.");
}
}
客户
public Client() {
Object myObject;
Socket mySocket = new Socket(address, port);
try {
ObjectOutputBuffer output = new ObjectOutputBuffer(new BufferedOutputStream(mySocket.getOutputStream()));
output.reset();
output.flush();
} catch (Exception e) {
System.out.println("Could not get an input.");
mySocket.close();
return;
}
// get object data is not specified in this sample. it simply returns a serializable object
myObject = getObjectData();
while (myObject != null) {
try {
output.writeUnshared(myObject);
output.reset();
output.flush();
} catch (Exception e) {
e.printStackTrace();
break;
} // catch
} // while
try {
output.close();
socket.close();
} catch (Exception e) {
System.out.println("Could not close.");
}
}
感谢所有可能提供帮助的人!