Java sockets: program stops in socket.getInputStream () without error?

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 the input stream" is the last lifesign on the cmd line. An exception is thrown. Why is this happening? I do not understand...

args [0] - 5000. // edit: flush does not help.

+7
source share
2 answers

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

Quote :

Creates an ObjectInputStream that reads from the specified InputStream. The serialization stream header is read from the stream and checked. This constructor blocks until the corresponding ObjectOutputStream writes and discards the header.

+16
source

Maybe,

 link.getInputStream(); 

may return null, although this should return an error while looking at class files. One more thing I noticed, you state:

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

From this, you specify the ObjectInputStream as an ObjectOutputStream without actuation (it doesn't work anyway)

You must try:

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

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

Tell me if this works: D

0
source

All Articles