(Java学习笔记) Java Networking (Java 网络)
Java Networking (Java 网络)
1. 网络通信协议 Network Communication Protocols
Network Protocol is a set of rules that governs the communications between computers on a network, or rather, rules for sending blocks of data (each known as a Protocol Data Unit (PDU)) from one node in a network to another node. OSI(Open System Interconnect, 开放式系统互联, 一般都叫OSI参考模型) OSI参考模型的分层结构 OSI参考模型(OSI/RM)的全称是开放系统互连参考模型(Open System Interconnection Reference Model,OSI/RM),它是由国际标准化组织(International Standard Organization,ISO)提出的一个网络系统互连模型。 OSI Model是国际标准, 但由于过于完美,各厂商达不到其要求,所以只是参考模型,现在应用最广泛的是TCP/IP Model. TCP/IP不是TCP和IP这两个协议的合称, 而是包括TCP协议(Transmission Control Protocol, 传输控制协议)、IP协议(Internet Protocol, 因特网互联协议)、UDP协议(User Datagram Protocol, 用户数据报协议)、ICMP协议(Internet Control Message Protocol, Internet控制报文协议)和其它一些协议的协议簇(protocol suite)。 |
● OSI和TCP/IP对比
|
|
|
|
|
|
链路层:链路层用于定义物理传输通道,包含对某些网络连接设备的驱动协议,例如针对光纤、双绞线提供的驱动。 Link layer: Link layer is used for defining the physical transmission channels, and consists of driver protocols for some networking connection devices, e.g., optical fibers, twisted-pair cables. 网络层:网络层是整个TCP/IP协议的核心,它主要用于将传输的数据进行分组,将分组数据发送到目标计算机或者网络。 Internet layer: The Internet layer is the core of the whole TCP/IP protocol. It is mainly used for grouping transmitted data, and sends the grouped data to destination computer or Internet. 运输层:主要使网络程序进行通信,在进行网络通信时,可以采用TCP协议,也可以采用UDP协议。 Transport layer: It makes the Internet programs communicate by TCP protocol or UDP protocol. 应用层:主要负责应用程序的协议,例如HTTP协议、FTP协议等。 Application layer: It is responsible for the protocols between application programs, e.g., HTTP, FTP, etc. |
● IP地址
An IP address is a unique number that devices implementing the Internet Protocol use in order to identify each other on a network. |
In IPv4 an address consists of 32 bits (4 octets/Bytes) which limits the address space to 2^32 (4294967296) possible unique addresses. In IPv6 an address consists of 32 bits (16 octets/Bytes) which increases the address space to 2^128 (approximately 3.403×1038) possible unique addresses. |
● TCP vs UDP
TCP is a connection oriented stream over an IP network. It guarantees that all sent packets will reach the destination in the correct order. This imply the use of acknowledgement packets sent back to the sender, and automatic retransmission, causing additional delays and a general less efficient transmission than UDP. UDP a is connection-less protocol. Communication is datagram oriented. The integrity is guaranteed only on the single datagram. Datagrams reach destination and can arrive out of order or don't arrive at all. It is more efficient than TCP because it uses non ACK. It's generally used for real time communication, where a little percentage of packet loss rate is preferable to the overhead of a TCP connection. |
1) TCP is connection oriented and reliable where as UDP is connection less and unreliable. 2) TCP needs more processing at network interface level where as in UDP it's not. 3) TCP uses, 3 way handshake, congestion control, flow control and other mechanism to make sure the reliable transmission. 4) UDP is mostly used in cases where the packet delay is more serious than packet loss. |
● TCP & HTTP(HyperText Transfer Protocol, 超文本传输协议)
HTTP协议通常承载于TCP协议之上,有时也承载于TLS(Transport Layer Security)或SSL(Secure Socket Layer)协议层之上,这个时候,就成了我们常说的HTTPS。 我们在传输数据时,可以只使用(传输层)TCP/IP协议, 但是那样的话,如果没有应用层,便无法识别数据内容,如果想要使传输的数据有意义,则必须使用到应用层协议, 应用层协议有很多,比如HTTP、FTP、TELNET等,也可以自己定义应用层协议。 WEB使用HTTP协议作应用层协议,以封装HTTP文本信息,然后使用TCP/IP做传输层协议将它发到网络上。" |
可以认为TCP/IP协议是搬运工,保证搬动的东西不被损坏, HTTP协议是做业务的,用来决定要不要搬运,以及如何搬运,从哪去搬运 |
● Socket(套接字), Port(端口)
※ Socket是(排插型)插座, Port各个插口 |
A port is an endpoint of communication in an operating system, and it can be thought of as a doorway into a computer. Network Applications that use the TCP/IP suite utilize sockets to communicate with one another. A socket is an agreed upon pathway (约定的路径) for communications made up of an IP address and a port number, e.g.,. 10.1.1.1:25 The IP Layer provides the TCP layer with IP Address of the client and server. The TCP layer contains the details about the source port and the destination port. ※ "Socket" may refer to "Socket API" or "Socket Address" |
※ 端口的范围是1~65535 ※ 查看已经被使用的端口: cmd→netstat -ano |
Some of the more common port numbers are 21, 25, 53, 80 110, 443 21 = FTP: File Transfer Protocol 23 = Telnet: Telnet 25 = SMTP: Simple Mail Transfer Protocol 53 = DNS: Domain Name System ,Apache-tomcat的默认端口是808) 110 = POP3: Post Office Protocol version 3 119 = NNTP: Net News Transport Protocol 443 = SSL: Secure Sockets Layer |
|
●Socket连接与HTTP连接
由于通常情况下Socket连接就是TCP连接,因此Socket连接一旦建立,通信双方即可开始相互发送数据内容,直到双方连接断开。但在实际网络应用中,客户端到服务器之间的通信往往需要穿越多个中间节点,例如路由器、网关、防火墙等,大部分防火墙默认会关闭长时间处于非活跃状态的连接而导致 Socket 连接断连,因此需要通过轮询告诉网络,该连接处于活跃状态。 而HTTP连接使用的是"请求—响应"的方式,不仅在请求时需要先建立连接,而且需要客户端向服务器发出请求后(TCP连接不需要客户端向服务器发出请求),服务器端才能回复数据。 使用HTTP连接而不用Socket连接的场景: 很多情况下,需要服务器端主动向客户端推送数据,保持客户端与服务器数据的实时与同步。此时若双方建立的是Socket连接,服务器就可以直接将数据传送给客户端;若双方建立的是HTTP连接,则服务器需要等到客户端发送一次请求后才能将数据传回给客户端,因此,客户端定时向服务器端发送连接请求,不仅可以保持在线,同时也是在"询问"服务器是否有新的数据,如果有就将数据传给客户端。 |
,所以一般计算机上不会限制这个端口,所以Http协议能够顺利通过所有机器上的防火墙。而使用Socket编程的话,就需要自己指定特定的端口,那么很可能这个端口是在某个环境中禁用的,那么就无法穿透防火墙。 |
●TCP 3-Way Handshake (SYN,SYN-ACK,ACK)-可靠的协议 vs UDP-不可靠的协议
TCP uses a three-way handshake to establish a connection. |
第一次握手: 客户端向服务器发出连接请求, 等待服务器确认; First handshake: The client sends a connection request to the server, waiting for the server's conformation. 第二次握手: 服务器向客户端回送一个响应, 通知客户端收到了连接请求; Second handshake: The server sends back a response to the client, informing the client of having received the connection request; 第三次握手: 客户端再次向服务器端发送确认信息, 确认连接. Third handshake: The client sends again a confirmation message, and the connection is established. |
Host A sends a TCP SYNchronize packet to Host B Host B receives A's SYN Host B sends a SYNchronize-ACKnowledgement Host A receives B's SYN-ACK Host A sends ACKnowledge Host B receives ACK. TCP socket connection is ESTABLISHED. |
(Java学习笔记) Java Networking (Java 网络)的更多相关文章
- Java学习笔记心得——初识Java
初识Java 拿到这本厚厚的<Java学习笔记>,翻开目录:Java平台概论.从JDK到TDE.认识对象.封装.继承与多态...看着这些似懂非懂的术语名词,心里怀着些好奇与担忧,就这样我开 ...
- Java学习笔记之:Java简介
一.引言 Java是由Sun Microsystems公司于1995年5月推出的Java面向对象程序设计语言和Java平台的总称.由James Gosling和同事们共同研发,并在1995年正式推出. ...
- Java学习笔记之:Java String类
一.引言 字符串广泛应用在Java编程中,在Java中字符串属于对象,Java提供了String类来创建和操作字符串. 创建字符串最简单的方式如下: String str= "Hello w ...
- Java学习笔记之:Java引用数据类型之字符串
一.简介 字符串广泛应用在Java编程中,在Java中字符串属于对象,Java提供了String类来创建和操作字符串. 创建字符串最简单的方式如下: String greeting = "H ...
- Java 学习笔记 ------第一章 Java平台概论
本章学习目标: Java版本迁移简介 认识Java SE.Java EE.Java ME 认识JDK规范与操作 了解JVM.JRE与JDK 下载与安装JDK 一.Java版本迁移简介 书上已经表达得非 ...
- 【Java学习笔记之一】java关键字及作用
Java关键字及其作用 一. 总览: 访问控制 private protected public 类,方法和变量修饰符 abstract class extends final implements ...
- Java学习笔记三十一:Java 包(package)
Java 包(package) 一:包的作用: 如果我们在使用eclipse等工具创建Java工程的时候,经常会创建包,那么,这个包是什么呢. 为了更好地组织类,Java 提供了包机制,用于区别类名的 ...
- 【Java学习笔记之一】 java关键字及作用
Java关键字及其作用 一. 总览: 访问控制 private protected public 类,方法和变量修饰符 abstract class extends final implements ...
- java学习笔记(9)——网络
计算机网络: 最简单的网络由两台计算机组成 计算机A ---协议---> 网络 ---协议---> 计算机B---->端口1---->A软件 192.168.0.118 ...
- Java学习笔记之:Java 流
一.介绍 Java.io包几乎包含了所有操作输入.输出需要的类.所有这些流类代表了输入源和输出目标. Java.io包中的流支持很多种格式,比如:基本类型.对象.本地化字符集等等. 一个流可以理解为一 ...
随机推荐
- 雷林鹏分享: C# 教程
C# 教程 C# 是一个简单的.现代的.通用的.面向对象的编程语言,它是由微软(Microsoft)开发的. 本教程将告诉您基础的 C# 编程,同时将向您讲解 C# 编程语言相关的各种先进理念. 现在 ...
- vue.js手机号验证是否正确
{literal} var mobile_mode=/^1[34578]\d{9}$/; {/literal} if(!mobile_mode.test(te ...
- luffy之多条件登录与极验滑动验证码
多条件登录 JWT扩展的登录视图,在收到用户名与密码时,也是调用Django的认证系统中提供的authenticate()来检查用户名与密码是否正确. 我们可以通过修改Django认证系统的认证后端( ...
- Vue自动化工具(Vue-CLI)的安装
安装VUM 前面学习了普通组件以后,接下来我们继续学习单文件组件则需要提前先安装准备一些组件开发工具.否则无法使用和学习单文件组件. 一般情况下,单文件组件,我们运行在 自动化工具vue-CLI中,可 ...
- Django模型类之models字段类型和参数以及元数据meta
models之字段类型和参数 示例: # class Test(models.Model): # courses_test # """测试学习用""& ...
- 卸载WPS后怎么WORD的图标还是WPS
在电脑中选择使用Microsoft Office并将之前安装的WPS Office办公软件卸载了.但是卸载之后发现电脑系统中的Word.Excel等文件无法正常显示图标.在这样的情况下,我们应该如何解 ...
- 『TensorFlow』分布式训练_其二_单机多GPU并行&GPU模式设定
建议比对『MXNet』第七弹_多GPU并行程序设计 一.tensorflow GPU设置 GPU指定占用 gpu_options = tf.GPUOptions(per_process_gpu_mem ...
- k8s问题收集
问题 解决 failed: rpc error: code = Unknown desc = NetworkPlugin cni failed to set up pod "*****&qu ...
- python-flask-script定制manage命令
安装: pip3 install flask-script #!/usr/bin/env python # -*- coding:utf-8 -*- from flask_script import ...
- Pycharm中安装package出现microsoft visual c++ 14.0 is required问题解决办法
在利用pycharm安装scrapy包是遇到了挺多的问题.在折腾了差不多折腾了两个小时之后总算是安装好了.期间各种谷歌和百度,发现所有的教程都是利用命令行窗口安装的.发现安装scrapy需要的包真是多 ...