(已完成)java socket-客户端套接字连接过程 源码分析.md

应用程序

Socket客户端套接字.连接方法()

www.runoob.com/java/net-se… demo

java net包-socket

1.SocketImpl1

class SocksSocketImpl extends PlainSocketImpl implements SocksConsts {

 


复制代码

2.SocketImpl2

/*
 * This class PlainSocketImpl simply delegates to the appropriate real
 * SocketImpl. We do this because PlainSocketImpl is already extended
 * by SocksSocketImpl.
 * <p>
 * There are two possibilities for the real SocketImpl,
 * TwoStacksPlainSocketImpl or DualStackPlainSocketImpl. We use
 * DualStackPlainSocketImpl on systems that have a dual stack
 * TCP implementation. Otherwise we create an instance of
 * TwoStacksPlainSocketImpl and delegate to it.
 *
 * @author Chris Hegarty
 */
class PlainSocketImpl extends AbstractPlainSocketImpl

{


 /**

     * Constructs an empty instance.

     */

    PlainSocketImpl() {

        if (useDualStackImpl) {

            impl = new DualStackPlainSocketImpl(exclusiveBind);

        } else {

            impl = new TwoStacksPlainSocketImpl(exclusiveBind);

        }

    }


复制代码

//https://github.com/frohoff/jdk8u-dev-jdk/blob/master/src/windows/classes/java/net/DualStackPlainSocketImpl.java

void socketConnect(InetAddress address, int port, int timeout)
        throws IOException {
        int nativefd = checkAndReturnNativeFD();

        if (address == null)
            throw new NullPointerException("inet address argument is null.");

        int connectResult;
        if (timeout <= 0) {
            connectResult = connect0(nativefd, address, port);
        } else {
            configureBlocking(nativefd, false);
            try {
                connectResult = connect0(nativefd, address, port);
                if (connectResult == WOULDBLOCK) {
                    waitForConnect(nativefd, timeout);
                }
            } finally {
                configureBlocking(nativefd, true);
            }
        }
        /*
         * We need to set the local port field. If bind was called
         * previous to the connect (by the client) then localport field
         * will already be set.
         */
        if (localport == 0)
            localport = localPort0(nativefd);
    }
复制代码
static native int connect0(int fd, InetAddress remote, int remotePort)
throws IOException; //最终的话 都是调用 操作系统的套接字连接,具体流程是:jdk socket——》jvm native——》操作系统
复制代码

总结

1.关于包
socket所有的类都在java.net包。

nio类都在jdk nio包。

2.流程
最终的话 都是调用 操作系统的套接字连接,具体流程是:jdk socket——》jvm native——》操作系统

转载于:https://juejin.im/post/5c4b93c66fb9a049c15f9650

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值