Java UDP Broadcast code sample

Source: Internet
Author: User

Application scenario: A school, whenever the time is over to provide tips for the class function.

Analysis: The use of UDP to achieve broadcast to the same network segment of the computer to send data, the client received the data, Pop-up prompts dialog box. The service side needs a loop to determine whether the time is over, if you use the dead loop here, very much of the system resources, I tested the cup in 100%, with Java Timer class can be a good solution to this problem, not only to ensure that the program is always running, and does not account for system resources, specific principles, not carefully studied. Here's a look at the code implementation

Service Segment Code:

Import java.io.IOException; Import Java.net.DatagramPacket; Import Java.net.DatagramSocket; Import java.net.InetAddress; Import java.net.SocketException; Import java.net.UnknownHostException; Import Java.text.SimpleDateFormat; Import Java.util.Date; Import Java.util.Timer; public class Udpsend {public void SendData () throws SocketException, unknownhostexception{datagramsocket ds = new Datagra Msocket ();//create socket string str = "1" to send datagram packets; Datagrampacket DP = new Datagrampacket (Str.getbytes (), str.getbytes (). Length, Inetaddress.getbyname ("255.255.255.255 "), 3001); Constructs a datagram package that is used to send packets of length to the specified port number on the specified host to try {Ds.send (DP);} catch (IOException e) {e.printstacktrace ();} ds.close (); public static void Main (string[] args {Timer timer = new timer (); Timer.schedule (new MyTask (), 1000, 1000);} static C Lass MyTask extends java.util.timertask{@Override public void Run () {udpsend TT = new Udpsend (); Date d = new Date (); SimpleDateFormat SDF = new SimpleDateFormat ("HH:mm:ss"); String STRDate = Sdf.format (d); String[] Classtime = {"17:18:00", "17:19:00", "17:20:00"}; for (int i = 0;i<classtime.length;i++) {if (Classtime[i].equals (strdate)) {try {tt.senddata ();} catch ( SocketException e) {//Todo auto-generated catch block E.printstacktrace ();} catch (Unknownhostexception e) {//Todo Au To-generated Catch block E.printstacktrace (); } } } } } }

Customer single Simple code example, no loops

Package UDP; Import Java.net.DatagramPacket; Import Java.net.DatagramSocket; public class Udprecv {public static void main (string[] args) throws Exception {datagramsocket ds = new Datagramsocket (30 01)//Create a Receive datagram socket and bind it to the specified port on the local host byte[] buf = new byte[1024]; Datagrampacket DP = new Datagrampacket (buf, 1024); Ds.receive (DP); String strrecv = new String (Dp.getdata (), 0, Dp.getlength ()) + "from" + dp.getaddress (). Gethostaddress () + ":" + DP.GETP ORT (); System.out.println (STRRECV); Ds.close (); } }

Related Article

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.