Implementation of UDP
The two classes that implement the UDP protocol in Java are the Datagrampacket packet class and the Datagramsocket socket class .
Unlike the TCP protocol implementation, it is:
UDP socket Datagramsocket is a very simple concept compared to sockets and ServerSocket, and there is no connection meaning. sockets only need to know the local port on which the packet is listening and sending .
That is, in the TCP protocol of the mean socket class and ServerSocket class for functional division, UDP protocol only with a packet socket Datagramsocket send and receive data.
Send and receive data, addresses, and ports are encapsulated in the packet class Datagrampacket.
Datagrampacket class Constructors:
public datagrampacket (byte [] buf, int length); /* offset is the offset of the BUF buffer */ public Datagrampacket (byte [] buf,int offset,< Span style= "color: #0000ff;" >int length); /* address means destination port */ public Datagrampacket (byte [] buf,int offset,< Span style= "color: #0000ff;" >int length,inetadress address, int Port)
Common methods:
/*returns the host IP address of the accepted packet, typically used to obtain the sending host IP that received the packet*/ Publicinetadress getadress ();/*returns the host port number of the packet*/ Public intGetport ();/*returns the buffer data that was received or sent*/ Public byte[] GetData ();/*sets the host IP address that returns the emitted packet*/ Public voidsetadress (inetadress iaddr);/*set the port number to send this packet to the remote host*/ Public voidSetport (intIport);
Datagramsocket class
For packet socket Datagramsocket, it is just the port that sends or receives the packet, and there is no need to consider the link. Therefore, a Datagramsocket object needs to be established on both the client and server side, and then the receive () method is used to accept the packet and send the packet through the Send () method.
Port Correlation Control
UDP Network Application Example
The client sends 1-26 numbers to the server side;
The server side returns the corresponding A-Z to the client.
Client program:
Packageweb;ImportJava.net.DatagramPacket;ImportJava.net.DatagramSocket;Importjava.net.InetAddress;ImportJava.util.Scanner; Public classudpcilent { Public Static voidMain (string[] args) {Try { /*UDP cilent SEND*/Datagramsocket Socket=NewDatagramsocket (); System.out.println ("UDP cilent Start ..."); System.out.println ("Enter 1 to", Server would Return U A to Z: "); String Info=NewScanner (system.in). Next (); Socket.send (NewDatagrampacket (Info.getbytes (), Info.length (), Inetaddress.getbyname ("127.0.0.1"), 3000)); System.out.println ("UDP cilent Send Message ..."); byte[] rec=New byte[1024]; Datagrampacket Recivepacket=NewDatagrampacket (REC, rec.length); Socket.receive (Recivepacket); System.out.println ("Receive"); String Recivedata=NewString (Recivepacket.getdata (), 0, Recivepacket.getlength ()); System.out.println ("Receive from" +recivepacket.getaddress (). Gethostaddress () + ":" +recivedata); Socket.close (); System.out.println ("UDP cilent close ..."); } Catch(Exception e) {//Todo:handle Exception } }}
Service-side Programs
Packageweb;ImportJava.net.DatagramPacket;ImportJava.net.DatagramSocket;Importjava.net.InetAddress;ImportJava.net.Socket; Public classUdpserver { Public Static voidMain (string[] args) {Try { /*UDP Receive Message*/Datagramsocket Socket=NewDatagramsocket (3000); byte[]bf=New byte[1024]; Datagrampacket Packet=NewDatagrampacket (BF, 1024); System.out.println ("UDP Server Start ... Wait to Receive Message ... "); Socket.receive (packet); String Info=NewString (Packet.getdata (), 0,packet.getlength ()) + "from" +packet.getaddress (). Gethostaddress () + ":" +Packet.getport (); SYSTEM.OUT.PRINTLN (info); /*Feedback*/inetaddress Address=packet.getaddress (); intport=Packet.getport (); String Str=NewString (Packet.getdata (), 0, Packet.getlength ()); intrenum=integer.parseint (str); intfeedback= ' A '; for(inti=1;i<=26;i++) { if(renum==i) {Feedback=feedback+ (i-1); } } CharBack= (Char) Feedback; String fbstring=back+ ""; byte[] fstr=fbstring.getbytes (); Datagrampacket Feed=NewDatagrampacket (Fstr,fstr.length,address,port); Socket.send (Feed); Socket.close (); System.out.println ("UDP Server close ..."); } Catch(Exception e) {//Todo:handle Exception } } }
Simple examples of Java UDP and a brief description of the knowledge points