Using Java to implement a data report communication process

zhaozj2021-02-11  198

Data Reports A logical packet format for transmitting information on the media, which is propagated, independent, independent, and contains information of address information, which can reach the destination, arrive at the destination. Time, whether the content will change when it arrives, can not be accurately known. Its communication between the communication is not need to establish a connection. For some applications that do not require high quality, Datashers Newsletter is a very good choice. There are two types of DataGramsocket and DataGramSocket and DataGrampacket in Java's Java.net package to communicate network communication for applications.

Below, I want to explain in detail in Java to implement the data newsletter of the client and the server, please see:

One,

Workflow for client applications

1) First, you have to create a socket, we can implement it by creating a DataGramsocket object, and there are two configuration methods in the DataGramsocket class in Java:

a) Public DataGramsocket () Constructs a datagram socket and connects to any available ports available to the local host. If you can't open the socket, you will throw the socketException exception.

b) Public DataGramsocket (int port) constructs a datagram and connects to the port specified by the local host. If you can't open the socket or socket Unable to connect to the specified port connection to throw the SocketException exception.

2) Create a data packet to implement unconnected package delivery services. Each data packet is created with a DataGrampacket class, and the DataGrampacket object encapsulates the data package data, the package length, the target address, the target port. As a client to send a data packet, you want to call the DataGrampacket class to create a DatagramPacket object with the following configuration function, and put the data to be sent into the object.

DataGrampacket (Byte BufferedAtAddress Address, INT page), is a packet transmitted to a packet length to the packet to specify the port number of the specified port number, and the parameter length must be less than or equal to BUFFEREDARRY.LENGTH.

The DataGrampacket class provides four classes to get information

a) Public Byte [] getData () Returns a byte array, contains data in the datagret to send.

b) Public int getLength () returns the length of the transmitted or received data

c) Public inetaddress getaddress () Returns an IP address of the machine that sends or receive this data.

d) PUBLIC INT getPort () Returns the port number of the remote host that sends or receives the datagram.

3) Create a DataGramsocket and a DataGrampacket object, you can send a data packet package. The transmission is implemented by calling the SEND method of the DataGramsocket object, which requires a DataGrampacket object as a parameter, and the data constitutes data reported to the data in the DataGrampacket object.

4) Of course, we can also receive data packets, in order to receive the result data packets returned from the server, we need to create a new DataGrampacket object, which requires another constructor of DataGrampacket DataGrampacket (Byte BufferedArray) [], int LENGTH, ie simply refers to the buffers and lengths of the received datagram. Call the Receive () method of the DataGramsocket object to complete the work of the received datagram. At this time, the DataGraMpacket object to be created is required as a parameter. This method will blindly know that you receive a data packet, at which time the DataGrampacket buffer contains The received data is received, and the data packet also includes information such as the sender's IP address, port number on the sender machine. 5) Handle data within the receiving buffer and obtain the results of the service.

6) When the communication is completed, you can use the Close () method of the DataGramsocket object to close the Denom Communication Socket. Of course, Java will automatically close the Socket, release the resources occupied by DataGramsocket and DataGrampacket. But as a good program habit, it is also to be displayed.

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

New Post(0)