设置套接字操作的超时

时间:2022-10-14 22:42:13

When I create a socket:

当我创建一个套接字时:

Socket socket = new Socket(ipAddress, port);

It throws an exception, which is OK, because the IP address is not available. (The test variables where String ipAddress = "192.168.0.3" and int port = 300.)

它会引发异常,这是正常的,因为IP地址不可用。 (测试变量,其中String ipAddress =“192.168.0.3”和int port = 300.)

The problem is: how do I set it to timeout for that socket?

问题是:如何将其设置为该套接字的超时?

When I create the socket, how do I reduce the time before I get a UnknownHostException and get the socket to timeout?

当我创建套接字时,如何在收到UnknownHostException并使套接字超时之前减少时间?

5 个解决方案

#1


135  

Use the Socket() constructor, and connect(SocketAddress endpoint, int timeout) method instead.

使用Socket()构造函数,然后连接(SocketAddress endpoint,int timeout)方法。

In your case it would look something like:

在你的情况下,它看起来像:

Socket socket = new Socket();
socket.connect(new InetSocketAddress(ipAddress, port), 1000);

Quoting from the documentation

引用文档

connect

public void connect(SocketAddress endpoint, int timeout) throws IOException

Connects this socket to the server with a specified timeout value. A timeout of zero is interpreted as an infinite timeout. The connection will then block until established or an error occurs.

使用指定的超时值将此套接字连接到服务器。超时为零被解释为无限超时。然后,连接将阻塞,直到建立或发生错误。

Parameters:

参数:

endpoint - the SocketAddress
timeout - the timeout value to be used in milliseconds.

endpoint - SocketAddress timeout - 要使用的超时值,以毫秒为单位。

Throws:

抛出:

IOException - if an error occurs during the connection
SocketTimeoutException - if timeout expires before connecting
IllegalBlockingModeException - if this socket has an associated channel, and the channel is in non-blocking mode
IllegalArgumentException - if endpoint is null or is a SocketAddress subclass not supported by this socket

IOException - 如果连接期间发生错误SocketTimeoutException - 如果超时在连接IllegalBlockingModeException之前到期 - 如果此套接字具有关联的通道,并且通道处于非阻塞模式IllegalArgumentException - 如果endpoint为null或者是此类不支持的SocketAddress子类插座

Since: 1.4

自:1.4

#2


40  

You don't set a timeout for the socket, you set a timeout for the operations you perform on that socket.

您没有为套接字设置超时,您为在该套接字上执行的操作设置了超时。

For example socket.connect(otherAddress, timeout)

例如socket.connect(otherAddress,timeout)

Or socket.setSoTimeout(timeout) for setting a timeout on read() operations.

或者socket.setSoTimeout(timeout)用于在read()操作上设置超时。

See: http://docs.oracle.com/javase/7/docs/api/java/net/Socket.html

请参阅:http://docs.oracle.com/javase/7/docs/api/java/net/Socket.html

#3


18  

You could use the following solution:

您可以使用以下解决方案:

SocketAddress sockaddr = new InetSocketAddress(ip, port);
// Create your socket
Socket socket = new Socket();
// Connect with 10 s timeout
socket.connect(sockaddr, 10000);

Hope it helps!

希望能帮助到你!

#4


8  

You can't control the timeout due to UnknownHostException. These are DNS timings. You can only control the connect timeout given a valid host. None of the preceding answers addresses this point correctly.

由于UnknownHostException,您无法控制超时。这些是DNS计时。给定有效主机时,您只能控制连接超时。前面的答案都没有正确地解决这一问题。

But I find it hard to believe that you are really getting an UnknownHostException when you specify an IP address rather than a hostname.

但是,当您指定IP地址而不是主机名时,我发现很难相信您确实收到了UnknownHostException。

EDIT To control Java's DNS timeouts see this answer.

编辑要控制Java的DNS超时,请参阅此答案。

#5


5  

Use the default constructor for Socket and then use the connect() method.

使用Socket的默认构造函数,然后使用connect()方法。

#1


135  

Use the Socket() constructor, and connect(SocketAddress endpoint, int timeout) method instead.

使用Socket()构造函数,然后连接(SocketAddress endpoint,int timeout)方法。

In your case it would look something like:

在你的情况下,它看起来像:

Socket socket = new Socket();
socket.connect(new InetSocketAddress(ipAddress, port), 1000);

Quoting from the documentation

引用文档

connect

public void connect(SocketAddress endpoint, int timeout) throws IOException

Connects this socket to the server with a specified timeout value. A timeout of zero is interpreted as an infinite timeout. The connection will then block until established or an error occurs.

使用指定的超时值将此套接字连接到服务器。超时为零被解释为无限超时。然后,连接将阻塞,直到建立或发生错误。

Parameters:

参数:

endpoint - the SocketAddress
timeout - the timeout value to be used in milliseconds.

endpoint - SocketAddress timeout - 要使用的超时值,以毫秒为单位。

Throws:

抛出:

IOException - if an error occurs during the connection
SocketTimeoutException - if timeout expires before connecting
IllegalBlockingModeException - if this socket has an associated channel, and the channel is in non-blocking mode
IllegalArgumentException - if endpoint is null or is a SocketAddress subclass not supported by this socket

IOException - 如果连接期间发生错误SocketTimeoutException - 如果超时在连接IllegalBlockingModeException之前到期 - 如果此套接字具有关联的通道,并且通道处于非阻塞模式IllegalArgumentException - 如果endpoint为null或者是此类不支持的SocketAddress子类插座

Since: 1.4

自:1.4

#2


40  

You don't set a timeout for the socket, you set a timeout for the operations you perform on that socket.

您没有为套接字设置超时,您为在该套接字上执行的操作设置了超时。

For example socket.connect(otherAddress, timeout)

例如socket.connect(otherAddress,timeout)

Or socket.setSoTimeout(timeout) for setting a timeout on read() operations.

或者socket.setSoTimeout(timeout)用于在read()操作上设置超时。

See: http://docs.oracle.com/javase/7/docs/api/java/net/Socket.html

请参阅:http://docs.oracle.com/javase/7/docs/api/java/net/Socket.html

#3


18  

You could use the following solution:

您可以使用以下解决方案:

SocketAddress sockaddr = new InetSocketAddress(ip, port);
// Create your socket
Socket socket = new Socket();
// Connect with 10 s timeout
socket.connect(sockaddr, 10000);

Hope it helps!

希望能帮助到你!

#4


8  

You can't control the timeout due to UnknownHostException. These are DNS timings. You can only control the connect timeout given a valid host. None of the preceding answers addresses this point correctly.

由于UnknownHostException,您无法控制超时。这些是DNS计时。给定有效主机时,您只能控制连接超时。前面的答案都没有正确地解决这一问题。

But I find it hard to believe that you are really getting an UnknownHostException when you specify an IP address rather than a hostname.

但是,当您指定IP地址而不是主机名时,我发现很难相信您确实收到了UnknownHostException。

EDIT To control Java's DNS timeouts see this answer.

编辑要控制Java的DNS超时,请参阅此答案。

#5


5  

Use the default constructor for Socket and then use the connect() method.

使用Socket的默认构造函数,然后使用connect()方法。