Socket series What is socket and socket Series
1. What is socket?
Socket is the intermediate abstraction layer for communications between the application layer and the TCP/IP protocol family. It is a group of interfaces that the application layer can send and receive data by calling these interfaces. Generally, this abstraction layer is provided by the operating system or implemented by the JVM itself. The socket can be used to implement application communication over the network. The application on one machine wants to write information to the socket, and the other connected machine can read the information. There are two socket types in the TCP/IP protocol family: stream socket and datagram socket, which correspond to TCP and UDP respectively. A TCP/IP socket is uniquely identified by an Internet address, protocol, and port number.
2-3-1: It is necessary and useful to add the socket abstraction layer between the transport layer and the application layer. It is similar to the facade mode in the design mode, the user does not need to know and process the details of the complex TCP/IP protocol family business logic. At this time, the socket shows its advantages, and it hides these complex processes under the socket interface, it helps the user to parse the packets of the TCP/IP protocol family to comply with the TCP/IP protocol family, so that the user can communicate with the data simply by calling the interface.
Figure 2-3-1 socket Abstraction Layer
2. socket in Java
What is socket, so what is socket in Java? Java provides two classes for the TCP protocol: Socket and ServerSocket. One represents the client and the other represents the server. You can implement TCP communication by operating these two classes. For UDP, Java provides the DatagramSocket class for UDP communication.
3. Since the address needs to communicate, it must involve WHO to communicate with. Only by determining what your target address is can the information be accurately transmitted to the correct machine. Generally, you can determine a target address by using an IP string or host name (for example, www.baidu.com. We can learn from the previous IP protocol that the IP address is actually a network address, not a machine address, so it is accurate to say that a host is connected through the IP protocol, it is actually the connection between a host and the network determined by the IP address.
In Java, an InetAddress class is provided to represent a network destination address, which includes the IP attribute and host name attribute. There are two versions of the IP address, it uses 32-bit IPv4 and 128-bit IPv6 respectively. Based on different IP protocol versions, two subclasses, Inet4Address and Inet6Address, are derived. InetAddress performs domain name resolution using local machine configuration or network Naming Service (such as Domain Name System DNS). By default, InetAddress caches ing between domain names and IP addresses for a limited period of time, in this way, you do not need to repeatedly send DNS requests to access the same address, which greatly improves the efficiency. Once an InetAddress instance is created, it cannot be changed and always points to an address. This class will be used in the ServerSocket, Socket, initramsocket and other classes to be introduced in the following sections.