博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
(已完成)java socket-客户端套接字连接过程 源码分析.md
阅读量:6894 次
发布时间:2019-06-27

本文共 2366 字,大约阅读时间需要 7 分钟。

应用程序

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

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. * 

* 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

你可能感兴趣的文章
Android模拟器——Genymotion(很快)
查看>>
学习规划
查看>>
SpringMVC项目使用Thymeleaf模板引擎
查看>>
定义和使用SQL变量
查看>>
12.21 php-fpm的pool12.22 php-fpm慢执行日志12.23 open_basedir12.24 php-fpm进程管理
查看>>
Supervisor进程管理软件的安装与配置
查看>>
细谈 vue - transition 篇
查看>>
Ubuntn中获取仓库中的工具源码与构建
查看>>
Html Dom getElementsByClassName
查看>>
Android 中文 API ---- tabhost使用方法一(tabwidget+framlayout)
查看>>
Kubernetes生产环境经验告诉你如何实现蓝绿部署和负载均衡
查看>>
go 缓存机制
查看>>
P2P路由模式的概念和优势
查看>>
wangframe如何扩展?
查看>>
7.Spring Boot配置文件application.yml
查看>>
计算学校周次,亲测成功!
查看>>
Centos 7 可安装 mysql5.7
查看>>
雷神2—狂热视觉震撼
查看>>
node.js实现多图片上传
查看>>
could not bind to address 0.0.0.0:443 no listening sockets available, shutting d
查看>>