1. UDP protocol to send data and receive data
The UDP protocol sends the data:
• Create a Socket object on the sending side
• Create data and package the data
• Call the Send method of the socket object to send the packet
• Freeing resources
The UDP protocol receives data :
• Create a Socket object on the receiving end
• Create a packet, receive data (Receive container)
• Call the Receive method of the socket object, receive the packet
• parse the packet and display it in the console
• Freeing resources
2. Code implementation
(1) First we write the program of the sending end , as follows:
1 PackageCom.himi.udpDemo;2 3 Importjava.io.IOException;4 ImportJava.net.DatagramPacket;5 ImportJava.net.DatagramSocket;6 Importjava.net.InetAddress;7 Importjava.net.SocketException;8 Importjava.net.UnknownHostException;9 Ten /** One * A * UDP protocol sends data: - * 1. Create the socket object on the sending side - * 2. Create the data and package the data the * 3. Call the Send method of the socket object, send the packet - * 4. Releasing Resources - * - */ + Public classUdpsenddemo { - Public Static voidMain (string[] args)throwsIOException { + //Create a send-side socket object, where UDP uses the client's socket object class Datagramsocket. A //Datagramsocket:datagramsocket () atDatagramsocket ds =NewDatagramsocket (); - - //Create the data and package the data - //Datagrampacket: This class represents a datagram packet - /**Datagrampacket (byte[] buf, int length, inetaddress address, int port) - * * Constructs a datagram package that is used to send packets of length to the specified port number on the specified host. in */ - //Create Data to byte[] bytes = "The Wind and the world, the wind win." The Mocker of the wind and clouds. Holy Heart tactic, Xuanwu true power ". GetBytes (); + //IP Address Object -InetAddress address = Inetaddress.getbyname ("49.123.72.145"); the //Port number * intPort = 10086; $ //Data PackagingPanax NotoginsengDatagrampacket DP =Newdatagrampacket (Bytes, bytes.length, address, port); - the + //call the Send method of the socket object, send the packet A //Public void Send (Datagrampacket p) the Ds.send (DP); + - $ //Freeing Resources $ ds.close (); - - } the -}
(2) The next implementation is the code of the receiving end , as follows:
1 PackageCom.himi.udpDemo;2 3 Importjava.io.IOException;4 ImportJava.net.DatagramPacket;5 ImportJava.net.DatagramSocket;6 Importjava.net.InetAddress;7 8 /**9 * Ten * UDP protocol receives data: One * 1. Create a socket object on the receiving end A * 2. Create a packet, receive data (Receive container) - * 3. Call the Receive method of the socket object, receive the packet - * 4. Parse the packet and display it in the console the * 5. Releasing Resources - * - */ - + Public classUdpreceivedemo { - + Public Static voidMain (string[] args)throwsIOException { A //create a socket object on the receiving end at //datagramsocket (int port): Creates a datagram socket and binds it to the specified port on the local host -Datagramsocket ds =NewDatagramsocket (10086); - - //Create a packet, receive data (Receive container) - //Datagrampacket (byte[] buf, int length) - byte[] bytes =New byte[1024]; in intLength =bytes.length; - toDatagrampacket DP =Newdatagrampacket (bytes, length); + - //calls the Receive method of the socket object, receives the packet the //Public void receive (Datagrampacket p) *Ds.receive (DP);//Blocking Method $ Panax Notoginseng //parse the packet and display it in the console - //get the IP of each other the //Public inetaddress getaddress () +InetAddress address =dp.getaddress (); AString IP =address.gethostaddress (); the + //Public byte[] GetData (): Buffer to get data - //public int GetLength (): Gets the actual length of the data $ byte[] Bys =Dp.getdata (); $ intLen =dp.getlength (); -String s =NewString (bys,0, Len); - theSYSTEM.OUT.PRINTLN ("Send-side" +ip+ "Data:" +s); - Wuyi the //Freeing Resources - ds.close (); Wu - About $ } - -}
(3) Operation:
--Run the receiver first ( similar to the server first )
--and then run the send side, this time, the upper receiver console, will show the sender sent over the data, the effect is as follows:
Java Basic Knowledge Enhancement Network programming note 03:UDP UDP protocol send data and receive data