UDP transport
There are two main types of UDP, the sender and the receiver, Datagramsocket, Datagrampacket
Establish the sending and receiving end
Set up a data package
Call the receive send method of the socket
Close socket
Note: The sending and receiving ends are two separate running programs
UDP Send Side Demo
DatagramPacket(byte[] buf, int length,InetAddress address, int port)
Constructs a datagram package that is used to length
send a package of length to a specified port number on a specified host.
public static void Udpsenddemo () throws IOException {System.out.println ("send Side");/* * Create send side of UDP transport: * 1. Setting up a UDP socket service * 2. Will The data sent is encapsulated in the packet * 3. Send data packets with UDP socket service * 4. Close the socket service *///set up UDP socket service, use Datagramsocket object Datagramsocket ds = new Datagramsocket ();//Do not specify the port number, the sending side, is random//data encapsulated into packet string str = "UDP send";//use Datagrampacket to encapsulate the data into the package of the object byte[] by = Str.getbytes ();D atagrampacket dp = new Datagrampacket (By,by.length,inetaddress.getbyname ("127.0.0.1"), 6534);// Send data packets through the socket service of UDP, send method Ds.send (DP);d s.close ();}
UDP Receive Side Demo
public static void Udpreceivedemo () throws IOException {//Accept-side System.out.println ("Receive-side"); */* Create the receiving end of the UDP transport: * 1. Create a UDP socket service, because to receive data, you must explicitly specify the port number * 2. Create a packet that stores the data that is received so that it can be parsed using a packet method * 3. The data received using the Receive method of the socket service is stored in the packet * 4. Resolves the data in the package with the method in the packet * 5. Close *///set up UDP socket service, use Datagramsocket object datagramsocket ds = new Datagramsocket (6534);//Receive Port 6534// Create packet byte[] buf = new byte[1024];D atagrampacket dp = new Datagrampacket (buf, buf.length);//Use the Receive method to store the data to a packet ds.receive (DP );///Note that this method is a blocking//method of using a packet object to parse the data, such as: address, port, content string IP = dp.getaddress (). gethostaddress ();//Get IP address string int port = Dp.getport ();//Gets the port string datatext = new String (Dp.getdata (), 0,dp.getlength ());//encapsulates the data into a string object System.out.println ("IP : "+ip+" Port: "+port+" Data: "+datatext";//close Ds.close ();}
a simple QQ chat program based on multithreaded simulation
Import Java.io.bufferedreader;import Java.io.ioexception;import Java.io.inputstreamreader;import Java.net.datagrampacket;import Java.net.datagramsocket;import Java.net.inetaddress;class Receive implements Runnable{private datagramsocket ds;public Receive (datagramsocket ds) {this.ds = ds;} public void Run () {try {while (true) {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 datatext = new String (Dp.getdata (), 0,dp.getlength ()), if (Datatext.equals ("over")) {System.out.println ("IP:" + ip+ "Port:" +port+ "Exit chat room"); Else{system.out.println ("IP:" +ip+ "Port:" +port+ "Data:" +datatext);}}} catch (Exception e) {//Todo:handle Exception}}}class send implements Runnable{private datagramsocket ds;public Send (Data Gramsocket ds) {this.ds = ds;} public void Run () {try {bufferedreader br = new BufferedReader (new InputStreamReader (system.in)); String line = null;while (line = Br.readline ())!=null) {byte[] buf = line.getbytes ();D atagrampacket dp = new Datagrampacket (Buf,buf.length,inetaddress.getbyname (" 127.0.0.255 "), 6534),//ip the effective bit is 0-254,255 is broadcast, that is, 127.0.0, all the surviving computers on this IP field can receive the information Ds.send (DP) I send, if (" over ". Equals ( line)); Ds.close ();} catch (Exception e) {//Todo:handle Exception}}}public class Main {public static void main (string[] args) throws Ioexcept Ion{datagramsocket dsend =new datagramsocket ();D Atagramsocket Dr =new Datagramsocket (6534); Send send = new Send (dsend); Receive receive = new receive (DR); thread T1 = new thread (send); Thread t2 = new Thread (receive); T1.start (); T2.start ();}}
Java Learning Lesson 60th-UDP Protocol & Multithreading-based simulation simple QQ Chat Program