歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> Java基於UDP的多點廣播數據報技術的一個實現例子

Java基於UDP的多點廣播數據報技術的一個實現例子

日期:2017/3/1 10:35:51   编辑:Linux編程

This is a UDP network program. The following presents a multicast datagram program, which is actually a new technology.

[java]

  1. package com.han;
  2. import java.net.*;
  3. /**
  4. * This is a UDP network program.
  5. * The following presents a multicast datagram program,
  6. * which is actually a new technology.
  7. *
  8. * @author HAN
  9. *
  10. */
  11. public class Weather extends Thread{
  12. String weather ="節目預告: 八點有大型晚會,請收聽";
  13. int port =9898;
  14. InetAddress iaddress;//沒法初始化,這裡只能聲明。因為初始化new對象時要拋出異常所以在成員變量區域是語法通不過的。
  15. MulticastSocket socket;
  16. //在構造方法中初始化成員變量
  17. Weather(){
  18. try {
  19. //A multicast group is specified by a class D IP address
  20. //and by a standard UDP port number.
  21. //Class D IP addresses are in the range 224.0.0.0 to 239.255.255.255, inclusive.
  22. //The address 224.0.0.0 is reserved and should not be used.
  23. iaddress=InetAddress.getByName("233.0.0.0");
  24. socket=new MulticastSocket(port);
  25. socket.setTimeToLive(1);
  26. socket.joinGroup(iaddress);
  27. }catch(Exception e){
  28. e.printStackTrace();
  29. }
  30. }
  31. @Override //最簡單的方法也就是建立一個線程來運行
  32. public void run(){
  33. while(true){
  34. byte[] data=weather.getBytes();
  35. DatagramPacket packet=new DatagramPacket(data,data.length,iaddress,port);
  36. // System.out.println(weather);
  37. System.out.println(new String(data));
  38. try {
  39. socket.send(packet);
  40. sleep(3000);//線程休眠3秒
  41. } catch (Exception e) {
  42. // TODO Auto-generated catch block
  43. e.printStackTrace();
  44. }
  45. }
  46. }
  47. public static void main(String[] args){
  48. Weather w=new Weather();
  49. w.start();
  50. }
  51. }
This is the receive part.

[java]

  1. package com.han;
  2. import java.awt.BorderLayout;
  3. import java.awt.Color;
  4. import java.awt.event.*;
  5. import java.io.IOException;
  6. import java.net.DatagramPacket;
  7. import java.net.InetAddress;
  8. import java.net.MulticastSocket;
  9. import javax.swing.JButton;
  10. import javax.swing.JFrame;
  11. import javax.swing.JPanel;
  12. import javax.swing.JScrollPane;
  13. import javax.swing.JTextArea;
  14. /**
  15. * This is the receive part.
  16. * @author HAN
  17. *
  18. */
  19. public class Receive extends JFrame implements Runnable, ActionListener{
  20. private static final long serialVersionUID = 3362377947503474102L;
  21. int port=9898;
  22. InetAddress group;
  23. MulticastSocket socket; //socket sends and receives the packet.
  24. DatagramPacket packet;
  25. JButton ince=new JButton("開始接收");
  26. JButton stop=new JButton("停止接收");
  27. JTextArea inceAr=new JTextArea(10,20);
  28. JTextArea inced=new JTextArea(10,20);
  29. Thread thread;
  30. boolean b = false;
  31. byte[] buf=new byte[30];// If the message is longer than the packet's length, the message is truncated.
  32. //在構造方法中設置具體參數特性,也就是初始化
  33. public Receive(){
  34. super("廣播數據報");
  35. thread=new Thread(this);
  36. ince.addActionListener(this);
  37. stop.addActionListener(this);
  38. inceAr.setForeground(Color.blue);
  39. JPanel north=new JPanel();
  40. north.add(ince);
  41. north.add(stop);
  42. add(north,BorderLayout.NORTH);
  43. JPanel center=new JPanel();
  44. JScrollPane sp=new JScrollPane(inceAr);
  45. JScrollPane sp2=new JScrollPane(inced);
  46. inceAr.setLineWrap(true);
  47. inceAr.setEditable(false);
  48. inced.setLineWrap(true);
  49. inced.setEditable(false);
  50. center.add(sp);
  51. center.add(sp2);
  52. add(center,BorderLayout.CENTER);
  53. pack(); //JFrame inherited from Window
  54. validate();
  55. setVisible(true);
  56. setDefaultCloseOperation(EXIT_ON_CLOSE);
  57. try {
  58. socket=new MulticastSocket(port);
  59. //A multicast group is specified by a class D IP address
  60. //and by a standard UDP port number.
  61. //Class D IP addresses are in the range 224.0.0.0 to 239.255.255.255, inclusive.
  62. //The address 224.0.0.0 is reserved and should not be used.
  63. group=InetAddress.getByName("233.0.0.0");
  64. socket.joinGroup(group);
  65. packet=new DatagramPacket(buf,buf.length);
  66. } catch (IOException e) {
  67. // TODO Auto-generated catch block
  68. e.printStackTrace();
  69. }
  70. }
  71. @Override
  72. public void run(){
  73. while(true){
  74. try {
  75. socket.receive(packet);
  76. } catch (IOException e) {
  77. // TODO Auto-generated catch block
  78. e.printStackTrace();
  79. }
  80. // String message=new String(buf);
  81. String message=new String(packet.getData(),0,packet.getLength());//very important !!
  82. // System.out.println(message.length());
  83. inceAr.setText("正在接受的內容: \n"+message);
  84. inced.append(message+"\n");
  85. if(b==true){
  86. break;
  87. }
  88. }
  89. }
  90. public void actionPerformed(ActionEvent e){
  91. Object object=e.getSource();
  92. if(object==ince){
  93. //If this thread is already on the state "run", do nothing.
  94. if(!thread.isAlive()){
  95. thread=new Thread(this);
  96. thread.start();
  97. ince.setBackground(Color.red);
  98. stop.setBackground(Color.yellow);
  99. b=false;
  100. }
  101. }
  102. if(object==stop){
  103. ince.setBackground(Color.yellow);
  104. stop.setBackground(Color.red);
  105. b=true;
  106. }
  107. }
  108. public static void main(String[] args){
  109. @SuppressWarnings("unused")
  110. Receive rec=new Receive();
  111. // rec.setSize(460, 200);//提供了額外設置窗體大小的方式
  112. }
  113. }
這上面的程序運用了很多JAVA的核心技術以及很多需要注意的地方,可以堪稱一個JAVA的精髓例子,記錄下來以便以後查閱。
Copyright © Linux教程網 All Rights Reserved