In the use of the UDP protocol, we usually use other language tools to get the job done. So today we mainly introduce the use of UDP protocol under Java. First let's take a look at the basic concepts of the UDP protocol. The full name of the UDP protocol is the user datagram. In the network, it is used to process data packets in the same way as the TCP protocol. In the OSI model, in the fourth layer? ? The transport layer is on the upper layer of the IP protocol. UDP has the disadvantage of not providing data packet grouping, assembly, and sorting of data packets. That is, after the message is sent, it is impossible to know whether it is safely and completely arrived. .

Why use UDP?

When choosing to use the protocol, you must be cautious when choosing UDP. In the environment where the network quality is not very satisfactory, the UDP protocol packet loss will be more serious. However, due to the characteristics of UDP: it is not a connection protocol, so it has resources. The advantages of low consumption and fast processing speed, so audio, video and normal data usually use UDP more when transmitting, because even if they occasionally lose one or two data packets, they will not have much impact on the receiving result. For example, we chat The ICQ and OICQ used are the UDP protocols used.

First, use DatagramSocket to send and receive data principle

Java uses DatagramSocket to represent the Socket of UDP protocol. DatagramSocket itself is just a dock. It does not maintain state and cannot generate IO stream. Its sole function is to receive and send datagrams. Java uses DatagramPacket to represent datagrams. DatagramSocket receives and sends data. Completed by the DatagramPacket object.

1. DatagramSocket constructor

DatagramSocket(): Creates a DatagramSocket instance and binds it to the local default IP address, a randomly selected port of all available ports on the machine.

DatagramSocket(int prot): Create a DatagramSocket instance and bind the object to the local default IP address and the specified port.

DatagramSocket(int port, InetAddress laddr): Create a DatagramSocket instance and bind the object to the specified IP address and the specified port.

A DatagramSocket instance can be created by any of the three constructors above, usually creating a DatagramSocket instance of the specified port when the server is created -- this ensures that other clients can send data to the server. Once you have the DatagramSocket instance, you can receive and send data in two ways.

Receive(DatagramPacket p): Receives a datagram from the DatagramSocket.

Send(DatagramPacket p): Sends a datagram out of the DatagramSocket object.

As can be seen from the above two methods, when using DatagramSocket to send datagrams, DatagramSocket does not know where to send the datagram, but the DatagramPacket itself determines the destination of the datagram. Just as the terminal does not know the destination of each container, the terminal simply sends the containers out, and the container itself contains the destination of the container.

2. DatagramPacket constructor

DatagramPacket(byte[] buf, int length): Creates a DatagramPacket object with an empty array whose purpose is to receive data from the DatagramSocket.

DatagramPacket(byte[] buf, int length, InetAddress addr, int port): Creates a DatagramPacket object with an array containing data. When creating the DatagramPacket object, it also specifies the IP address and port -- this determines the datagram. destination.

DatagramPacket(byte[] buf, int offset, int length): Creates a DatagramPacket object with an empty array, and specifies that the received data is placed in the buf array from offset, up to length bytes.

DatagramPacket(byte[] buf, int offset, int length, InetAddress address, int port): Creates a DatagramPacket object for sending, specifying the length of bytes from the offset in the send buf array.

When the Client/Server program uses the UDP protocol, there is actually no obvious server or client, because both parties need to first create a DatagramSocket object to receive or send datagrams, and then use the DatagramPacket object as the carrier for transmitting data. . A program with a fixed IP address and a fixed port DatagramSocket object is called a server because the DatagramSocket can actively receive client data.

Before receiving the data, you should use the first or third constructor above to generate a DatagramPacket object, giving the byte array of the received data and its length. Then call the Receive() method of the DatagramSocket to wait for the datagram to arrive, and receive() will wait all the time (this method will block the thread that called the method) until it receives a datagram.

As shown in the following code:

// Create a DatagramPacket object that receives the data

DatagramPacket packet=new DatagramPacket(buf, 256);

// Receive datagrams

Socket.receive(packet);

Before sending the data, call the second or fourth constructor to create a DatagramPacket object. The byte array in this case stores the data you want to send. In addition to this, the full destination address, including the IP address and port number, is also given. The send data is implemented by the send() method of the DatagramSocket, which uses the destination address of the datagram to route the datagram. As shown in the following code:

// Create a DatagramPacket object that sends data

DatagramPacket packet = new DatagramPacket(buf, length, address, port);

// send datagram

Socket.send(packet);

When using the DatagramPacket to receive data, you will feel that the DatagramPacket is too cumbersome to design. The developer only cares about how much data the DatagramPacket can put, and whether the DatagramPacket uses a byte array to store the data does not want to care. But when Java asks to create a DatagramPacket for receiving data, it must pass in an empty byte array whose length determines how much data the DatagramPacket can put, which actually exposes the implementation details of the DatagramPacket. Then DatagramPacket provides a getData() method, which in turn returns the byte array encapsulated in the Datagram Packet object. This method is even more redundant - if the program needs to get the byte array encapsulated in the DatagramPacket, the direct access is passed to The byte array argument of the DatagramPacket constructor is fine, no need to call this method.

When the server (or the client) receives a DatagramPacket object, if it wants to "feed back" some information to the sender of the datagram, but since the UDP protocol is non-connected, the receiver does not know each. Who sent the datagram, but the program can call the following three methods of DatagramPacket to get the sender's IP address and port.

InetAddress getAddress(): When the program is ready to send this datagram, the method returns the IP address of the target machine of the datagram; when the program just receives a datagram, the method returns the IP address of the sending host of the datagram.

Int getPort(): When the program is ready to send this datagram, the method returns the port of the target machine of the datagram; when the program just receives a datagram, the method returns the port of the sending host of the datagram.

SocketAddress getSocketAddress(): When the program is ready to send this datagram, the method returns the destination SocketAddress of the datagram. When the program just receives a datagram, the method returns the SocketAddress of the sending host of the datagram. The return value of the getSocketAddress() method is a SocketAddress object, which is actually an IP address and a port number. That is, the SocketAddress object encapsulates an InetAddress object and an integer representing the port, so the SocketAddress object can represent both the IP address and the port.

Disposable Vape

The utility model provides a disposable electronic cigarette, comprising: a hollow shell, the bottom of the shell is provided with a lower cover; the shell contains an atomizer, and the outer side of the atomizer is sheathed with a disposable cigarette A bomb, a microphone cover is arranged under the atomizer, a microphone is covered under the microphone cover, a battery is arranged on one side of the atomizer, and an upper cover is arranged on the top of the casing; The atomizer includes an atomizing corThe utility model provides a disposable electronic cigarette, comprising: a hollow shell, the bottom of the shell is provided with a lower cover; the shell contains an atomizer, and the outer side of the atomizer is sheathed with a disposable cigarette A bomb, a microphone cover is arranged under the atomizer, a microphone is covered under the microphone cover, a battery is arranged on one side of the atomizer, and an upper cover is arranged on the top of the casing; The atomizer includes an atomizing core, an oil-absorbing cotton sleeved on the outside of the atomizing core, and an atomizer outer tube sleeved on the outside of the oil-absorbing cotton. The disposable electronic cigarette provided by the utility model absorbs the smoke oil on the surface through the absorbing cotton, and then atomizes the smoke through the atomizing core, which greatly reduces the risk of oil leakage, at the same time, reduces the burning of cotton and ensures the smoking taste.e, an oil-absorbing cotton sleeved on the outside of the atomizing core, and an atomizer outer tube sleeved on the outside of the oil-absorbing cotton. The disposable electronic cigarette provided by the utility model absorbs the smoke oil on the surface through the absorbing cotton, and then atomizes the smoke through the atomizing core, which greatly reduces the risk of oil leakage, at the same time, reduces the burning of cotton and ensures the smoking taste.

disposable vape,vape pen,vape accessories,vape store,vape battery,e-cigarettes,Disposable Pod

Suizhou simi intelligent technology development co., LTD , https://www.msmvape.com