簡單易懂的組播(課本)

來源:互聯網
上載者:User

1,不斷重複廣播一則新聞。

Code:
  1. package MulSocket.fromBook;   
  2.   
  3. import java.net.*;   
  4.   
  5. public class BroadCast extends Thread {   
  6.     String s = "今天天氣好晴朗,處處好風光!";   
  7.     int port = 5858; // 組播的連接埠   
  8.     InetAddress group = null; // 組播組   
  9.     MulticastSocket socket = null; // 多點廣播通訊端   
  10.   
  11.     BroadCast() {   
  12.         try {   
  13.             group = InetAddress.getByName("239.255.8.0"); // 設定組播組為239.255.8.0   
  14.             socket = new MulticastSocket(port); // 多點廣播通訊端將在port連接埠廣播   
  15.             socket.setTimeToLive(0); // 多點廣播通訊端發送資料報範圍為本網   
  16.             socket.joinGroup(group);   
  17.             // 加入組播組,加入group後,socket發送的資料報可以被加入到group中的成員接收到   
  18.         } catch (Exception e) {   
  19.         }   
  20.     }   
  21.   
  22.     public void run() {   
  23.         while (true) {   
  24.             try {   
  25.                 DatagramPacket packet = null; // 待廣播的資料報   
  26.                 byte data[] = s.getBytes();   
  27.                 packet = new DatagramPacket(data, data.length, group, port);   
  28.                 System.out.println(new String(data));   
  29.                 socket.send(packet); // 廣播資料報   
  30.                 sleep(2000);   
  31.             } catch (Exception e) {   
  32.                 System.out.println(e.toString());   
  33.                 break;   
  34.             }   
  35.         }   
  36.     }   
  37.   
  38.     public static void main(String args[]) {   
  39.         new BroadCast().start();   
  40.     }   
  41. }   

2加入組播組,接收廣播資料

Code:
  1. package MulSocket.fromBook;   
  2.   
  3. import java.net.*;   
  4. import java.awt.*;   
  5. import java.awt.event.*;   
  6. import javax.swing.*;   
  7.   
  8. public class Receive extends JFrame implements Runnable, ActionListener {   
  9.     private static final long serialVersionUID = 1L;   
  10.     int port; // 組播的連接埠   
  11.     InetAddress group = null; // 組播組的地址   
  12.     MulticastSocket socket = null; // 多點廣播通訊端   
  13.        
  14.     JButton startReceive, stopReceive;   
  15.     JTextArea showArea;   
  16.     Thread thread; // 負責接收資訊的線程   
  17.     boolean stop = false;   
  18.   
  19.     public Receive() {   
  20.         super("定時接收資訊");   
  21.         thread = new Thread(this);   
  22.         startReceive = new JButton("開始接收");   
  23.         stopReceive = new JButton("停止接收");   
  24.         startReceive.addActionListener(this);   
  25.         stopReceive.addActionListener(this);   
  26.         showArea = new JTextArea(10, 10);   
  27.         JPanel north = new JPanel();   
  28.         north.add(startReceive);   
  29.         north.add(stopReceive);   
  30.         Container con = getContentPane();   
  31.         con.add(north, BorderLayout.NORTH);   
  32.         con.add(new JScrollPane(showArea), BorderLayout.CENTER);   
  33.         port = 5858;   
  34.         try {   
  35.             //初始化 一氣呵成   
  36.             group = InetAddress.getByName("239.255.8.0");   
  37.             socket = new MulticastSocket(port);   
  38.             socket.joinGroup(group);   
  39.         } catch (Exception e) {   
  40.         }   
  41.         setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);   
  42.         setSize(320, 300);   
  43.         validate();   
  44.         setVisible(true);   
  45.     }   
  46.   
  47.     public void actionPerformed(ActionEvent e) {   
  48.         if (e.getSource() == startReceive) {   
  49.             if (!(thread.isAlive())) {   
  50.                 thread = new Thread(this);   
  51.                 stop = false;   
  52.             }   
  53.             try {   
  54.                 thread.start();   
  55.             } catch (Exception ee) {   
  56.             }   
  57.         }   
  58.         if (e.getSource() == stopReceive) {   
  59.             stop = true;   
  60.         }   
  61.     }   
  62.   
  63.     public void run() {   
  64.         while (true) {   
  65.             byte data[] = new byte[8192];   
  66.             DatagramPacket packet = null;   
  67.             packet = new DatagramPacket(data, data.length, group, port);   
  68.             try {   
  69.                 socket.receive(packet);   
  70.                 String message = new String(packet.getData(), 0, packet   
  71.                         .getLength());   
  72.                 showArea.append("/n" + message);   
  73.                 showArea.setCaretPosition(showArea.getText().length());   
  74.             } catch (Exception e) {   
  75.             }   
  76.             if (stop == true)   
  77.                 break;   
  78.         }   
  79.     }   
  80.   
  81.     public static void main(String args[]) {   
  82.         new Receive();   
  83.     }   
  84. }   

思路明確簡單易懂。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.