<<Effective TCP/IP Programming>>读书笔记

这是一篇关于《Effective TCP/IP Programming》的读书笔记,涵盖了理解TCP/IP连接与无连接的区别、子网与CIDR、TCP性能及可靠性、协议工作原理等多个关键技巧,旨在帮助开发者更好地理解和应用TCP/IP协议。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

技巧1 理解基于连接和无连接协议之间的差异

TCP/IP分为四层,分别是接口层/物理层/链路层-->网络层-->传输层-->应用层
网络层主要就是IP层,该层提供了一个很好的高效的,不可靠的,无连接的服务,负责接收来自上层的数据包,然后封装
到一个IP数据包中,并路由该数据包到正确的硬件接口上。
TCP在IP层上增加了三个服务,以用来实现TCP的可靠的面向连接的服务:
1.为TCP段提供了校验位,这就能保证到达目的地的数据不会在网络上传输时被破坏
2.为每个字节分配一个序列号,如果数据不按顺序到达目的地那么接受者可以重新进行组合
3.TCP提供了一个确认和重传机制来保证每一个段最终都会递交到目的地

UDP在IP层上增加了两个服务:
1.校验和
2.端口 提供了一个多路输出数据到适当的应用程序的方法

技巧2 理解子网和CIDR

A类网络: 0.0.0.0 ~ 127.255.255.255  网络位8位,第0位是0
B类网络: 128.0.0.0 ~ 191.255.255.255 网络位16位 第0位为1
C类网络: 192.0.0.0 ~ 223.255.255.255 网络位24位 第0,1位为1
还有D类网络主要是用于多点广播地址
E类网络是保留的

广播类型:
1.有线广播 255.255.255.255 不会被路由器转发
2.网络直接广播 主机位全是1,会被传递给网络中所有主机
3.子网直接广播 
4.全子网直接广播

技巧3 理解私有地址和NAT

私有地址:
1.10.0.0.0 ~ 10.255.255.255 (10/8 前缀)
2.172.16.0.0 ~ 172.31.255.255 (172.16/12 前缀)
3.192.168.0.0 ~ 192.168.255.255 (192.168/16 前缀)
NAT转换机制:
1.静态模式 一一映射
2.缓冲池模式 多对多映射
3.PAT模式 单一全局分配地址,通过端口来区分不同的私有地址

技巧4 开发和使用应用程序框架

建立自己的socket开发框架,屏蔽底层细节

技巧5 选择套接字接口而不是XTI/TLI

技巧6 记住TCP是一个流协议

对于TCP来说,没有用户可见的数据包的概念,需要用户指定协议,通过协议对TCP的数据流进行切分,
划分是数据包的概念。

技巧7 不要低估TCP的性能

UDP并不一定比TCP提供更好的性能,许多因素都可以影响两个协议的相对性能。

技巧8 不要彻底改造TCP

技巧9 注意TCP是可靠的协议,但并非是不会出错的协议

发送方的TCP层对于到达接收方的TCP段是不会做任何保证的,接受方的TCP层唯一知道的是这个数据段
达到了,但是这个段可能是被破坏的,可能是重复的数据,可能是乱序的,还有一些其它可能。
然后接受方将会对发送方的TCP做出保证,该保证就是它确认的任何数据以及在它之前到达的所有数据在TCP层上
以及正确接收到了,但是这并不意味这该数据已经传递给应用程序了,也不意味它一定会传递到。例如,接收方
主机可能在确认数据之后但是在应用程序读取数据之前崩溃。因此编写应用程序的人需要考虑到这一失败模式。
TCP层给予应用程序的保证就是达到的数据是有序的,并且是没有被破坏的。


失败模式:
    1.TCP确认的数据实际上有可能不会达到它的目的应用程序
    2.网络中断(只要双方保持连接,TCP就能保证按顺序递交数据并且数据不会被破坏)

发生连接中断的情况:  
    1.可能发生永久的或暂时的网络紊乱
        发送端重发报文,直到发送方TCP放弃,(在报文重发的这段时间进行写操作的时候是正常的,写操作其实是
        将数据拷贝到了内核缓冲区,由内核负责进行发送)当超时重发结束后链接会被中断此时如果有读操作挂起,
        就返回一个错误条件设置errno为ETIEMDOUT,如果没有挂起的读操作,那么下一个写操作就会失败,设置errno为
        ETIMEDOUT,再进行一次写操作就会返回一个SIGPIPE信号,如果该信号被忽略或捕捉到了就返回一个EPIPE错误。
        如果数据包无法转发到目的主机,会给原来的主机发送一个ICMP消息说明网络或主机不可达到。
        设置errno为ENETUNREACH或E
D:\biancheng\yf服务器代码\jdwlw>mvn clean package [INFO] Scanning for projects... [WARNING] [WARNING] Some problems were encountered while building the effective model for jdwlw:jdwlw:war:0.0.1-SNAPSHOT [WARNING] 'build.plugins.plugin.version' for org.apache.maven.plugins:maven-compiler-plugin is missing. @ line 365, column 12 [WARNING] [WARNING] It is highly recommended to fix these problems because they threaten the stability of your build. [WARNING] [WARNING] For this reason, future Maven versions might no longer support building such malformed projects. [WARNING] [INFO] [INFO] ----------------------------< jdwlw:jdwlw >----------------------------- [INFO] Building maven Maven Webapp 0.0.1-SNAPSHOT [INFO] from pom.xml [INFO] --------------------------------[ war ]--------------------------------- Downloading from aliyun: https://maven.aliyun.com/repository/public/com/google/code/ksoap2/ksoap2-base/3.6.4/ksoap2-base-3.6.4.pom [WARNING] The POM for com.google.code.ksoap2:ksoap2-base:jar:3.6.4 is missing, no dependency information available Downloading from aliyun: https://maven.aliyun.com/repository/public/com/google/code/ksoap2/ksoap2-base/3.6.4/ksoap2-base-3.6.4.jar [INFO] ------------------------------------------------------------------------ [INFO] BUILD FAILURE [INFO] ------------------------------------------------------------------------ [INFO] Total time: 1.518 s [INFO] Finished at: 2025-06-22T23:18:56+08:00 [INFO] ------------------------------------------------------------------------ [ERROR] Failed to execute goal on project jdwlw: Could not resolve dependencies for project jdwlw:jdwlw:war:0.0.1-SNAPSHOT: com.google.code.ksoap2:ksoap2-base:jar:3.6.4 was not found in https://jitpack.io during a previous attempt. This failure was cached in the local repository and resolution is not reattempted until the update interval of jitpack.io has elapsed or updates are forced -> [Help 1] [ERROR] [ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch. [ERROR] Re-run Maven using the -X switch to enable full debug logging. [ERROR] [ERROR] For more information about the errors and possible solutions, please read the following articles: [ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/DependencyResolutionException 解决上述报错。pom.xml:<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>jdwlw</groupId> <artifactId>jdwlw</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>war</packaging> <name>maven Maven Webapp</name> <url>http://maven.apache.org</url> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties> <repositories> <repository> <id>jitpack.io</id> <url>https://jitpack.io</url> </repository> <!-- 可选的阿里云镜像加速 --> <repository> <id>aliyun</id> <name>Aliyun Maven</name> <url>https://maven.aliyun.com/repository/public</url> <releases> <enabled>true</enabled> </releases> <snapshots> <enabled>false</enabled> </snapshots> </repository> </repositories> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>3.8.1</version> <scope>test</scope> </dependency> <!-- <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <version>3.1.0</version> </dependency> --> <dependency> <groupId>javax.servlet</groupId> <artifactId>servlet-api</artifactId> <version>2.5</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>4.1.6.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>4.1.6.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>4.1.6.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-beans</artifactId> <version>4.1.6.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-aop</artifactId> <version>4.1.6.RELEASE</version> </dependency> <!-- https://mvnrepository.com/artifact/org.ksoap2/ksoap2 --> <dependency> <groupId>com.google.code.ksoap2</groupId> <artifactId>ksoap2-base</artifactId> <version>3.6.4</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId> <version>4.1.6.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-web</artifactId> <version>4.1.6.RELEASE</version> </dependency> <!-- <dependency> <groupId>org.springframework</groupId> <artifactId>spring-orm</artifactId> <version>4.1.6.RELEASE</version> </dependency> --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-orm</artifactId> <version>3.2.13.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context-support</artifactId> <version>3.2.2.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc-portlet</artifactId> <version>4.1.6.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-expression</artifactId> <version>4.1.6.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-aspects</artifactId> <version>4.1.6.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-websocket</artifactId> <version>4.1.6.RELEASE</version> </dependency> <dependency> <groupId>quartz</groupId> <artifactId>quartz</artifactId> <version>1.5.2</version> </dependency> <dependency> <groupId>org.apache.ibatis</groupId> <artifactId>ibatis-sqlmap</artifactId> <version>2.3.4.726</version> </dependency> <!-- <dependency>--> <!-- <groupId>com.ibatis</groupId>--> <!-- <artifactId>ibatis2-sqlmap</artifactId>--> <!-- <version>2.1.7.597</version>--> <!-- </dependency>--> <dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>1.2.17</version> </dependency> <dependency> <groupId>commons-io</groupId> <artifactId>commons-io</artifactId> <version>2.4</version> </dependency> <dependency> <groupId>commons-logging</groupId> <artifactId>commons-logging</artifactId> <version>1.2</version> </dependency> <dependency> <groupId>commons-lang</groupId> <artifactId>commons-lang</artifactId> <version>2.6</version> </dependency> <dependency> <groupId>commons-dbcp</groupId> <artifactId>commons-dbcp</artifactId> <version>1.4</version> </dependency> <dependency> <groupId>commons-pool</groupId> <artifactId>commons-pool</artifactId> <version>1.6</version> </dependency> <dependency> <groupId>commons-fileupload</groupId> <artifactId>commons-fileupload</artifactId> <version>1.3.1</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.35</version> </dependency> <dependency> <groupId>com.belerweb</groupId> <artifactId>pinyin4j</artifactId> <version>2.5.0</version> </dependency> <dependency> <groupId>jstl</groupId> <artifactId>jstl</artifactId> <version>1.2</version> </dependency> <dependency> <groupId>com.google.code.gson</groupId> <artifactId>gson</artifactId> <version>2.5</version> </dependency> <dependency> <groupId>org.apache.httpcomponents</groupId> <artifactId>httpclient</artifactId> <version>4.4.1</version> </dependency> <dependency> <groupId>javax.mail</groupId> <artifactId>mail</artifactId> <version>1.5.0-b01</version> </dependency> <!-- 激光消息推送Starts --> <dependency> <groupId>cn.jpush.api</groupId> <artifactId>jpush-client</artifactId> <version>3.1.3</version> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-api</artifactId> <version>1.7.5</version> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-log4j12</artifactId> <version>1.7.5</version> </dependency> <dependency> <groupId>com.google.guava</groupId> <artifactId>guava</artifactId> <version>17.0</version> </dependency> <dependency> <groupId>com.squareup.okhttp</groupId> <artifactId>mockwebserver</artifactId> <version>1.5.4</version> <scope>test</scope> </dependency> <!-- 激光消息推送END --> <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi</artifactId> <version>3.11</version> </dependency> <dependency> <groupId>net.sf.ehcache</groupId> <artifactId>ehcache</artifactId> <version>2.10.0</version> </dependency> <dependency> <groupId>javax.servlet.jsp</groupId> <artifactId>jsp-api</artifactId> <version>2.2</version> </dependency> <dependency> <groupId>com.lowagie</groupId> <artifactId>itext</artifactId> <version>2.1.7</version> </dependency> <!-- https://mvnrepository.com/artifact/commons-net/commons-net --> <dependency> <groupId>commons-net</groupId> <artifactId>commons-net</artifactId> <version>3.3</version> </dependency> <!-- https://mvnrepository.com/artifact/com.mchange/c3p0 --> <dependency> <groupId>com.mchange</groupId> <artifactId>c3p0</artifactId> <version>0.9.5.2</version> </dependency> <!-- https://mvnrepository.com/artifact/org.apache.ws.commons.axiom/axiom-api --> <dependency> <groupId>org.apache.ws.commons.axiom</groupId> <artifactId>axiom-api</artifactId> <version>1.2.13</version> </dependency> <!-- https://mvnrepository.com/artifact/dom4j/dom4j --> <dependency> <groupId>dom4j</groupId> <artifactId>dom4j</artifactId> <version>1.6.1</version> </dependency> <!-- https://mvnrepository.com/artifact/org.apache.axis/axis --> <dependency> <groupId>org.apache.axis</groupId> <artifactId>axis</artifactId> <version>1.4</version> </dependency> <!-- https://mvnrepository.com/artifact/javax.xml.rpc/javax.xml.rpc-api --> <dependency> <groupId>javax.xml.rpc</groupId> <artifactId>javax.xml.rpc-api</artifactId> <version>1.1.1</version> </dependency> <!-- https://mvnrepository.com/artifact/org.apache.axis2/axis2 --> <dependency> <groupId>org.apache.axis2</groupId> <artifactId>axis2</artifactId> <version>1.7.6</version> <type>pom</type> </dependency> <!-- https://mvnrepository.com/artifact/org.apache.axis2/axis2-adb --> <dependency> <groupId>org.apache.axis2</groupId> <artifactId>axis2-adb</artifactId> <version>1.7.6</version> </dependency> <!-- https://mvnrepository.com/artifact/soap/soap --> <dependency> <groupId>soap</groupId> <artifactId>soap</artifactId> <version>2.3</version> </dependency> <!-- https://mvnrepository.com/artifact/commons-httpclient/commons-httpclient --> <dependency> <groupId>commons-httpclient</groupId> <artifactId>commons-httpclient</artifactId> <version>3.1</version> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> <version>2.9.10.4</version> </dependency> <dependency> <groupId>org.eclipse.paho</groupId> <artifactId>org.eclipse.paho.client.mqttv3</artifactId> <version>1.2.0</version> </dependency> <dependency> <groupId>cn.hutool</groupId> <artifactId>hutool-all</artifactId> <version>5.7.2</version> </dependency> </dependencies> <build> <finalName>jdwlw</finalName> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <configuration> <source>1.8</source> <target>1.8</target> <encoding>utf-8</encoding> <compilerArguments> <extdirs>${project.basedir}/src/main/webapp/WEB-INF/lib</extdirs> </compilerArguments> </configuration> </plugin> </plugins> <resources> <resource> <directory>src/main/resources</directory> <includes> <include>**/*.properties</include> <include>**/*.xml</include> </includes> <filtering>false</filtering> </resource> <resource> <directory>src/main/java</directory> <includes> <include>**/*.properties</include> <include>**/*.xml</include> </includes> <filtering>false</filtering> </resource> </resources> </build> </project>
最新发布
06-23
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值