在运行调试器时,程序暂停从服务器主输入输出流初始化对象流。以下是代码:
public TFileReader(Client cli)throws Exception{
this.cli = cli;
fileSock = new Socket(cli.ServerIp(), cli.FilePort());
fobjIn = new ObjectInputStream(fileSock.getInputStream());
fobjOut = new ObjectOutputStream(fileSock.getOutputStream());
fobjOut.flush();
}
@Override
public void run(){
try{
System.out.println("file reader thread online");
fobjOut.writeObject(cli.Name());
fobjOut.flush();
String per = (String) fobjIn.readObject();
System.out.println(per+"video filing...");
if(!per.equals("OKF"))
{
throw new Exception("Error In retriving video.");
}
暂停fobjIn
并且不执行fobjOut
虽然fobjIn
它从fobjIn
断点传递但没有击中断点。
答案 0 :(得分:5)
我会像这样保持简单
public TFileReader(Client cli) throws IOException {
this.cli = cli;
socket = new Socket(cli.ServerIp(), cli.FilePort());
out = new ObjectOutputStream(socket.getOutputStream());
out.flush();
in = new ObjectInputStream(socket.getInputStream());
}
public void writeObject(Object o) throw IOException {
out.writeObject(o);
out.reset();
out.flush();
}
public <T> T readObject() throw IOException {
return (T) in.readObject();
}
public void close() throws IOException {
in.close();
out.close();
socket.close();
}
答案 1 :(得分:0)
问题是ObjectInputStream在初始化时预先读取数据。
首选解决方案位于Java Creating a new ObjectInputStream Blocks:始终在初始化ObjectInputStream之前初始化ObjectOutputStream ,以便可以启动内部使用的“握手”。
当您不控制所有代码并且无法更改顺序时,请考虑延迟OIS初始化直到数据可用(InputStream.available或标记/读取/重置在包装它的缓冲流上等)。