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