6

I am feeling really stupid right now guys.... basically I am connecting over TCP on a local machine... and when I try to make the In/out streams at the client it wont get passed creating the object input stream. What gives? This stops after printing 2... no exceptions or anything... This isn't the first time I've used this class which is partialy why I am puzzled.

try {
            System.out.println("1");
            mySocket = new Socket("localhost", 11311);
            System.out.println("12");
            oos = new ObjectOutputStream(mySocket.getOutputStream());
            System.out.println("2");
            ois = new ObjectInputStream(mySocket.getInputStream());
            System.out.println("13");

        } catch (Exception e) {
            e.printStackTrace();
        }
Michael
  • 446
  • 2
  • 7
  • 19
  • Is the server sending the object? It seems as the server does not send anything. – Tobias Oct 01 '11 at 19:21
  • `ObjectInputStream` probably waits for a stream header to be received. – NiematojakTomasz Oct 01 '11 at 19:27
  • I am not following... the server accepts the TCP connection. I thought to read you would go ois.readObject()? I have it set up so the client sends the first object... I just wanted to create the streams. – Michael Oct 01 '11 at 19:29

2 Answers2

6

From the specification of ObjectInputStream:

This constructor will block until the corresponding ObjectOutputStream has written and flushed the header.

Kris
  • 5,714
  • 2
  • 27
  • 47
1

(For future readers:) I had the same problem because i made a silly change in server program and didn't test it for a long time then i was confused about why program is locked.

ServerSocket accepts the connection (responderSocket = serverSock.accept();) then suddenly for a inapropriate if (The silly change i mentioned!) program jumps out of the thread and because i didn't add a finally block to close streams and sockets the socket was left abandoned w/o sending or recieving anything (even stream headers). So in client side program there was no stream header (When i debbugged The code i saw that the last function executed before lock was:

public ObjectInputStream(InputStream in) throws IOException {
    verifySubclass();
    bin = new BlockDataInputStream(in);
    handles = new HandleTable(10);
    vlist = new ValidationList();
    enableOverride = false;
    readStreamHeader();                  //// <== This function
    bin.setBlockDataMode(true);
}

readStreamHeader();)

So be careful about what happens in server side, maybe problem isn't where you expecting it!

Mohammad Jafar Mashhadi
  • 4,102
  • 3
  • 29
  • 49
  • 3
    You shouldn't have constructed the object streams in the accept() loop in the first place. You should construct them in the run() method of the connection-handling thread. Otherwise you run the risk of blocking the accept() loop on the I/O for the stream headers, which will block further clients. – user207421 Jul 06 '13 at 21:22