首次连接失败后插座未连接 [英] Socket not connecting after first connection failure
问题描述
我不明白为什么以下程序在初始连接失败时不起作用(我在第一次连接失败后故意启动服务器套接字):
I don't understand why the following program doesn't work when the initial connect fails (I intentionally start the server socket after the first connect fails):
Socket client = new Socket();
while (true) {
try {
client.connect(address);
break;
} catch (IOException e) {
Thread.sleep(1000);
}
}
如果我移动客户端= while循环中的新Socket();
,它可以工作,但我在文档中找不到任何说明如果初始连接失败,套接字被损坏并且必须被回收。
If I move the client = new Socket();
inside the while loop, it works, but I couldn't find anything in the documentation that states that if an initial connect fails the socket is "broken" and has to be recycled.
推荐答案
您无法重新连接已连接失败的套接字。您必须关闭它并使用新套接字重试。您正在寻找的文档将是Berkeley套接字API级别,而不是Java,
You can't reconnect a socket that has already failed to connect. You have to close it and try again with a new socket. The documentation you're looking for would be at the Berkeley Sockets API level, not Java,
这篇关于首次连接失败后插座未连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!