简体   繁体   English

Java如何处理底层套接字网络逻辑

[英]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)] 请让我指出源代码,其中Java使用[DllImport(WS2_32,SetLastError = true)]处理C#这样的低级套接字网络逻辑

I am trying to understand how Java handles low level networking logic like C# does using DLLImport. 我试图了解Java如何使用DLLImport处理C#这样的底层网络逻辑。

Finding hard time to trace the implementation of SocketImpl or SocketImplFactory logic in java.net.Socket source code. 很难在java.net.Socket源代码中跟踪SocketImpl或SocketImplFactory逻辑的实现。 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. 即使我能够将其追溯到SocketImpl的AbstractPlainSocketImpl.java 隐含形式 ,似乎也没有实现connect0来进行更深入的研究。

You should look for concrete child classes of java.net.SocketImpl like DualStackPlainSocketImpl . 您应该寻找像DualStackPlainSocketImpl这样的java.net.SocketImpl的具体子类。

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: 本机代码通常是特定于平台的,并由C ++或C实现。OpenJDK / jdk8u的DualStackPlainSocketImpl.c中的一个Windows示例:

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. 在示例中, connnect()调用可能正是您要查找的级别:Windows中的WinSock2 API调用 From Linux sources you will find similar connnect() system call. 从Linux来源,您会发现类似的connnect()系统调用。

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

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