Program pauses on initializing object input stream in Java

时间:2021-02-28 20:48:08

While running debugger, the program pauses on initializing object streams from server main input output streams. Following is the code :

在运行调试器时,程序暂停从服务器主输入输出流初始化对象流。以下是代码:

 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.");
            }

It pauses on fobjIn and do not go to execute fobjOut although fobjIn it passes from fobjIn breakpoint but do not hit out breakpoint.

它暂停fobjIn并且不执行fobjOut虽然fobjIn它从fobjIn断点传递但是没有击中断点。

2 个解决方案

#1


5  

I would keep it simple like this

我会像这样保持简单

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();
}

#2


0  

The problem is that ObjectInputStream pre-reads data on initialization.

问题是ObjectInputStream在初始化时预读取数据。

The preferred solution is at Java Creating a new ObjectInputStream Blocks: always initialize your ObjectOutputStream before initializing your ObjectInputStream, so that the "handshake" that the two use internally can be initiated.

首选解决方案是Java创建新的ObjectInputStream Blocks:在初始化ObjectInputStream之前始终初始化ObjectOutputStream,以便可以启动内部使用的“握手”。

When you don't control all the code and cannot change the order, consider delaying the OIS initialization until data is available (InputStream.available or mark/read/reset on a buffered stream wrapping it, etc).

当您不控制所有代码并且无法更改顺序时,请考虑延迟OIS初始化直到数据可用(InputStream.available或标记/读取/重置在包装它的缓冲流上等)。

#1


5  

I would keep it simple like this

我会像这样保持简单

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();
}

#2


0  

The problem is that ObjectInputStream pre-reads data on initialization.

问题是ObjectInputStream在初始化时预读取数据。

The preferred solution is at Java Creating a new ObjectInputStream Blocks: always initialize your ObjectOutputStream before initializing your ObjectInputStream, so that the "handshake" that the two use internally can be initiated.

首选解决方案是Java创建新的ObjectInputStream Blocks:在初始化ObjectInputStream之前始终初始化ObjectOutputStream,以便可以启动内部使用的“握手”。

When you don't control all the code and cannot change the order, consider delaying the OIS initialization until data is available (InputStream.available or mark/read/reset on a buffered stream wrapping it, etc).

当您不控制所有代码并且无法更改顺序时,请考虑延迟OIS初始化直到数据可用(InputStream.available或标记/读取/重置在包装它的缓冲流上等)。