13
InetAddress host = InetAddress.getLocalHost();
Socket link = new Socket(host, Integer.parseInt(args[0]));
System.out.println("before input stream");
ObjectInputStream in = new ObjectInputStream(link.getInputStream());
System.out.println("before output stream");
ObjectInputStream out = new ObjectOutputStream(link.getOutputStream());

"before input stream" is the last lifesign on cmd-line. There is no Exception thrown. Why is this happening? I don't understand...

args[0] is 5000. //edit: flush doesn't help.

5
  • have you run it from the debugger? perhaps that'll break into the code and give you an idea of whats going on. Commented Nov 18, 2011 at 17:14
  • @Brian It connects automatically in the constructor. Commented Nov 18, 2011 at 17:16
  • @user Last line in your code doesn't compile. ..Input.. out = new ..Output.. Commented Nov 18, 2011 at 17:17
  • Obviously my memory failed me. I stand corrected :) Commented Nov 18, 2011 at 17:17
  • Also: ObjectInputStream out = new ObjectOutputStream(link.getOutputStream()); Commented Nov 18, 2011 at 17:17

2 Answers 2

22

This is because the ObjectInputStream(InputStream in)-constructor is a blocking-call if the inputStream is empty.

Quote:

Creates an ObjectInputStream that reads from the specified InputStream. A serialization stream header is read from the stream and verified. This constructor will block until the corresponding ObjectOutputStream has written and flushed the header.

Sign up to request clarification or add additional context in comments.

3 Comments

so if i make the outputstream on the client and on the server side first, that should work? interessting.... i will give it a try. thanks.
well yes. if I exchange the creation of the streams it works. like written below, the ObjectInputStream out-thing is just a typo. thanks@all!
Very basic thing but very necessary to know. Thanks man you saved my days
0

Possibly,

link.getInputStream(); 

could be returning null, though that should return an error by looking at the class files. Another thing I noticed was, you declare:

ObjectInputStream out = new ObjectOutputStream(link.getOutputStream());

From this, you are stating a ObjectInputStream as a ObjectOutputStream without a cast (Would not be appropriate here anyways)

you should try:

ObjectOutputStream out = new ObjectOutputStream(link.getOutputStream());

This should work, as the script may queue the System.out, but notice the error before it can be initialized.

Tell me if this works :D

Comments

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.