资源说明:第1章简介....................................................................................................................................3
1.1 计算机网络,分组报文和协议........................................................................................3
1.2 关于地址............................................................................................................................6
1.3 关于名字............................................................................................................................8
1.4 客户端和服务器................................................................................................................8
1.5 什么是套接字....................................................................................................................9
1.6 练习..................................................................................................................................10
第2章基本套接字......................................................................................................................10
2.1 套接字地址......................................................................................................................10
2.2 TCP套接字.......................................................................................................................17
2.2.1 TCP客户端............................................................................................................17
2.2.2 TCP服务器端........................................................................................................22
2.2.3 输入输出流...........................................................................................................26
2.3 UDP套接字......................................................................................................................28
2.3.1 DatagramPacket类...............................................................................................28
2.3.2 UDP客户端...........................................................................................................30
2.3.3 UDP服务器端.......................................................................................................36
2.3.4 使用UDP套接字发送和接收信息......................................................................38
2.4 练习..................................................................................................................................40
第3章发送和接收数据...............................................................................................................41
3.1 信息编码..........................................................................................................................42
3.1.1 基本整型...............................................................................................................42
3.1.2 字符串和文本.......................................................................................................48
3.1.3 位操作:布尔值编码...........................................................................................50
3.2 组合输入输出流..............................................................................................................51
3.3 成帧与解析......................................................................................................................52
3.4 Java特定编码...................................................................................................................58
3.5 构建和解析协议消息......................................................................................................59
3.5.1 基于文本的表示方法...........................................................................................62
3.5.2 二进制表示方法...................................................................................................65
3.5.3 发送和接收...........................................................................................................67
3.6 结束..................................................................................................................................76
3.7 练习..................................................................................................................................76
第4章进阶..................................................................................................................................77
4.1 多任务处理......................................................................................................................77
4.1.1 Java 多线程..........................................................................................................78
4.1.2 服务器协议...........................................................................................................80
4.1.3 一客户一线程.......................................................................................................84
4.1.4 线程池...................................................................................................................86
4.1.5 系统管理调度:Executor接口...........................................................................89
4.2 阻塞和超时......................................................................................................................91
4.2.1 accept(),read()和receive()....................................................................................91
4.2.2 连接和写数据.......................................................................................................92
4.2.3 限制每个客户端的时间.......................................................................................92
4.3 多接收者..........................................................................................................................94
4.3.1 广播.......................................................................................................................94
4.3.2 多播.......................................................................................................................95
4.4 控制默认行为................................................................................................................100
4.4.1 Keep-Alive...........................................................................................................100
4.4.2 发送和接收缓存区的大小.................................................................................101
4.4.3 超时.....................................................................................................................101
4.4.4 地址重用.............................................................................................................102
4.4.5 消除缓冲延迟.....................................................................................................102
4.4.6 紧急数据.............................................................................................................103
4.4.7 关闭后停留.........................................................................................................103
4.4.8 广播许可.............................................................................................................103
4.4.9 通信等级.............................................................................................................104
4.4.10 基于性能的协议选择.......................................................................................104
4.5 关闭连接........................................................................................................................104
4.6 Applets...........................................................................................................................111
4.7 结束................................................................................................................................112
4.8 练习................................................................................................................................112
第5章 NIO.................................................................................................................................112
5.1 为什么需要NIO?........................................................................................................113
5.2 与Buffer一起使用Channel..........................................................................................115
5.3 Selector...........................................................................................................................118
5.4 Buffer详解.....................................................................................................................125
5.4.1 Buffer索引...........................................................................................................125
5.4.2 创建Buffer..........................................................................................................126
5.4.3 存储和接收数据.................................................................................................128
5.4.4 准备Buffer:clear(),flip(),和rewind()........................................................130
5.4.5 压缩Buffer中的数据..........................................................................................132
5.4.6 Buffer透视:duplicate(),slice()等..................................................................134
5.4.7 字符编码.............................................................................................................136
5.5 流(TCP)信道详解....................................................................................................136
5.6 Selector详解...................................................................................................................139
5.6.1 在信道中注册.....................................................................................................139
5.6.2 选取和识别准备就绪的信道.............................................................................141
5.6.3 信道附件.............................................................................................................143
5.6.4 Selector小结........................................................................................................144
5.7 数据报(UDP)信道...................................................................................................144
5.8 练习................................................................................................................................149
1. 使用定长的写缓冲区改写TCPEchoClientNonblocking.java。....................................149
2.使用Buffer和DatagramChannel编写一个回显客户端。................................................149
第6章深入剖析........................................................................................................................149
6.1 缓冲和TCP....................................................................................................................152
6.2 死锁风险........................................................................................................................155
6.3 性能相关........................................................................................................................158
6.4 TCP套接字的生存周期.................................................................................................158
6.4.1 连接.....................................................................................................................158
6.4.2 关闭TCP连接.....................................................................................................164
6.5 解调多路复用揭秘........................................................................................................167
6.6 练习................................................................................................................................169
本源码包内暂不包含可直接显示的源代码文件,请下载源码包。