Java 25-4 Network programming of UDP protocol transmission ideas

Source: Internet
Author: User

UDP transport

Two classes:Datagramsocket and Datagrampacket(see API)

A: Set up the sending side, the receiving end.

B: Set up the data packet.

C: Call the socket's send receive method.

D: Close the socket.

E: The sending and receiving end is two separate running programs.

UDP transmission-send-side ideas

UDP sends data:
A: Create A Socket send-side object
B: Create a Datagram Package (package the data)
C: Call the Socket object to send a datagram packet
D: Freeing resources (underlying IO stream)

UDP transport-send-side code

1  Public Static voidMain (string[] args)throwsIOException {2         //A: Create A send-side socket object3         //Datagramsocket (): This class represents the socket used to send and receive datagram packets. 4Datagramsocket ds =NewDatagramsocket ();5         6         //B: Create the data and package the data7         //Datagrampacket (byte[] buf, int length, inetaddress address, int port)8         //Create Data9         byte[] bys = "UDP Send Side". GetBytes ();Ten         //Data Length One         intLength =bys.length; A          -         //Create an IP address object -InetAddress address = Inetaddress.getbyname ("172.19.xx.xx"); the          -         //Creating Ports -         intPort = 10010; -          +         //To package the data -Datagrampacket DP =NewDatagrampacket (bys,length,address,port); +          A         //C: Call the Send method of the socket object to send the packet at         //Public void Send (Datagrampacket p) packet for non-connected packet delivery service - Ds.send (DP); -          -         //D: Releasing Resources - ds.close (); -     } in  -}

UDP transmit-receive-side thinking

UDP Receives data:
A: Creating A Socket Receive-side object
B: Create a datagram package to receive data (Create container)
C: Call the Socket object's method to receive the datagram packet
D: Parse packet (byte converted to string) and print in console
E: Freeing Resources

UDP transport-Receive-side code

1  Public classReceivedemo {2      Public Static voidMain (string[] args)throwsIOException {3         //create a receive-side socket object4         //datagramsocket (int port)5Datagramsocket ds =NewDatagramsocket (10010);6 7         //Create a packet (Receive container)8         //Datagrampacket (byte[] buf, int length)9         byte[] Bys =New byte[1000];Ten         intLength =bys.length; OneDatagrampacket DP =Newdatagrampacket (bys, length); A  -         //call the Receive method of the socket object to receive data -         //Public void receive (Datagrampacket p) theDs.receive (DP); //  block type  -  -         //parse the packet and display it in the console -         //get the IP of each other +         //Public inetaddress getaddress () -InetAddress address =dp.getaddress (); +String IP =address.gethostaddress (); A         //Public byte[] GetData (): Get Data buffer at         //public int GetLength (): Gets the actual length of the data -         byte[] Bys2 =Dp.getdata (); -         intLen =dp.getlength (); -String s =NewString (BYS2, 0, Len); -The data passed by SYSTEM.OUT.PRINTLN (IP +) is: "+s); -  in         //Freeing Resources - ds.close (); to     } +}

Note: Because the UDP receiver is a blocking receive, start the receiving end first, and then start the send side.

A diagram of the UDP protocol sending and receiving data:

Java 25-4 Network programming of UDP protocol transmission ideas

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.