Java套接字:程序停在socket.getInputStream()没有错误?

时间:2021-09-06 04:59:46
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...

“输入流之前”是cmd-line上的最后一个生命值。没有抛出异常。为什么会这样?我不明白......

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

args [0]是5000. //编辑:flush没有帮助。

2 个解决方案

#1


17  

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

这是因为如果inputStream为空,则ObjectInputStream(InputStream in)-constructor是一个阻塞调用。

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.

创建一个从指定的InputStream读取的ObjectInputStream。从流中读取序列化流头并进行验证。此构造函数将阻塞,直到相应的ObjectOutputStream已写入并刷新标头。

#2


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:

可能会返回null,但是应该通过查看类文件返回错误。我注意到的另一件事是,你声明:

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)

从这里开始,您将ObjectInputStream声明为没有强制转换的ObjectOutputStream(此处不合适)

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.

这应该有效,因为脚本可能会对System.out进行排队,但在初始化之前请注意错误。

Tell me if this works :D

告诉我这是否有效:D

#1


17  

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

这是因为如果inputStream为空,则ObjectInputStream(InputStream in)-constructor是一个阻塞调用。

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.

创建一个从指定的InputStream读取的ObjectInputStream。从流中读取序列化流头并进行验证。此构造函数将阻塞,直到相应的ObjectOutputStream已写入并刷新标头。

#2


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:

可能会返回null,但是应该通过查看类文件返回错误。我注意到的另一件事是,你声明:

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)

从这里开始,您将ObjectInputStream声明为没有强制转换的ObjectOutputStream(此处不合适)

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.

这应该有效,因为脚本可能会对System.out进行排队,但在初始化之前请注意错误。

Tell me if this works :D

告诉我这是否有效:D