Java UDP broadcast code example

Source: Internet
Author: User

Application Scenario: A school provides the function of prompting for a class at the end of each lesson.

Analysis: UDP is used to send data to computers in the same network segment. After the client accepts the received data, a prompt dialog box is displayed. The server needs a loop to determine whether the time has reached the end time. If an infinite loop is used here, it accounts for a lot of system resources. I tested that the CPU is at 100%, using the Timer class in Java can solve this problem well, which ensures that the program is always running and does not occupy system resources. I have not studied the principle carefully. The code implementation is as follows:

 

Service segment code:

Import Java. io. ioexception; <br/> Import java.net. export rampacket; <br/> Import java.net. datagramsocket; <br/> Import java.net. inetaddress; <br/> Import java.net. socketexception; <br/> Import java.net. unknownhostexception; <br/> Import Java. text. simpledateformat; <br/> Import Java. util. date; <br/> Import Java. util. timer; <br/> public class udpsend {<br/> Public void senddata () throws socketexception, <br/> unknownhostexception {<br/> datagramsocket DS = new datagramsocket (); // create a socket for sending a datagram packet <br/> string STR = "1"; <br/> datagrampacket dp = new datagrampacket (Str. getbytes (), <br/> Str. getbytes (). length, <br/> inetaddress. getbyname ("255.255.255.255"), 3001); <br/> // construct a datagram packet, sends a length package to the specified port number on the specified host <br/> try {<br/> DS. send (DP); <br/>}catch (ioexception e) {<br/> E. printstacktrace (); <br/>}< br/> DS. close (); </P> <p >}< br/> Public static void main (string [] ARGs) {<br/> timer = new timer (); <br/> timer. schedule (New mytask (), 1000,100 0); <br/>}< br/> static class mytask extends Java. util. timertask {<br/> @ override <br/> Public void run () {<br/> udpsend TT = new udpsend (); <br/> date d = new date (); <br/> simpledateformat SDF = new simpledateformat ("HH: mm: SS"); <br/> string strdate = SDF. format (d); <br/> string [] classtime = {"17:18:00", "17:19:00", "17:20:00" };< br/> for (INT I = 0; I <classtime. length; I ++) {<br/> If (classtime [I]. equals (strdate) {<br/> try {<br/> TT. senddata (); <br/>}catch (socketexception e) {<br/> // todo auto-generated Catch Block <br/> E. printstacktrace (); <br/>} catch (unknownhostexception e) {<br/> // todo auto-generated Catch Block <br/> E. printstacktrace (); <br/>}< br/>

 

Example of a simple client ticket code without loops

 

Package UDP; <br/> Import java.net. export rampacket; <br/> Import java.net. extends ramsocket; <br/> public class udprecv {<br/> Public static void main (string [] ARGs) throws exception {<br/> extends ramsocket DS = new datagramsocket (3001 ); // create a socket for receiving data packets and bind it to the specified port on the local host <br/> byte [] Buf = new byte [1024]; <br/> datagrampacket dp = new datagrampacket (BUF, 1024); <br/> DS. receive (DP); <br/> string strrecv = new string (DP. getdata (), 0, DP. getlength () + "from" <br/> + dp. getaddress (). gethostaddress () + ":" + dp. getport (); <br/> system. out. println (strrecv); <br/> DS. close (); <br/>}< br/>

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.