Java UDP protocol programming highlights (1)

Source: Internet
Author: User

Java is a commonly used UDP protocol editing tool. So what should we pay attention to when editing Java UDP protocol? Next we will give some necessary explanations for this. It mainly resolves the meanings of some key statements and related content.

1. UDP Protocol definition

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, the fourth layer is the transport layer, which is the top layer of the IP protocol. UDP has the disadvantages of not providing datagram grouping, assembly, and sorting of data packets. That is to say, when a message is sent, it cannot be known whether it is safe and complete.

Ii. Reasons for using 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 for our chat are the UDP protocol. When selecting the protocol, you must be cautious when selecting UDP. In an environment with unsatisfactory network quality, UDP packet loss may be serious.

Iii. Java UDP programming classes

1. InetAddress

Describe and package an Internet IP address. You can use the following method to return an instance:

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.

InetAddress. getByAddress (addr): The InetAddress instance is returned Based on the address string.

InetAddress. getByAddress (host, addr): returns the InetAddress instance based on the host location string and address string.

2. DatagramSocket

Java UDP protocol editing, used to receive and send UDP Socket instances. This class has three constructors:

DatagramSocket (): usually used for client programming. It does not have a specific listening port and only uses a temporary one. The program assigns an available port to the operating system.

DatagramSocket (int port): creates an instance and regularly monitors the Port packets. Usually used on the server

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

The main method of DatagramSocket is as follows:

1) receive (DatagramPacket d): receives data packets to d. The receive method generates a "blocking ". "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.