Java uses UDP

Source: Internet
Author: User

Protocols that can be used in Java communication include TCP protocol and UDP protocol

UDP Protocol Concepts

The full name of the UDP protocol is the User Datagram Protocol, which is used in the network as the TCP protocol for processing packets, but it is a non-connected protocol. In the OSI model, the fourth layer, the transport layer, is in the upper layer of the IP protocol. UDP has the disadvantage of not providing packet grouping, assembling, or sorting packets, that is, when the message is sent, it is not possible to know whether or not it arrives safely and completely.

In summary: UDP does not provide a reliable guarantee to ensure that the data accurate to arrive at the destination

Why use UDP when choosing to use the protocol, the choice of UDP must be cautious? In the network quality is not very satisfied with the environment, UDP packet loss will be more serious? But because of the characteristics of UDP: it does not belong to the connection protocol, thus has the advantage of small resource consumption, processing speed, so usually the audio ? video and normal data are transmitted with more UDP, because they lose one or two packets at a time, and do not have too much effect on receiving results? For example, we chat with ICQ and OICQ is the use of the UDP protocol?

Use steps

The core classes of UDP in Java are datagramsocket and datagrampacket

Specific Use steps:

1, a new Datagramsocket;

2, through the Datagrampacket to undertake the package;

3. Receive by using Receive method;

4. Read data converted to string

manipulating UDP in Java

With the Datagramsocket and Datagrampacket classes located under the Java.net package in the JDK, you can easily control user data messages?
Before describing them, you must understand the InetAddress class at the same location? InetAddress implements the java.io. Serializable interface, which does not allow inheritance? It is used to describe and wrap an Internet IP address, returning a inetaddress instance through three methods:
Getlocalhost (): Returns an instance of the encapsulated 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? Where host can be a domain name or a legitimate IP address?
The Datagramsocket class is used to create a socket instance that receives and sends a UDP protocol? And the socket class relies on the SocketImpl class, The implementation of the Datagramsocket class also relies on the Datagramscoketimplfactory class specifically designed for it? There are 3 builders of the Datagramsocket class:
Datagramsocket (): Create an instance? This is a very special use, usually for client programming, it does not have a specific listening port, just use a temporary?
Datagramsocket (int port): Create an instance and pin the messages that listen to port ports?
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 messages from LOCALADDR.
It is important to note that when creating an instance of the Datagramsocket class, if the port is already in use, a SocketException exception is thrown, and the program terminates illegally, should the exception be taken into the trap? The main methods of the Datagramsocket class are 4:
Receive (Datagrampacket D): Receives data message to D? The Receive method produces a "block"?
Send (Datagrampacket D): Send paper D to destination?
Setsotimeout (int timeout): Set the time-out period in milliseconds?
Close (): Close Datagramsocket? When an application exits, it usually releases the resource voluntarily, shutting down the socket, but could cause the resource not to be recycled due to an abnormal exit. Therefore, you should actively use this method to close the socket when the program is completed. Or close the socket after catching the exception thrown?
"Blocking" is a professional noun that produces an internal loop that causes the program to pause in this place until a condition triggers

Sample code
{server that receives data}   byte[] buf = new byte[1000];  Datagramsocket ds = new Datagramsocket (12345);//Start monitoring 12345 ports  datagrampacket IP = new Datagrampacket (buf, Buf.length) ;//Create an instance of the receiving datagram  while (true)  {ds.receive (IP);//block until you receive the datagram and load the data into the IP  System.out.println (new String (BUF)) ;}  {Client sending data}   InetAddress target = inetaddress.getbyname ("www.xxx.com");//Gets the address instance of the target machine  datagramsocket ds = new Datagramsocket ( 9999);//Send datagram from 9999 port  String hello = "Hello, I am come in!"; /data to be sent  byte[] buf = Hello.getbytes ();//Convert data to byte type  op = new Datagrampacket (buf, Buf.length, Target, 12345) ;//Package The data in the BUF buffer  ds.send (OP);//Send data  





Java uses UDP

Related Article

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.