Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How are sockets implemented in JVM?

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!

like image 391
guerda Avatar asked Jan 13 '09 06:01

guerda


People also ask

How are sockets implemented?

On the Internet, stream sockets are typically implemented using TCP so that applications can run across any networks using TCP/IP protocol. Raw sockets. Allow direct sending and receiving of IP packets without any protocol-specific transport layer formatting.

How do sockets work in java?

A socket is one endpoint of a two-way communication link between two programs running on the network. A socket is bound to a port number so that the TCP layer can identify the application that data is destined to be sent to. An endpoint is a combination of an IP address and a port number.

How is TCP socket implemented?

TCP sockets are used for communication between a server and a client process. The server's code runs first, which opens a port and listens for incoming connection requests from clients. Once a client connects to the same (server) port, the client or server may send a message.


3 Answers

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.

like image 97
Rob Avatar answered Sep 19 '22 22:09

Rob


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

like image 36
OscarRyz Avatar answered Sep 18 '22 22:09

OscarRyz


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.

like image 31
guerda Avatar answered Sep 19 '22 22:09

guerda