Java network programming helps solve practical problems,

zhaozj2021-02-16  48

Java

Developing network software is very convenient and powerful,

Java

This power is derived from his unique set of powerful for the network.

API

,These ones

API

Is a series of classes and interfaces

Java.net

with

Javax.net

in. In this article we will introduce the socket

(Socket)

Single, while explaining how to use an example

Network API

After the tag, you can write the network low-end communication software after completing this article.

What is a socket

(Socket)

?

Network API

Is typical for

TCP / IP

The internet

Java

The program communicates with other programs,

Network API

rely

Socket

Communication.

Socket

It can be seen as one endpoint in the communication connection in two programs, and a program writes a piece of information.

Socket

Medium, this

Socket

Send this information to another

Socket

In, this information can be transmitted to other programs. Figure

1

Let's analyze the programs on Host A. Procedure A to write a section into socket, Socket is accessed by Host A network management software, and sends this information through Host A network interface card to Host B, After the Host B network interface card is received, the network management software transmitted to the Host B is transmitted, and the network management software saves this information in the Host B socket, and then the program B can read this information in the socket. Suppose the third host Host C is added to the network of Figure 1, then how does Host A know that the information is transmitted correctly to Host B instead of being transferred to Host C? Each host based on the TCP / IP network is given a unique IP address. The IP address is a 32-bit unsigned integer. Since it is not converted to binary, it is usually separated by a decimal point, such as: 198.163.227.6, just as The IP addresses are all consisting of four parts, each of which is 0-255 to represent an 8-bit address. It is worth noting that the IP address is 32-bit address. This is the IP Protocol version 4 (IPv4) specified. Currently since IPv4 addresses are nearly exhausted, IPv6 addresses are gradually replacing IPv4 addresses. The IPv6 address is 128 Symbol integer. Suppose the second program is added to Host B of the network of Figure 1, then how can the information from Host A can be correctly transmitted to program b instead of passing to newly joined programs? This is because each TCP / IP network communication program is given a unique port and port number. The port is an information buffer for retaining the input / output information in the socket. The port number is a 16-bit unsigned symbol. Integer, the range is 0-65535 to distinguish every program on the host (port number is like a room number in the house), the short slogan below 256 is reserved to the standard application, such as the port number of POP3 is 110, each The sockets are combined into the IP address, port, port number, which can distinguish every socket T, let's talk about two sockets: Flow socket and self-address data Socket. Stream sockets need to establish a reliable connection between when sending and receiving information between the two network applications, the flow cover is relying on the TCP protocol to ensure that the information is properly reached, actually The IP package is likely to lose in the network or an error occurs during the transfer process. Any case occurs, and the TCP of the acceptor will contact the sender TCP to resend this IP package. This is the so-called establishing a reliable connection between two flow sockets. Flow socket plays a necessary role in the C / S program, the client program (the network application that needs to access some services) creates an IP address and server program for hosting the server's host (provided for client applications) The port number of the port number of the service network application. The initialization code of the client-end socket passes the IP address and port number to the network management software of the client host, and the management software passes the IP address and port number to the server host by NIC; the server-side host reads the NIC transmission Data, then check if the server program is in the listening state, which is still performed by a socket and port; if the server program is listening, the server-side network management software issues a positive After receiving the response signal, the client flow sleeve initialization code creates a port number to the client program, and passes the port number to the server program (the server program will use this port number to identify Whether the information belongs to a client program) At the same time, the initialization of the stream sleeve is completed.

转载请注明原文地址:https://www.9cbs.com/read-22605.html

New Post(0)