600字范文,内容丰富有趣,生活中的好帮手!
600字范文 > java socket超时时间_java中socket 默认超时时间 | 学步园

java socket超时时间_java中socket 默认超时时间 | 学步园

时间:2021-09-21 09:41:56

相关推荐

java socket超时时间_java中socket 默认超时时间 | 学步园

MS:永远吧

Java代码

connect

publicvoidconnect(SocketAddressendpoint,

inttimeout)

throwsIOException

Connectsthissockettotheserverwithaspecifiedtimeoutvalue.

Atimeoutofzeroisinterpretedasaninfinitetimeout.

Theconnectionwillthenblockuntilestablishedoranerroroccurs.

Parameters:

endpoint-theSocketAddress

timeout-thetimeoutvaluetobeusedinmilliseconds.

Throws:

IOException-ifanerroroccursduringtheconnection

SocketTimeoutException-iftimeoutexpiresbeforeconnecting

IllegalBlockingModeException-

ifthissockethasanassociatedchannel,andthechannelisinnon-blockingmode

IllegalArgumentException-

ifendpointisnullorisaSocketAddresssubclassnotsupportedbythissocket

Since:

1.4

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.

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

Since:

1.4

选自java API :.Socket

Socket socket = new Socket();

SocketAddress address = new InetSocketAddress("127.0.0.1", 8080);

try {

socket.connect(address, 60000);

SocketTimeoutException

} catch (IOException e) {

e.printStackTrace();

}

使用Socket.connect(SocketAddress sa, int timeout)方法。timeout为毫秒数,为0则永不超时(Socket默认也是如此)。如果到时间,仍然没有连接成功,则抛出SocketTimeoutException,在包中。

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。