Started his socket (java)

Started his socket (java)

First, the network protocol

To talk about the socket must first understand the network protocol
called Internet protocol for data exchange is a computer network established rules, standards or conventions and the socket is just a certain kind of package (s) protocol implementation. Its essence is not considered agreement

1, OSI seven layer protocol

Here Insert Picture Description

Physical Layer

In the OSI reference model, the physical layer (Physical Layer) is the lowest layer reference model, is the first layer OSI model.
The main function of the physical layer are: data link layer provides the physical connection, transparent transmission of the bitstream using a transmission medium.
Role of the physical layer to achieve transparent transfer between the computer nodes adjacent bit stream, the specific transmission medium masked differences and physical devices as possible. The above data link layer so that the specific transmission medium regardless of what network Yes. "Transparent transmission bit stream" indicates the bit stream transmitted by the actual circuit has not changed, the bit stream is transmitted, if this circuit is invisible.

data link layer

The data link layer (Data Link Layer) is the second layer of the OSI model, is responsible for establishing the link between the management node. The layer main functions are: controlling a variety of protocols, there will be an error in the physical channel becomes free of error, the data link can be reliably transmitting a data frame.
In a computer network due to the presence of various disturbances, physical link is unreliable. Thus, the main function of this layer bitstream is based on the physical layer, through error control, flow control method, so that an error becomes a physical line error-free data link, i.e., to provide a reliable physical medium the method of transmitting data.

This layer is typically further divided into a medium access control (MAC) and Logical Link Control (LLC) sub-layer two.
The main task of the MAC sublayer is to solve the multi-user network-shared channel contention, the medium access control to complete the network;
main tasks of the LLC sublayer to establish and maintain a network connection, performs the error check, flow control and link control.

DETAILED working data link layer is the data bit stream received from the physical layer, and encapsulated in a frame transmitted to the floor; Likewise, data from an upper layer frame, disassembly of the bit stream data is forwarded to physical layer; and, is also responsible for processing information acknowledgment frames sent back to the receiving end, in order to provide reliable data transmission.

Network layer

Network layer (Network Layer) is the third layer of the OSI model, which is the most complex of the OSI reference model layer is the highest layer of subnetworks. It is on the basis of the lower layers of resources to provide services to the subnet. Its main tasks are: by routing algorithms to select the most appropriate path for the packets or packet through subnetworks. The layer control information transfer between the data link layer and the transport layer to establish, maintain and terminate network connections. Specifically, the data link layer in this layer is converted into data packets, then the path selection, the segment combination, order, into / out routing control and the like, the information is transmitted from one network device to another network device .
In general, the data link layer is to solve the communication between the nodes in the same network, and the network layer is mainly to solve the communication between different subnetworks. For example, when the communication between the wide area network, will inevitably encounter route (i.e. there may be multiple paths between two nodes) selection.

When implementing the network layer function, the main problem to be solved as follows:
1. Addressing: physical address used in the data link layer (e.g. MAC address) only to solve the problem addressed within the network. When the communication between different subnets, to identify and locate devices on the network, each subnet is assigned a unique device address. Since each physical subnet technology may be different, so that it should address is a logical address (e.g., IP address).
2. exchange: provision of different information exchange. Common switching technology are: circuit switching technology and store and forward technology, which in turn includes a packet switching technology and packet switching technology.
Routing algorithm: When there are multiple paths between the source node and the destination node, the routing algorithm according to the present layer, data packets through the network to choose the best route, and transfers the information from the most appropriate path from the transmitting end to the receiving end.
3. Services connections: the data link layer flow control is different, the former is the flow control between adjacent nodes in the network, which is controlled from the source node to the destination node traffic between. Its object is to prevent clogging, and performs error detection.

Transport Layer

OSI layer 3 under the main task of the data communication task layer 3 data processing. And the transport layer (Transport Layer) is the fourth layer of the OSI model. So that the layer is a subnet and communication resources and interfaces subnet bridge acts as a connecting link.
The main task of this layer is to: provide a reliable end-user errors and flow control to ensure the correct transmission of packets. Role of the transport layer is to shield the details of the underlying data communication to high-level, ie transparently transmit user packets. The common layer protocols: TCP protocol TCP / IP is, Novell SPX network protocol and Microsoft's NetBIOS / NetBEUI protocol.
The transport layer provides transport services between the session layer and network layer, the data obtained from this service session layer, and if necessary, the data is divided. Then, the transport layer to the network layer passes data, and that data can be correctly transmitted to the network layer. Therefore, the transport layer is responsible for providing reliable delivery of data between two nodes, when ties between the two nodes to determine, the transport layer is responsible for supervision. In summary, the main functions of the transport layer as follows:
1. The transport connection management: establishment, maintenance and removal of the transport connection functions. The transport layer provides "connection-oriented" and "non-oriented series of" high-level services for the two on the basis of the network layer.
2. Processing transmission errors: providing reliable "connection-oriented" and less reliable "connectionless" service data transmission, error control and flow control. In providing "connection-oriented" services, by the target device through this layer acknowledgment data transmission, if confirmation is not received within the specified time, the data is retransmitted. Monitor the quality of service.

Session Layer

The session layer (Session Layer) is the fifth layer of the OSI model is the interface between the user and the network application, the main tasks are: to provide a method to establish a connection and use two presentation layer entities. The connection between presentation layer entities referred to different sessions. Therefore communication and data exchange between tasks is to organize and coordinate the two sessions with the session layer of management.
Users can establish a session according to half-duplex, full-duplex and simplex mode. When the session is established, the user must provide remote address they want to connect. These different addresses and logical addresses MAC (Media Access Control sublayer) address or network layer, which are designed for the user, the user easier to remember. This domain name (DN) that is used on the kind of network remote address such as: www.baidu.com is a domain name. Session layer specific functions as follows:
Session Management: Allows users to establish between two physical devices, maintain and terminate sessions, and supports data exchange between them. For example, provide unidirectional or bidirectional session simultaneous sessions, and transmission order management session, and the length of time occupied by the session.
Session Flow Control: Provides session traffic control and cross-session capabilities.
Addressing: Using the remote address to establish a session connection. l
Error control: establishing a data exchange session layer logically mainly responsible for maintaining and termination, but the actual work is to receive data from the transport layer, and is responsible for correcting the error. Session control and remote procedure calls belong to this level of functionality. It should be noted that this layer error checking error is not a communication medium, but the disk space, printer is out of paper types of advanced error.

Presentation layer

Presentation Layer (Presentation Layer) is the sixth layer of the OSI model, it commands and data from the application layer interprets the various grammatical meaning given appropriate, and transmitted to the session layer according to a certain format. Its main function is to "Problem showing user information, such as encoding, encryption and decryption, and data format conversion" and the like. Specific functional layer is represented as follows:
1. Data Format Processing: negotiated and established format of the data exchange between applications to resolve the differences in data representation format.
2. The coded data of: processing the character set and the digital conversion. For example, since the data type in the user program (integer or real, signed or unsigned, etc.), user identity and so can have different representations, therefore, required between devices having different character sets or formats conversion function.
Compression and decompression: In order to reduce the amount of data transmission, this layer is also responsible for data compression and recovery.
3. Data encryption and decryption: You can improve the security of the network.

Application layer

The application layer (Application Layer) is the highest level of the OSI reference model, which is the interface between the computer user, as well as various applications and networks, whose function is to provide services directly to the user, the user wishes to complete a variety of completion on the network jobs. It is based on six other layers on the work, is responsible for the completion of the link between the application and network operating system, network, build a variety of network services and applications required to end the link between the user and the completion of the proposed network users supervision, management and services, and other protocols. In addition, this layer is also responsible for coordination between the various applications.
Services and application layer protocol provides users are: file services, directory services, file transfer services (FTP), remote login service (Telnet), e-mail service (E-mail), print services, security services, network management services, database services. The above-described various network services performed by the different application layer protocols and procedures between different network operating system functions, interface, implementation techniques, hardware support, as well as various security and reliability application program interfaces each having the differences are significant. The main function of the application layer is as follows:
1. User Interface: the application layer and the network user, and a direct interface between applications and the network, enabling the user to contact the interactive network.
2. To achieve a variety of services: the layer having a variety of applications and can be done to achieve a variety of services requested by the user.

Specifically:
a physical layer: LAN, cable, hub, repeater, a modem
data link layer: a bridge, switch
network layer: router
gateway program in the fourth layer is the transport layer and above

2, TCP / IP protocol suite

Here Insert Picture Description
TCP / IP protocol concepts are organized into four layers, which correspond to respective three layers of the ISO reference model. TCP / IP protocol suite does not contain the data link layer and the physical layer, so it can not complete the entire computer network system independent functions must work with many other protocols

Layer four protocol TCP / IP layer model were completed the following features:

First layer: Network Interface Layer

包括用于协作IP数据在已有网络介质上传输的协议。实际上TCP/IP标准并不定义与ISO数据链路层和物理层相对应的功能。相反,它定义像地址解析协议(Address Resolution Protocol,ARP)这样的协议,提供TCP/IP协议的数据结构和实际物理硬件之间的接口。

第二层:网间层

对应于OSI七层参考模型的网络层。本层包含IP协议、RIP协议(Routing Information Protocol,路由信息协议),负责数据的包装、寻址和路由。同时还包含网间控制报文协议(Internet Control Message Protocol,ICMP)用来提供网络诊断信息。

第三层:传输层

对应于OSI七层参考模型的传输层,它提供两种端到端的通信服务。其中TCP协议(Transmission Control Protocol)提供可靠的数据流运输服务,UDP协议(Use Datagram Protocol)提供不可靠的用户数据报服务。

第四层:应用层

对应于OSI七层参考模型的应用层和表达层。因特网的应用层协议包括Finger、Whois、FTP(文件传输协议)、Gopher、HTTP(超文本传输协议)、Telent(远程终端协议)、SMTP(简单邮件传送协议)、IRC(因特网中继会话)、NNTP(网络新闻传输协议)等

二、socket

socket简介

Socket又称"套接字",应用程序通常通过"套接字"向网络发出请求或者应答网络请求。凭借这种通信机制,客户/服务器系统的开发工作既可以在本地单机上进行,也可以跨网络进行。
简单来说:Socket=Ip address+ TCP/UDP/… + port。(此处的socket只是一个统称,根据不同的协议 其实java 使用了不同的 socket对象,比如TCP使用的是 ServerSocket,Socket。UDP则使用DatagramSocket)
Here Insert Picture Description

socket连接过程

1、服务器监听:是服务器端套接字并不定位具体的客户端套接字,而是处于等待连接的状态,实时监控网络状态。
2、客户端请求:是指由客户端的套接字提出连接请求,要连接的目标是服务器端的套接字。为此,客户端的套接字必须首先描述它要连接的服务器的套接字,指出服务器端套接字的地址和端口号,然后就向服务器端套接字提出连接请求。
3、连接确认:是指当服务器端套接字监听到或者说接收到客户端套接字的连接请求,它就响应客户端套接字的请求,建立一个新的线程,把服务器端套接字的描述发给客户端,一旦客户端确认了此描述,连接就建立好了。而服务器端套接字继续处于监听状态,继续接收其他客户端套接字的连接请求

socket编程

TCP 的socket

为了实现服务器与客户机的通信,服务器和客户机都必须建立套接字。服务器与客户机的工作原理可以用下面的过程来描述。
1.服务器先用 socket 函数来建立一个套接字,用这个套接字完成通信的监听。
2.用 bind 函数来绑定一个端口号和 IP 地址。因为本地计算机可能有多个网址和 IP,每一个 IP 和端口有多个端口。需要指定一个 IP 和端口进行监听。
3.服务器调用 listen 函数,使服务器的这个端口和 IP 处于监听状态,等待客户机的连接。
4.客户机用 socket 函数建立一个套接字,设定远程 IP 和端口。
5.客户机调用 connect 函数连接远程计算机指定的端口。
6.服务器用 accept 函数来接受远程计算机的连接,建立起与客户机之间的通信。
7.建立连接以后,客户机用 write 函数向 socket 中写入数据。也可以用 read 函数读取服务器发送来的数据。
8.服务器用 read 函数读取客户机发送来的数据,也可以用 write 函数来发送数据。
9.完成通信以后,用 close 函数关闭 socket 连接。

Here Insert Picture Description
基于TCP 的socket连接、断开 就是常说的3次握手、4次挥手。
https://download.csdn.net/download/soulonlyhlh/12129469
此处是本人写的一个小例子,仅供参考

UDP 的socket

无连接的通信不需要建立起客户机与服务器之间的连接,因此在程序中没有建立连接的过程。进行通信之前,需要建立网络套接字。服务器需要绑定一个端口,在这个端口上监听接收到的信息。客户机需要设置远程 IP 和端口,需要传递的信息需要发送到这个 IP 和端口上。
Here Insert Picture Description

SCTP的Socket

作为与TCP 同级的协议,也可进行通信。本人不曾使用,个人觉得与TCP 类似
代码例子可参照
https://www.oracle.com/technical-resources/articles/javase/SCTP.html

总结:协议就像齿轮,每个厂商需要协商齿轮的规格才能让不同厂商生产的齿轮也能相互咬颌,socket则像是发动机保证了里面的各个齿轮可以正常使用,并将它们包装起来,方便其他人的使用,而其他的框架,程序则是引擎以及汽车。

题外话,翻阅资料时 额外看到
四种网络编程方式:ISAPI、CGI、WinInet、Winsock
https://blog.csdn.net/winder9898/article/details/51996135
采用socket以外的方法实现网络通信
1.JavaRMI
2.分布对象系统(CORBA):
3.MPI
https://blog.csdn.net/u011365209/article/details/11694539
个人以为 Java RMI最后底层应该也是使用了Socket,但没去翻阅源码若有人知晓烦请告知下


以上浅见若有不正确的地方,烦请各位大神指正

Published 21 original articles · won praise 6 · views 30000 +

Guess you like

Origin blog.csdn.net/soulonlyhlh/article/details/104129699