It is very convenient and powerful to develop network software with Java. This power of Java comes from its unique set of powerful APIs for network. These APIs are a series of classes and interfaces, all located in the packages java.net and javax .net. In this article, we will introduce the concept of socket (Socket), and use examples to illustrate how to use Network API to manipulate sockets. After completing this article, you can write low-end network communication software.
What is a socket?
Network API is typically used for Java programs to communicate with other programs based on TCP/IP networks. Network API relies on Socket for communication. Socket can be regarded as an endpoint in the communication connection between two programs. One program writes a piece of information into the Socket, and the Socket sends the information to another Socket so that the information can be transmitted to other programs. As shown in Figure 1
Let’s analyze Figure 1. Program A on Host A writes a piece of information into the Socket. The content of the Socket is accessed by Host A’s network management software, and the piece of information is sent through Host A’s network interface card. After receiving this information, Host B's network interface card sends it to Host B's network management software. The network management software saves this information in Host B's Socket, and then Program B can read this information in the Socket. segment information.
Assume that a third host, Host C, is added to the network in Figure 1. How does Host A know that the information is correctly transmitted to Host B instead of to Host C? Each host in a TCP/IP-based network is assigned a unique IP address. The IP address is a 32-bit unsigned integer. Since it is not converted into binary, it is usually separated by decimal points, such as: 198.163.227.6, as All IP addresses seen are composed of four parts, each part ranges from 0-255 to represent an 8-bit address.
It is worth noting that IP addresses are all 32-bit addresses, which is stipulated by IP protocol version 4 (referred to as Ipv4). Currently, since IPv4 addresses are almost exhausted, IPv6 addresses are gradually replacing Ipv4 addresses, and Ipv6 addresses are 128-bit. Unsigned integer.
Assuming that the second program is added to Host B of the network in Figure 1, how can the information transmitted from Host A be correctly passed to Program B instead of to the newly added program? This is because every program based on TCP/IP network communication is assigned a unique port and port number. The port is an information buffer used to retain the input/output information in the Socket. The port number is a 16-bit unsigned Integer, ranging from 0-65535, to distinguish each program on the host (the port number is like the room number in a house). Short port numbers below 256 are reserved for standard applications. For example, the port number of pop3 is 110. Each Sockets are combined into IP addresses, ports, and port numbers, so that the whole formed can distinguish each socket t. Let's talk about two types of sockets: stream sockets and self-addressed data. socket.
Stream Socket
Whenever sending and receiving information between two network applications, a reliable connection needs to be established. Stream Socket relies on the TCP protocol to ensure that the information reaches the destination correctly. In fact, the IP packet may be lost in the network or errors may occur during transmission. In either case, the TCP as the recipient will contact the sender TCP to resend the IP packet. This is called establishing a reliable connection between two stream sockets.
The above is the content of Java Socket Programming (Part 1) (1). For more related content, please pay attention to the PHP Chinese website (m.sbmmt.com)!