Java UDP protocol code explanation (1)

Source: Internet
Author: User
Tags time in milliseconds

In the use of UDP protocol, we usually use other language tools to complete the work. So today we will mainly introduce the use of UDP protocol in Java. First, let's take a look at the basic concepts of UDP. The full name of UDP protocol is user datagram, which is used to process data packets in the same way as TCP protocol in the network. In the OSI model, in the layer-4 ?? Transport layer, which is on the top of the IP protocol. UDP has the disadvantages of not providing packet grouping, assembly, and not sorting data packets. That is to say, when a packet is sent, it is impossible to know whether it is safe and complete 。

Why UDP?

When choosing to use the Protocol, you must be cautious when choosing UDP. in an environment with unsatisfactory network quality, UDP packet loss may be serious. However, due to the characteristics of UDP: it is not a connection protocol, so it has the advantages of low resource consumption and high processing speed. Therefore, most of the audio, video, and common data are transmitted using UDP, because even if one or two packets are occasionally lost, they do not have much impact on the received results. For example, the ICQ and OICQ used in our chat are the UDP protocol 。

Manipulating UDP in Java

You can use the initramsocket and initrampacket classes under the Java.net package in JDK to conveniently control user data packets 。

Before describing them, you must understand the InetAddress class in the same location. InetAddress implements Java. io. the Serializable interface does not allow inheritance. It is used to describe and package an Internet IP address and returns an InetAddress instance in three ways:

GetLocalhost (): returns the instance that encapsulates the local address 。

GetAllByName (String host): returns an array of InetAddress instances that encapsulate the Host address 。

GetByName (String host): returns an instance that encapsulates the Host address. The Host can be a domain name or a valid IP address 。

The initramsocket class is used to create a Socket instance for receiving and sending UDP protocols. It is the same as the Socket class that relies on the SocketImpl class, the implementation of the initramsocket class also relies on the initramscoketimplfactory class specifically designed for it. The initramsocket class has three constructors:

DatagramSocket (): creates an instance. This is a special usage and is usually used for client programming. It does not have a specific listening port and only uses a temporary one 。

DatagramSocket (int port): creates an instance and keeps listening to Port packets 。

DatagramSocket (int port, InetAddress localAddr): This is a very useful builder. When a machine has more than one IP address, the instance created by it only receives packets from LocalAddr 。

It is worth noting that when creating an initramsocket class instance, if the port has been used, a SocketException exception will be thrown and the program is terminated illegally, this exception should be captured. The main methods of the DatagramSocket class include four:

Receive (DatagramPacket d): receives data packets to d. The receive method produces a "blocking "。

Send (DatagramPacket d): Send the message d to the destination 。

SetSoTimeout (int timeout): Set the timeout time in milliseconds 。

Close (): Close mongoramsocket. When the application exits, resources are usually released and the Socket is closed. However, resources cannot be recycled due to abnormal exit, when the program is completed, you should take the initiative to use this method to close the Socket, or close the Socket after an exception is caught 。

"Blocking" is a specialized term that produces an internal loop that pauses the program in this place until a condition is triggered 。


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.