Easy-to-understand multicast (textbook)

Source: Internet
Author: User

1. repeatedly broadcast a piece of news.

Code:
  1. Package mulsocket. frombook;
  2. Import java.net .*;
  3. Public class broadcast extends thread {
  4. String S = "today's weather is fine, good scenery everywhere! ";
  5. Int Port = 5858; // The multicast port.
  6. Inetaddress group = NULL; // multicast group
  7. Multicastsocket socket = NULL; // multicast socket
  8. Broadcast (){
  9. Try {
  10. Group = inetaddress. getbyname ("239.20.8.0"); // you can specify 239.20.8.0 as the multicast group.
  11. Socket = new multicastsocket (port); // The multicast socket will be broadcast on the port.
  12. Socket. settimetolive (0); // The data packet sent by the Multi-Point broadcast socket is in the local network.
  13. Socket. joingroup (group );
  14. // Join the multicast group. After joining the group, the datagram sent by the socket can be received by the member added to the group.
  15. } Catch (exception e ){
  16. }
  17. }
  18. Public void run (){
  19. While (true ){
  20. Try {
  21. Datagrampacket packet = NULL; // The datagram to be broadcast
  22. Byte data [] = S. getbytes ();
  23. Packet = new datagrampacket (data, data. length, group, Port );
  24. System. Out. println (new string (data ));
  25. Socket. Send (packet); // broadcast Datagram
  26. Sleep (2000 );
  27. } Catch (exception e ){
  28. System. Out. println (E. tostring ());
  29. Break;
  30. }
  31. }
  32. }
  33. Public static void main (string ARGs []) {
  34. New broadcast (). Start ();
  35. }
  36. }

2. Join the multicast group to receive Broadcast Data

Code:
  1. Package mulsocket. frombook;
  2. Import java.net .*;
  3. Import java. AWT .*;
  4. Import java. AWT. event .*;
  5. Import javax. Swing .*;
  6. Public class receive extends jframe implements runnable, actionlistener {
  7. Private Static final long serialversionuid = 1l;
  8. Int port; // multicast Port
  9. Inetaddress group = NULL; // the address of the multicast group
  10. Multicastsocket socket = NULL; // multicast socket
  11. Jbutton startreceive, stopreceive;
  12. Jtextarea showarea;
  13. Thread thread; // The thread responsible for receiving information
  14. Boolean stop = false;
  15. Public receive (){
  16. Super ("timed receiving information ");
  17. Thread = new thread (this );
  18. Startreceive = new jbutton ("start receiving ");
  19. Stopreceive = new jbutton ("stop receiving ");
  20. Startreceive. addactionlistener (this );
  21. Stopreceive. addactionlistener (this );
  22. Showarea = new jtextarea (10, 10 );
  23. Jpanel North = new jpanel ();
  24. North. Add (startreceive );
  25. North. Add (stopreceive );
  26. Container con = getcontentpane ();
  27. Con. Add (North, borderlayout. North );
  28. Con. Add (New jscrollpane (showarea), borderlayout. center );
  29. Port = 5858;
  30. Try {
  31. // Initialize in one breath
  32. Group = inetaddress. getbyname ("239.20.8.0 ");
  33. Socket = new multicastsocket (port );
  34. Socket. joingroup (group );
  35. } Catch (exception e ){
  36. }
  37. Setdefaclocloseoperation (jframe. exit_on_close );
  38. Setsize (320,300 );
  39. Validate ();
  40. Setvisible (true );
  41. }
  42. Public void actionreceivmed (actionevent e ){
  43. If (E. getsource () = startreceive ){
  44. If (! (Thread. isalive ())){
  45. Thread = new thread (this );
  46. Stop = false;
  47. }
  48. Try {
  49. Thread. Start ();
  50. } Catch (exception ee ){
  51. }
  52. }
  53. If (E. getsource () = stopreceive ){
  54. Stop = true;
  55. }
  56. }
  57. Public void run (){
  58. While (true ){
  59. Byte data [] = new byte [8192];
  60. Required rampacket packet = NULL;
  61. Packet = new datagrampacket (data, data. length, group, Port );
  62. Try {
  63. Socket. Receive (packet );
  64. String message = new string (packet. getdata (), 0, packet
  65. . Getlength ());
  66. Showarea. append ("/N" + message );
  67. Showarea. setcaretposition (showarea. gettext (). Length ());
  68. } Catch (exception e ){
  69. }
  70. If (stop = true)
  71. Break;
  72. }
  73. }
  74. Public static void main (string ARGs []) {
  75. New receive ();
  76. }
  77. }

The idea is clear and easy to understand.

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.