Socket Service UDP protocol for Java

Source: Internet
Author: User

Exercise 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 service of setting up UDP socket, must understand a port number 3, create a packet. For storing received data, it is convenient to use the method of the packet object to resolve these data 4, using the Datagramsocket receive method to store the received data into the packet 5, the data packet through the method of parsing data 5, close the socket service */// Udpsocket service, use Datagramsocket object Datagramsocket ds=new datagramsocket (10002);//use Datagrampacket to encapsulate data into the object byte[] buf= New byte[1024];D atagrampacket dp=new datagrampacket (buf, buf.length);//Send data packets via UDP socket service via the Send Method Ds.receive ( DP);//The data in the packet is parsed by means of a packet, for example. Address, port, data content, etc. String ip=dp.getaddress (). 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 ("Send side start ...");/* *  1, Create the UDP transmission of the Send side 2, the establishment of the UDP Socket service 3, the data will be sent encapsulated into the packet 4, the socket service via UDP to send data packets 5, close the socket service *///udpsocket service. Use the Datagramsocket object Datagramsocket ds=new datagramsocket (8888);//Listener port//The data that will be sent is encapsulated into the packet string str= "UDP transport demo. Go ";//use Datagrampacket to encapsulate data into the object byte[] buf=str.getbytes ();D atagrampacket dp=new datagrampacket (buf, Buf.length, Inetaddress.getbyname ("192.168.1.100"), 10002);//Send data packets via UDP socket service via Send method Ds.send (DP);//Close Resource ds.close ();}}

Exercise 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, set up UDP socket service, must understand a port number 3, create a packet, to store the received data, convenient to use the method of the packet object to resolve these data 4, using the Datagramsocket receive method to store the received data into the packet 5, Resolves data in a packet by means of a packet 5, closes the socket service *///udpsocket service. Use the Datagramsocket object Datagramsocket ds=new datagramsocket (10003), while (true) {//Use Datagrampacket to encapsulate data into the object byte[] buf= New byte[1024];D atagrampacket dp=new datagrampacket (buf, buf.length);//Send data packets via UDP socket service via the Send Method Ds.receive ( DP);//plug-in.

The data in the packet is parsed by means of the packet, for example, the address, port, data content, and so on String ip=dp.getaddress (). 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 ("Send side start ...");/* * 1, create the transmit side of the UDP transmission 2, Set up the UDP socket service 3, the data will be sent encapsulated into the packet 4, the socket service via UDP to send data packets 5, close the socket service *///udpsocket service, Use the Datagramsocket object Datagramsocket ds=new datagramsocket (9999);//Listener port//The data that will be sent is encapsulated into 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 into the object while ((Line=bufr.readline ())!=null) {byte[] buf=line.getbytes ();//datagrampacket dp=new Datagrampacket (buf, Buf.length,inetaddress.getbyname ("192.168.1.100"), 10003);//Send data packets via UDP socket service. Via the Send method Ds.send (DP), if ("886". Equals line) {break;}} Close resource Ds.close ();}}
Perform:

Receive


Send


Socket Service UDP protocol for Java

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.