Java method for implementing UDP protocol based on socket service _java

Source: Internet
Author: User

This article illustrates the Java approach to implement UDP protocol based on socket service. Share to everyone for your reference. Specifically as follows:

Example 1:

Receive class:

Package Com.socket.demo; 
Import java.io.IOException; 
Import Java.net.DatagramPacket; 
Import Java.net.DatagramSocket; public class Udpreceivedemo {public static void main (string[] args) throws ioexception{System.out.println ("Receive-side start ... 
 ..."); 
  * 2, the establishment of UDP socket services, must be clear a port number 3, create a packet to store the received data, easy to use the method of packet objects to resolve these data 4, use the Datagramsocket receive method to store received data in the packet 5, through the packet method to resolve data packets in the data 5, shut down the socket service///udpsocket service, using Datagramsocket object Datagramsocket ds=new datagramsocket (100 
  02); 
  Use Datagrampacket to encapsulate data into the object byte[] buf=new byte[1024]; 
  Datagrampacket dp=new datagrampacket (buf, buf.length); 
  Via UDP socket service to send the data packages, through the Send method Ds.receive (DP); 
  The data in a packet is parsed by means of a packet, such as a String ip=dp.getaddress (), such as address, port, and data content. Gethostaddress (); 
  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); 
 Close resource Ds.close ();  } 
}

Send class:

Package Com.socket.demo; 
Import java.io.IOException; 
Import Java.net.DatagramPacket; 
Import Java.net.DatagramSocket; 
Import java.net.InetAddress; 
Import java.net.SocketException; 
Import java.net.UnknownHostException; 
public class Udpsenddemo {public 
 static void Main (string[] args) throws ioexception{System.out.println 
  ("Sender-side startup ... ... "); 
  * * 1, create UDP transmission of the sender 
  2, establish UDP socket service 
  3, the data to be sent encapsulated into the packet 
  4, through UDP socket service to send data packages 
  5, Turn off Socket 
  Service 
  ///udpsocket service, use Datagramsocket object 
  datagramsocket ds=new datagramsocket (8888);//Listening port 
  //The data to be sent will be encapsulated in 
  the packet String str= "UDP transport demo, go"; 
  Use Datagrampacket to encapsulate data into the object 
  byte[] Buf=str.getbytes (); 
  Datagrampacket dp= 
    New Datagrampacket (buf, Buf.length,inetaddress.getbyname ("192.168.1.100"), 10002); 
  Via UDP socket service to send the data packages, through the Send method 
  Ds.send (DP); 
  Close Resource 
  ds.close (); 
 } 

Example 2:

Receive class:

Package Com.socket.demo; 
Import java.io.IOException; 
Import Java.net.DatagramPacket; 
Import Java.net.DatagramSocket; public class UDPReceiveDemo2 {public static void main (string[] args) throws ioexception{System.out.println ("Receive-side start ... 
 ......"); 
  * 2, the establishment of UDP socket services, must be clear a port number 3, create a packet to store the received data, easy to use the method of packet objects to resolve these data 4, use the Datagramsocket receive method to store received data in the packet 5, through the packet method to resolve data packets in the data 5, shut down the socket service///udpsocket service, using Datagramsocket object Datagramsocket ds=new datagramsocket (100 
  03); 
   while (true) {//Use Datagrampacket to encapsulate data into the object byte[] buf=new byte[1024]; 
   Datagrampacket dp=new datagrampacket (buf, buf.length); 
   The socket service via UDP sends the data packages through the Send method Ds.receive (DP), or the blocking type. 
   The data in a packet is parsed by means of a packet, such as a String ip=dp.getaddress (), such as address, port, and data content. Gethostaddress (); 
   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); 
 //Close resource//ds.close (); } 
}

Send class:

Package Com.socket.demo; 
Import Java.io.BufferedReader; 
Import java.io.IOException; 
Import Java.io.InputStreamReader; 
Import Java.net.DatagramPacket; 
Import Java.net.DatagramSocket; 
Import java.net.InetAddress; public class UDPSendDemo2 {public static void main (string[] args) throws ioexception{System.out.println ("The sender starts ...") 
 ); * * 1, create UDP transmission of the sender 2, establish UDP socket service 3, the data to be sent encapsulated in the packet 4, through UDP socket service to send data packages 5, shut down socket service///udps Ocket service, using the Datagramsocket object Datagramsocket ds=new datagramsocket (9999);//Listening port//The data to be sent encapsulated in the packet//string str= "UDP transport 
  demo, go "; 
  BufferedReader bufr=new BufferedReader (New InputStreamReader (system.in));//keyboard input String line=null; Use Datagrampacket to encapsulate data in this object while ((Line=bufr.readline ())!=null) {byte[] buf=line.getbytes ();//Datagrampacket 
   dp= New Datagrampacket (buf, Buf.length,inetaddress.getbyname ("192.168.1.100"), 10003); 
   Via UDP socket service to send the data packages, through the Send method Ds.send (DP); if ("886". Equals (line)){break; 
 }//Close resource ds.close (); } 
}

The Operation effect chart is as follows:

Receive:

Send:

I hope this article will help you with your Java programming.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.