How are sockets implemented in JVM?

家住魔仙堡 提交于 2020-08-02 08:26:28

问题


I want to know, how sockets are implemented in the Java Virtual Machine.

  • Is there a native library included?
  • And if, is it a C library?

Where can I find information about this topic? The offical Java tutorial on networking does not help me there.

Some interesting links would help.

Update: Are there any official information provided by Sun?

Thanks in advance!

Edit I found a proof, I mark my answer as the correct one. Thanks to Oscar, that was the perfect hint for me!!! THANKS!


回答1:


Probably using the system-V socket calls exposed by the underlying platform, e.g. system-V under Unix, WinSock under Windows. The specification only dictates how a virtual machine must behave, so implementers are free to do as they wish.




回答2:


Java is open source, so you can grab the source code for a self paced deep examination of the class.

As an start and quick answer to your question here's what I've found in a very quick scan:

private native void socketConnect(InetAddress address, int port, int timeout)

So yeap, a native lib is included and my guess it is a C++ library.

There are a number of implementations ( SSLSocket, PlainSocket etc. )

The online source of JDK7 is here :S Not sure how up to date it is

I've used my IDE to navigate the source code that comes with every JDK installation.

IDE screenshot showing the source of Socket http://img83.imageshack.us/img83/5358/socketimpfv3.png




回答3:


In the network guide of Java 1.4.2, an interesting piece of information is provided:

The implementation details...

...that you don't need to know, unless you subclass SocketImpl/DatagramSocketImpl. Every *Socket object has an underlying SocketImpl/DatagramSocketImpl that interfaces to native code. The Impl classes implement two methods to support options:

void setOption(int optID, Object val) throws SocketException; Object getOption(int optID) throws SocketException;

that look much like C. These methods act as glue to the native methods, and ensure type safety before native methods are invoked.

So I think it's proofed: Java uses native libraries for sockets.



来源:https://stackoverflow.com/questions/438154/how-are-sockets-implemented-in-jvm

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!