7

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!

guerda
  • 23,388
  • 27
  • 97
  • 146

3 Answers3

5

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.

Rob
  • 47,999
  • 5
  • 74
  • 91
5

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

David Schmitt
  • 58,259
  • 26
  • 121
  • 165
OscarRyz
  • 196,001
  • 113
  • 385
  • 569
  • It's hard to mark this question as the answer, because I need an official citation o.s.e. +1 of course – guerda Jan 13 '09 at 12:14
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.

guerda
  • 23,388
  • 27
  • 97
  • 146
  • Well, from the source I can tell that method is "native" thus uses a native library that could be either in C, C++ or assembly. As for the quote, documentation is not running software. :) What is the rationale of your question anyway? – OscarRyz Jan 14 '09 at 01:43
  • I need to compare network programming techniques in Linux and Windows of Java and Python. – guerda Jan 16 '09 at 06:52