`

简单易懂的组播(课本)

    博客分类:
  • java
阅读更多

1,不断重复广播一则新闻。

Code:
  1. packageMulSocket.fromBook;
  2. importjava.net.*;
  3. publicclassBroadCastextendsThread{
  4. Strings="今天天气好晴朗,处处好风光!";
  5. intport=5858;//组播的端口
  6. InetAddressgroup=null;//组播组
  7. MulticastSocketsocket=null;//多点广播套接字
  8. BroadCast(){
  9. try{
  10. group=InetAddress.getByName("239.255.8.0");//设置组播组为239.255.8.0
  11. socket=newMulticastSocket(port);//多点广播套接字将在port端口广播
  12. socket.setTimeToLive(0);//多点广播套接字发送数据报范围为本地网络
  13. socket.joinGroup(group);
  14. //加入组播组,加入group后,socket发送的数据报可以被加入到group中的成员接收到
  15. }catch(Exceptione){
  16. }
  17. }
  18. publicvoidrun(){
  19. while(true){
  20. try{
  21. DatagramPacketpacket=null;//待广播的数据报
  22. bytedata[]=s.getBytes();
  23. packet=newDatagramPacket(data,data.length,group,port);
  24. System.out.println(newString(data));
  25. socket.send(packet);//广播数据报
  26. sleep(2000);
  27. }catch(Exceptione){
  28. System.out.println(e.toString());
  29. break;
  30. }
  31. }
  32. }
  33. publicstaticvoidmain(Stringargs[]){
  34. newBroadCast().start();
  35. }
  36. }

2加入组播组,接收广播数据

Code:
  1. packageMulSocket.fromBook;
  2. importjava.net.*;
  3. importjava.awt.*;
  4. importjava.awt.event.*;
  5. importjavax.swing.*;
  6. publicclassReceiveextendsJFrameimplementsRunnable,ActionListener{
  7. privatestaticfinallongserialVersionUID=1L;
  8. intport;//组播的端口
  9. InetAddressgroup=null;//组播组的地址
  10. MulticastSocketsocket=null;//多点广播套接字
  11. JButtonstartReceive,stopReceive;
  12. JTextAreashowArea;
  13. Threadthread;//负责接收信息的线程
  14. booleanstop=false;
  15. publicReceive(){
  16. super("定时接收信息");
  17. thread=newThread(this);
  18. startReceive=newJButton("开始接收");
  19. stopReceive=newJButton("停止接收");
  20. startReceive.addActionListener(this);
  21. stopReceive.addActionListener(this);
  22. showArea=newJTextArea(10,10);
  23. JPanelnorth=newJPanel();
  24. north.add(startReceive);
  25. north.add(stopReceive);
  26. Containercon=getContentPane();
  27. con.add(north,BorderLayout.NORTH);
  28. con.add(newJScrollPane(showArea),BorderLayout.CENTER);
  29. port=5858;
  30. try{
  31. //初始化一气呵成
  32. group=InetAddress.getByName("239.255.8.0");
  33. socket=newMulticastSocket(port);
  34. socket.joinGroup(group);
  35. }catch(Exceptione){
  36. }
  37. setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  38. setSize(320,300);
  39. validate();
  40. setVisible(true);
  41. }
  42. publicvoidactionPerformed(ActionEvente){
  43. if(e.getSource()==startReceive){
  44. if(!(thread.isAlive())){
  45. thread=newThread(this);
  46. stop=false;
  47. }
  48. try{
  49. thread.start();
  50. }catch(Exceptionee){
  51. }
  52. }
  53. if(e.getSource()==stopReceive){
  54. stop=true;
  55. }
  56. }
  57. publicvoidrun(){
  58. while(true){
  59. bytedata[]=newbyte[8192];
  60. DatagramPacketpacket=null;
  61. packet=newDatagramPacket(data,data.length,group,port);
  62. try{
  63. socket.receive(packet);
  64. Stringmessage=newString(packet.getData(),0,packet
  65. .getLength());
  66. showArea.append("/n"+message);
  67. showArea.setCaretPosition(showArea.getText().length());
  68. }catch(Exceptione){
  69. }
  70. if(stop==true)
  71. break;
  72. }
  73. }
  74. publicstaticvoidmain(Stringargs[]){
  75. newReceive();
  76. }
  77. }

思路明确简单易懂。

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics