☆UDP:
Encapsulates data and sources and purposes into packets without establishing a connection
The size of each datagram is within the limit of 64k
Because there is no connection, it is an unreliable agreement.
No need to establish connection, fast speed
Datagramsocket and
Datagrampacket class
UDP Transport:
Datagramsocket and Datagrampacket
Set up the sending side, the receiving end.
Set up the data package.
Invokes the Send receive method of the socket.
Close the socket.
The sending and receiving ends are two separate running programs.
UDP Transmission Programming:
☆ Sending End
On the sending side, specify the destination IP and port in the packet object.
Datagramsocket ds = new Datagramsocket (); byte[] by = "HELLO,UDP". GetBytes ();D atagrampacket dp = new Datagrampacket (by,0,b Y.length,inetaddress.getbyname ("127.0.0.1"), 10000);d S.send (DP);d s.close ();
☆ Receiving End
On the receiving side, specify the port to listen on.
Datagramsocket ds = new Datagramsocket (10000); byte[] by = new BYTE[1024];D atagrampacket dp = new Datagrampacket (By,by.leng TH);d s.receive (DP); String str = new String (Dp.getdata (), 0,dp.getlength ()); System.out.println (str+ "--" +dp.getaddress ());d s.close ();
Example:
Sender:
Package Cn.hncu.url.udp;import Java.io.bufferedinputstream;import Java.io.fileinputstream;import Java.io.inputstream;import Java.net.datagrampacket;import Java.net.datagramsocket;import java.net.InetAddress; Import Java.net.socketexception;public class Senddemo {public static void main (string[] args) {try {//send1 ();// This method demonstrates sending small data send2 ();//This method demonstrates sending a large data} catch (Exception e) {e.printstacktrace ();}} private static void Send2 () throws Exception {//for sending larger packetdatagramsocket ds=new Datagramsocket (10000);//Here Specify the port number 10000, the port number on each program is unique, a program to create a new number of datagramsocket need to use a different port number InputStream in=new fileinputstream ("file.txt"); Bufferedinputstream bis=new Bufferedinputstream (in); byte buf[]=new byte[1024];buf[0]=0;int len=0;while ((len= Bis.read (buf, 1, buf.length-1))!=-1) {//Here I leave the first bit of the array as a token to determine if the file has been read or not (len<1023) {buf[0]=1;//file is 0 set to 1 after reading. This is done for the receiver to close the action, if not, whether or not to read the reception//side will remain open}datagrampacket dp=new datagrampacket (buf, Buf.leng Th, Inetaddress.getbyname ("xxx.xxX.xxx.xxx "), 10001); The IP address of the previous line I use xxx.xxx.xxx.xxx to say, when running the program remember to change to your own IP address, the same as Ds.send (DP);} Ds.close ();} private static void Send1 () throws Exception {//for sending less packetdatagramsocket ds=new Datagramsocket (10000); String str= "You know, God is a girl"; Byte buf[]=str.getbytes ("Utf-8");//datagrampacket class, An IP address is constructed using the Datagrampacket dp=new datagrampacket (buf, Buf.length, Inetaddress.getbyname ("xxx.xxx.xxx.xxx") used to create the sending packet. ), 10001);//Note that both the IP and the port are the receiver's Ds.send (DP);d s.close ();}}
Receiving Party:
Package Cn.hncu.url.udp;import Java.net.datagrampacket;import Java.net.datagramsocket;import java.net.InetAddress; Import Java.net.socketexception;public class Receivedemo {public static void main (string[] args) {try {//receive1 ();// Receive small Data receive2 ();//Receive large data} catch (Exception e) {e.printstacktrace ();}} private static void Receive2 () throws Exception {//for receiving larger Packetdatagramsocket ds=new Datagramsocket (10001) ; byte buf[]=new byte[1024];D atagrampacket dp=new datagrampacket (buf, buf.length); while (true) {ds.receive (DP);// Parse out the information we want from DP//Get Ipbyte data[]=dp.getdata (); String Info=new string (data,1,data.length-1); SYSTEM.OUT.PRINTLN (info); if (data[0]==1) {ds.close ();}}} private static void Receive1 () throws Exception {//for receiving less packetdatagramsocket ds=new datagramsocket (10001); b Yte buf[]=new Byte[1024];//datagrampacket class, no IP address is constructed using the Datagrampacket dp=new datagrampacket (buf, which is used to create the received packet). Buf.length);d s.receive (DP);//Parse out the information we want from DP//Get ipstring ip=dp.getaddress (). gethostaddress (); INT Port=dp.getport (); byte Data[]=dp.getdata (); String Info=new string (data); System.out.println (ip+ ":" +port+ "----" +info);}}
Java------Socket Series (ii) UDP