java.netclass Datagramsocket This class represents the socket used to send and receive datagram packets, which is the sending or receiving point of the packet delivery service. java.netclass Datagrampacket This class represents a datagram packet, which implements a packet delivery service without connection. <1> void Send (Datagrampacket p)sends a datagram packet from this socket. Datagrampacket (byte[] buf, int length, inetaddress address, int port)constructs a datagram package that is used to send a package length to a specified port number on a specified host. <2>void receive (Datagrampacket p)receives a datagram packet from this socket. Datagrampacket (byte[] buf, int length)constructs a datagrampacket to receive packets of length. Requirement: Send a piece of text data through the UDP transmission mode. idea: 1. Establish Udpscoket service2. Provide data and encapsulate the data in a packet3. Send the data out via the function of the Scoket service4. Close Resources//Examples are as follows:
Importjava.net.*;//Send Dataclassudpsend{ Public Static voidMain (string[] args)throwsException {//1, set up the UDP service, through the Datagramscoket, establish the endpointDatagramsocket ds =NewDatagramsocket (10000);//You can specify a send port, such as 10000, or you can leave the system randomly assigned by default, without specifying//2. Identify the data and encapsulate it into a packet. Datagrampacket (byte[] buf, int length, inetaddress address, int port) byte[] buf = "UDP is Coming". GetBytes ();//specify information data directlyDatagrampacket DP=NewDatagrampacket (Buf,buf.length,inetaddress.getbyname ("192.168.1.105"), 8888); //3, send data via the Send method of the Scoket service,Ds.send (DP); //4, close the resourceDs.close (); }}
Requirements: Define an application that receives the data transmitted by the UDP protocol and processesidea: 1. Define the Udpscoket service, usually listening to a port, in fact, to the acceptance of the network application to define a digital ID. It is easy to define what data comes up to the application to handle. 2. Define a packet because you want to store the data that you want to receive because there are more features in the packet object that can extract different data information from the byte data3. Store the received byte data in a defined packet via the Receive method of the Scoket service4. Through the unique functions of the packet object, the different data is taken out and printed on the console. 5. Close Resources//Examples are as follows:
//Receive Dataclassudpreceive{ Public Static voidMain (string[] args)throwsException {//1, set up the UDP service, through the Datagramscoket, establish the endpointDatagramsocket ds =NewDatagramsocket (8888);//The port of the receiving application must be specified to match the port number of the send-side packet while(true) { //2, create a byte packet to store the bytes of data received. byte[] buf =New byte[1024]; Datagrampacket DP=NewDatagrampacket (buf,buf.length); //3, receiving data through the Receive method of the Scoket service,Ds.receive (DP);//Blocking Method//4, converts the byte data to a string and outputsString message =NewString (Dp.getdata (), 0, Dp.getlength ()); String IP=dp.getaddress (). gethostaddress (); intPort =Dp.getport (); System.out.println ("IP:" +ip+ "::" + "Port:" +port); System.out.println ("Message:" +message); } //5, close the resource//Ds.close (); }}
Java: The use of UDP for network programming