Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Program pauses on initializing object input stream in Java

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.

like image 855
Shan Khan Avatar asked Mar 18 '26 18:03

Shan Khan


1 Answers

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();
}
like image 118
Peter Lawrey Avatar answered Mar 20 '26 07:03

Peter Lawrey



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!