A brief analysis of the UDP transmission of Java Network Programming (i.)

Source: Internet
Author: User

There are generally two kinds of--tcp and UDP in network transmission mode. This article is for a brief look at how UDP is transmitted.

What are the characteristics of UDP?

1, for non-connected;
2, for the message, and each message does not exceed 64KB;
3, unreliable but efficient (do not establish a connection);
4, support a pair of one or one-to-many, many-to-one, many-to-many interactive communication;

For these characteristics, there is no reason why, the fact is only!

Second, how to play UDP in Java?

In Java, the establishment of UDP service-related operations is encapsulated in the Datagramsocket class, encapsulating datagram-related operations in the Datagrampacket class.

Third, the specific play of the idea is what?

UDP transmission is divided into the sending and receiving end.

Send side:
1, the establishment of UDP services;

2, specify the data, and encapsulate the data into a datagram packet;
3. Send out the datagram packet;
4, close the service resources.

Receiving end:
1, set up a UDP service, and listen to a certain port;
2, the establishment of a data packet for receiving data;
3, receive the data packet, and the data packets in the various data read out;
4, close the resources. (optional)

Four, pull out "donkey" to sneak out?

Import Java.net.*;class udpsend {public static void main (string[] args) throws Exception{datagramsocket ds = new datagrams Ocket (); byte[] buf = "I am kevin! Nice to meet you! ". GetBytes ();D atagrampacket dp = new Datagrampacket (buf, Buf.length, Inetaddress.getlocalhost (), 9090);d S.send (DP); Ds.close ();}}  Class Udprecv{public static void Main (string[] args) throws Exception{datagramsocket ds = new Datagramsocket (9090); byte[] BUF = new byte[1024];D atagrampacket dp = new Datagrampacket (buf, Buf.length);d s.receive (DP); String IP = dp.getaddress (). gethostaddress (); int port = Dp.getport (); String data = new String (Dp.getdata (), 0, Dp.getlength ()); SYSTEM.OUT.PRINTLN (IP + "--" + Port + ":" + data ");d s.close ();}}

Five, then casually talk about?

Reference API Documentation , you can see that the Datagrampacket class has 6 constructor methods, as follows:

It can be found that all the construction methods used to receive packets have no address and port parameters, and all the construction methods used to send packets must specify the address and port. It is also very well understood that only the storage can be received, and the sending must know the destination in order to send out.



A brief analysis of the UDP transmission of Java Network Programming (i.)

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.