UDP Learning 1 (send-side demo)

Source: Internet
Author: User

Recently is the final stage of the project-system testing, just a bit of free time this morning, using this time with Java to write a UDP send data demo, I am also recently learning Java (currently doing ASP. NET Web development), you are welcome to point out the little rookie soon after graduation. Enter the text below.

What is UDP? UDP is one of the network transport protocols, the application of the network to each other to transfer data can be followed by a protocol. According to personal understanding, it has the following features: 1. No connection, popular point of the sender does not need to confirm the receiving end is open, do not have to establish a connection, the sending side can also send the data out, but this time will cause the loss of data packets; 2. Unreliable, because it is not connected, Thus, the transmission from the end to the receiving end of the data transfer will be lost, the protocol itself does not make any detection and prompt, so is unreliable; 3. Fast (relative to TCP), because the sender does not need to detect the receiver when transmitting the packet, while TCP is the opposite; 4. Low Network overhead ( Relative to TCP), its packet header is eight bytes, while TCP is 20 bytes; 5. For message-oriented. After summing up the characteristics of UDP, the following is my one via UDP send a packet of the demo.

As long as the flow of data is explicitly sent over UDP, the code is easy to write. The steps are as follows: 1. Set up a send-side socket service (requires an explicit send-side address), 2. Prepare the packet to be sent (which port needs to be sent explicitly), 3. Send; 4. Close the socket service, Because the socket relies on the underlying system resources, it needs to be shut down. Write the following code through notepad++:

1 Importjava.net.*;2 classUdpsenddemo3 {4      Public Static voidMain (string[] args)throwsException5     {6InetAddress ia=inetaddress.getbyname ("localhost");//send-side address7         //set up a send-side socket service8Datagramsocket ds=NewDatagramsocket (12222,ia);//12222 is the send port port number9         //prepare the packets that need to be sentTen         byte[] buf= "UDP lai le". GetBytes (); One         //The ia here refers to the receiving end address, which is tested locally so simply use local as the receiving end. 12222 for the receive port number ADatagrampacket dp=NewDatagrampacket (buf,buf.length,ia,12222); -         //Send - Ds.send (DP); the         //Close Service - ds.close (); -     } -}
View Code

Open the command line, build the class file by Javac compile, this time I encountered a small problem, see:

After viewing, my Notepad with the UTF-8 encoding, and the JVM default is GBK compile, resulting in the compilation failed, so the Notepad to GBK encoding, then compile, finally succeeded, and then run OK, because it is not connected, the data is sent out, but no server received, resulting in the loss of data, in the next article, I will learn how to receive data at the receiving end.

UDP Learning 1 (send-side demo)

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.