java中socket 默认超时时间

MS:永远吧
Java代码 复制代码 收藏代码
  1. connect
  2. publicvoidconnect(SocketAddressendpoint,
  3. inttimeout)
  4. throwsIOException
  5. Connectsthissockettotheserverwithaspecifiedtimeoutvalue.
  6. Atimeoutofzeroisinterpretedasaninfinitetimeout.
  7. Theconnectionwillthenblockuntilestablishedoranerroroccurs.
  8. Parameters:
  9. endpoint-theSocketAddress
  10. timeout-thetimeoutvaluetobeusedinmilliseconds.
  11. Throws:
  12. IOException-ifanerroroccursduringtheconnection
  13. SocketTimeoutException-iftimeoutexpiresbeforeconnecting
  14. IllegalBlockingModeException-
  15. ifthissockethasanassociatedchannel,andthechannelisinnon-blockingmode
  16. IllegalArgumentException-
  17. ifendpointisnullorisaSocketAddresssubclassnotsupportedbythissocket
  18. Since:
  19. 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 :java.net.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,在java.net包中。

<wbr></wbr>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值