Socket series What is socket and socket Series

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.