user2645138
user2645138

Reputation:

How does Java handle low level Socket Networking logic

Please can some one point me to source code where Java handles low level Socket Networking logic like C# does using [DllImport(WS2_32, SetLastError = true)]

I am trying to understand how Java handles low level networking logic like C# does using DLLImport.

Finding hard time to trace the implementation of SocketImpl or SocketImplFactory logic in java.net.Socket source code. Even if I am able to trace it to AbstractPlainSocketImpl.java implimentation of SocketImpl there seems to be no implementation for connect0 to dig deeper.

Upvotes: 0

Views: 280

Answers (1)

SKi
SKi

Reputation: 8466

You should look for concrete child classes of java.net.SocketImpl like DualStackPlainSocketImpl.

There are places where implementation calls to native functions:

connectResult = connect0(nativefd, address, port);
....
static native int connect0(int fd, InetAddress remote, int remotePort)
    throws IOException;

Native code is quite often platform specific and implemented by C++ or C. One Windows example from DualStackPlainSocketImpl.c of OpenJDK/jdk8u:

JNIEXPORT jint JNICALL Java_java_net_DualStackPlainSocketImpl_connect0
  (JNIEnv *env, jclass clazz, jint fd, jobject iaObj, jint port) 
{
    SOCKETADDRESS sa;
    ...
    rv = connect(fd, (struct sockaddr *)&sa, sa_len);

In the example connnect() call is probably the level what you are looking for: WinSock2 API call in Windows. From Linux sources you will find similar connnect() system call.

Upvotes: 2

Related Questions