The example of this article tells the method of Java's UDP protocol based on the SOCKET service. Share it for everyone for your reference. The specifics are as follows:
Example 1:
Receive class:
package com.socket.demo; Import Java.io.ioException; Import Java.net.dataGrampacket; Import Java.net.datagramsocket; Public Class IveDemo {Public Static Void Main (String [] Args) Throws IOEXCEPTION {System.out.println ("Receiving end start ..."); /* 2, the set service of UDP's SOCKET must be clearly clarified. 3. Create a data packet to store the received data to facilitate the analysis of the method of the data packet object. This data 4. Use the Receive method of DataGramsocket to store the received data to the data packet 5. Pay the data in the data packet method by the data packet 5. Close the socket service*/// udpsocket service, use the DataGramsocket DATAGRAMSOT DS = New DataGramsocket (10002); // Use DataGrampacket to encapsulate the data in the object [] buf = new byte [1024]; ; // Data through UDP's socket service Send out the package and analyze the data in the data packet through the method ds.Receive (dp); // String ip = dp.getaddress (). //String name=dp.getAddress().getHostName(); int port=dp.getPort(); String text=new String(dp.getData(),0,dp.getLength()); //System.out .println ("-----"+IP+"-----"+Name+"-----"+Port+"-----"+Text); System.out.println ("- ---"+ip+"----------"+port+"-----"+text); //关闭资源ds.close(); } }
Send class:
package com.socket.demo; Import Java.io.ioException; Import Java.net.dataGrampacket; Import Java.net.datagramsocket; Import ddress; Import Java.net.socketexception; Import Java.net.unknownHostexception; Public Class UdpsendDemo {Public Static Void Main (String [] ARGS) Throws IOEXCEPTION {System.out.println ("Send the end ..."); UDP's socket Service 3. Package the data to be sent to the data package 4. Send the data package through the UDP Socket service 5. Close the socket service*// Udpsocket service, use the DataGramsocket object datagramsocket DS = NEW DataGramsocket (8888); / / /Monitoring port // Pack the data to be sent to the string string strikers = "UDP transmission demonstration, go"; // Use datagrampacket to encapsulate the data in the object [] buf = str.getBytes (); New DataGrampacket (buf, buf.length, Inetaddress.getBYNAME ("192.168.100"), 10002); // Send the data package out through the SOCKET service of the UDP, through the send method ds.send (dp); ds.close ();}}
Example 2:
Receive class:
package com.socket.demo; Import Java.io.ioException; Import Java.net.dataGrampacket; Import Java.net.datagramsocket; Public Class ivEdemo2 {Public Static Void main (String [] Args) Throws IOEXCEPTION {System.out.println ("Receiving end start ..."); /* 2, the set service of UDP's SOCKET must be clearly clarified. 3. Create a data packet to store the received data to facilitate the analysis of the method of the data packet object. This data 4. Use the Receive method of DataGramsocket to store the received data to the data packet 5. Pay the data in the data packet method by the data packet 5. Close the socket service*/// udpsocket service, use the DataGramsocket DATAGRAMSOT DS = New DataGramsocket (10003); While (True) {// Use DataGrampacket to encapsulate the data in the object [] buf = new byte [1024]; , buf.length); // Through UDP The socket service sends out the data packet, through the send method ds.receive (dp); // blocking type. // Perform the data in the packet through the method of the packet, such as string IP = dp.getaddress (). Gethostaddress (); // string name = dp.getAdddress (). Gethostname () ; int port=dp.getPort(); String text=new String(dp.getData(),0,dp.getLength()); //System.out.println("-----"+ip+"- ---- "+name+" ----- "+port+" ----- "+text); system.out.println (" ----- "+ip+" --------------------------------------------------------------------------- --- "+Port+" ------ "+Text);} // Turn off resources //ds.close ();}}
Send class:
package com.socket.demo; Import Java.io.BufferedReader; Import Java.io.ioException; Import Java.Io.inputStreamReader; Import cket; Import Java.net.datagramsocket; Import Java.net.inetadDress; Public Class UdpsendDemo2 {Public Static Void Main (String [] ARGS) Throws IOEXCEPTION {System.out.println ("Send the end ........."); UDP's socket Service 3. Package the data to be sent to the packet 4. Send the data package through the Socket service of UDP 5. Close the socket service*// UDPSOCKET service, use the DataGramsocket object DataGramsocket DS = NEW DATAGRAMSOCKET (99999); / / /Monitoring port // Pack the data to be sent to the data packet // string str = "UDP transmission demonstration, go"; bufferedReader bufr = new bufferedReader (new inputStreamReamReader (System.in)); // The keyboard input String Line = Null ; // Use DataGrampacket to encapsulate the data into this object (line = bufr.readline ())! = Null) {byte [] buf = line.getbytes (); // datagrampacket dp = new datagrampacke t (buf, buf .Length, Inetaddress.getByname ("" 192.168.1.100 "), 10003); // Send the data packet out through UDP's socket service, through the send method ds.send (dp); if (" 886 ".equals (line) ) {break;}} // Turn off the resource ds.close ();}}
The running renderings are as follows:
take over:
send:
It is hoped that this article is helpful to everyone's Java program design.