3

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.

2
  • BTW You should always create and flush your ObjectOutputStream first. If you don't the two ends can deadlock, each waiting to read from the other. Commented Jan 27, 2013 at 19:22
  • Actually i am initializing object in put stream just just after settngs class contructor thwn hoq i flush it qhen it is initialized first time.? Can u tell me whwre is the mistake Commented Jan 27, 2013 at 19:47

2 Answers 2

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();
}
Sign up to request clarification or add additional context in comments.

2 Comments

please check again my edited code , but im facing the same problem as above
As I said before, You should always create and flush your ObjectOutputStream first.
0

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

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.

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).

5 Comments

please don't use available(), it is not the solution to any problem.
@mihai im using object stream in server end too and sedning that data from objectouputstream from there also.
You have overlooked the one reliable solution in favor of several most dubious workarounds. -1
@jtahlborn Does available() not constitute a good indicator that the ObjectOutputStream has been created on the other side?
@EJP Thank you for your notes. I have updated the answer to feature the preferred solution more prominently. I believe the delaying of ObjectInputStream initialization also works - it has worked for me in the past - and I'm not immediately seeing the caveats. Could you elaborate on the concern regarding this approach?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.