(Java学习笔记) Java Networking (Java 网络)

Wesley13
• 阅读 439

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对比

(Java学习笔记) Java Networking (Java 网络)

(Java学习笔记) Java Networking (Java 网络)

(Java学习笔记) Java Networking (Java 网络)

(Java学习笔记) Java Networking (Java 网络)

(Java学习笔记) Java Networking (Java 网络)

(Java学习笔记) Java Networking (Java 网络)

链路层:链路层用于定义物理传输通道,包含对某些网络连接设备的驱动协议,例如针对光纤、双绞线提供的驱动。

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.

(Java学习笔记) Java Networking (Java 网络) (Java学习笔记) Java Networking (Java 网络)

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

80 = HTTP: Hyper Text Transfer Protocol(不管是在Linux下还是Windows下,Apache的默认端口都是80,Apache-tomcat的默认端口是808)

110 = POP3: Post Office Protocol version 3

119 = NNTP: Net News Transport Protocol

443 = SSL: Secure Sockets Layer

(Java学习笔记) Java Networking (Java 网络)

●Socket连接与HTTP连接

由于通常情况下Socket连接就是TCP连接,因此Socket连接一旦建立,通信双方即可开始相互发送数据内容,直到双方连接断开。但在实际网络应用中,客户端到服务器之间的通信往往需要穿越多个中间节点,例如路由器、网关、防火墙等,大部分防火墙默认会关闭长时间处于非活跃状态的连接而导致 Socket 连接断连,因此需要通过轮询告诉网络,该连接处于活跃状态。

而HTTP连接使用的是"请求—响应"的方式,不仅在请求时需要先建立连接,而且需要客户端向服务器发出请求后(TCP连接不需要客户端向服务器发出请求**)**,服务器端才能回复数据。

使用HTTP连接而不用Socket连接的场景: 很多情况下,需要服务器端主动向客户端推送数据,保持客户端与服务器数据的实时与同步。此时若双方建立的是Socket连接,服务器就可以直接将数据传送给客户端;若双方建立的是HTTP连接,则服务器需要等到客户端发送一次请求后才能将数据传回给客户端,因此,客户端定时向服务器端发送连接请求,不仅可以保持在线,同时也是在"询问"服务器是否有新的数据,如果有就将数据传给客户端。

Http协议一定通过指定的端口,80,所以一般计算机上不会限制这个端口,所以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.

点赞
收藏
评论区
推荐文章
blmius blmius
2年前
MySQL:[Err] 1292 - Incorrect datetime value: ‘0000-00-00 00:00:00‘ for column ‘CREATE_TIME‘ at row 1
文章目录问题用navicat导入数据时,报错:原因这是因为当前的MySQL不支持datetime为0的情况。解决修改sql\mode:sql\mode:SQLMode定义了MySQL应支持的SQL语法、数据校验等,这样可以更容易地在不同的环境中使用MySQL。全局s
Jacquelyn38 Jacquelyn38
2年前
2020年前端实用代码段,为你的工作保驾护航
有空的时候,自己总结了几个代码段,在开发中也经常使用,谢谢。1、使用解构获取json数据let jsonData  id: 1,status: "OK",data: 'a', 'b';let  id, status, data: number   jsonData;console.log(id, status, number )
皕杰报表之UUID
​在我们用皕杰报表工具设计填报报表时,如何在新增行里自动增加id呢?能新增整数排序id吗?目前可以在新增行里自动增加id,但只能用uuid函数增加UUID编码,不能新增整数排序id。uuid函数说明:获取一个UUID,可以在填报表中用来创建数据ID语法:uuid()或uuid(sep)参数说明:sep布尔值,生成的uuid中是否包含分隔符'',缺省为
Wesley13 Wesley13
2年前
Java获得今日零时零分零秒的时间(Date型)
publicDatezeroTime()throwsParseException{    DatetimenewDate();    SimpleDateFormatsimpnewSimpleDateFormat("yyyyMMdd00:00:00");    SimpleDateFormatsimp2newS
Stella981 Stella981
2年前
KVM调整cpu和内存
一.修改kvm虚拟机的配置1、virsheditcentos7找到“memory”和“vcpu”标签,将<namecentos7</name<uuid2220a6d1a36a4fbb8523e078b3dfe795</uuid
Wesley13 Wesley13
2年前
mysql设置时区
mysql设置时区mysql\_query("SETtime\_zone'8:00'")ordie('时区设置失败,请联系管理员!');中国在东8区所以加8方法二:selectcount(user\_id)asdevice,CONVERT\_TZ(FROM\_UNIXTIME(reg\_time),'08:00','0
Wesley13 Wesley13
2年前
00:Java简单了解
浅谈Java之概述Java是SUN(StanfordUniversityNetwork),斯坦福大学网络公司)1995年推出的一门高级编程语言。Java是一种面向Internet的编程语言。随着Java技术在web方面的不断成熟,已经成为Web应用程序的首选开发语言。Java是简单易学,完全面向对象,安全可靠,与平台无关的编程语言。
Stella981 Stella981
2年前
Django中Admin中的一些参数配置
设置在列表中显示的字段,id为django模型默认的主键list_display('id','name','sex','profession','email','qq','phone','status','create_time')设置在列表可编辑字段list_editable
Stella981 Stella981
2年前
Docker 部署SpringBoot项目不香吗?
  公众号改版后文章乱序推荐,希望你可以点击上方“Java进阶架构师”,点击右上角,将我们设为★“星标”!这样才不会错过每日进阶架构文章呀。  !(http://dingyue.ws.126.net/2020/0920/b00fbfc7j00qgy5xy002kd200qo00hsg00it00cj.jpg)  2
Wesley13 Wesley13
2年前
MySQL部分从库上面因为大量的临时表tmp_table造成慢查询
背景描述Time:20190124T00:08:14.70572408:00User@Host:@Id:Schema:sentrymetaLast_errno:0Killed:0Query_time:0.315758Lock_
Python进阶者 Python进阶者
2个月前
Excel中这日期老是出来00:00:00,怎么用Pandas把这个去除
大家好,我是皮皮。一、前言前几天在Python白银交流群【上海新年人】问了一个Pandas数据筛选的问题。问题如下:这日期老是出来00:00:00,怎么把这个去除。二、实现过程后来【论草莓如何成为冻干莓】给了一个思路和代码如下:pd.toexcel之前把这