简体   繁体   English

java.net.Socket.connect超时的准确性如何?

[英]What's the accuracy of java.net.Socket.connect timeouts?

I was wondering what's the timeout accuracy of the method java.net.Socket.connect(SocketAddress, int timeout) ? 我想知道方法java.net.Socket.connect(SocketAddress,int timeout)的超时精度是多少?

Initially I'd thought that it's built atop java.lang.Object.wait and thus have a 10-15 millisecond error ( source ), 最初,我认为它是建立在java.lang.Object.wait之上的,因此会有10到15毫秒的错误( source ),

But after examining the source code, it looks like all it does is to delegate the call to java.net.SocketImpl.connect(SocketaAddress, int) . 但是在检查了源代码之后,看起来它所做的全部就是将调用委派给java.net.SocketImpl.connect(SocketaAddress,int)

Does this mean that java.net.Socket.connect(SocketAddress, int) does not use Object.wait and hence is not subject to the 10-15 millisecond error Object.wait has? 这是否意味着java.net.Socket.connect(SocketAddress,int)不使用Object.wait,因此不受Object.wait的10-15毫秒错误的影响?

Firstly, I would expect that the socket connect timeout is handled directly by the OS/kernel. 首先,我希望套接字连接超时由OS /内核直接处理。 The C socket API supports connect timeouts, so I expect the JVM native implementation will simply delegate to it. C套接字API支持连接超时,因此我希望JVM本机实现将简单地委派给它。

Secondly, why do you care about accuracy of 10-15ms on a network connect? 其次,为什么您要关心网络连接上10-15ms的精度? It sounds like you'll end up with something very brittle. 听起来您最终会变得很脆。

it looks like all it does is to delegate the call to java.net.SocketImpl.connect(SocketaAddress, int). 看起来它所要做的就是将调用委派给java.net.SocketImpl.connect(SocketaAddress,int)。

which is an abstract method of the SocketImpl class. 这是SocketImpl类的抽象方法。

The subclass actually implementing it (the system-default SocketImpl implicitly retrieved from SocketImplFactory.createSocketImpl() in the Socket constructor) in turn relies on a native method, so it's not possible to know the inaccuracy in a platform-independent way. 实际实现它的子类(从Socket构造函数中从SocketImplFactory.createSocketImpl()隐式检索到的系统默认SocketImpl )又依赖于本机方法,因此无法以独立于平台的方式知道不准确性。

-- EDIT (response to comment) -编辑(回应评论)

If not using a Socket subclass that specifies a custom SocketImpl via the protected Socket(SocketImpl impl) constructor, the standard Socket instance created by the Socket() constructor uses a SocksSocketImpl (which in turn extends PlainSocketImpl ). 如果未使用通过受保护的Socket(SocketImpl impl)构造函数指定自定义SocketImplSocket子类,则由Socket()构造函数创建的标准Socket实例将使用SocksSocketImpl (这又扩展了PlainSocketImpl )。

SocksSocketImpl.connect(SocketAddress address, int timeout)

calls 来电

super.connect(SocketAddress address, int timeout) ( PlainSocketImpl.connect(SocketAddress address, int timeout) ), super.connect(SocketAddress address, int timeout)PlainSocketImpl.connect(SocketAddress address, int timeout) ),

which in turn calls 依次调用

PlainSocketImpl.connectToAddress(InetAddress address, int port, int timeout) , PlainSocketImpl.connectToAddress(InetAddress address, int port, int timeout)

which in turn calls 依次调用

PlainSocketImpl.doConnect(InetAddress address, int port, int timeout) , PlainSocketImpl.doConnect(InetAddress address, int port, int timeout)

which in turn calls 依次调用

PlainSocketImpl.socketConnect(InetAddress address, int port, int timeout) , PlainSocketImpl.socketConnect(InetAddress address, int port, int timeout)

which is a private native method, and we don't know what's inside :) 这是一个私有的本地方法,我们不知道里面有什么:)

So no, we're not relying upon Object.wait . 因此,不,我们依赖Object.wait

-- -

See http://jcs.mobile-utopia.com/jcs/18846_PlainSocketImpl.java and http://jcs.mobile-utopia.com/jcs/31401_SocksSocketImpl.java for source code 有关源代码,请参见http://jcs.mobile-utopia.com/jcs/18846_PlainSocketImpl.javahttp://jcs.mobile-utopia.com/jcs/31401_SocksSocketImpl.java

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM