• Java 网络编程:(十三)案例五:使用 MulticastSocket 实现多点广播


    多点广播

    Datagram只允许数据报发送给指定的目标地址,而MulticastSocket可以将数据报以广播方式发送到数量不等的多个客户端。

    IP协议为多点广播提供了这批特殊的IP地址,这些IP地址的范围是224.0.0.0至239.255.255.255。

      MulticastSocket 常用的方法:

    MulticastSocket(int port) :创建多播套接字并将其绑定到特定端口。创建一个MulticastSocket对象后,还需要将该MulticastSocket加入到指定的多点广播地址,如果结束也需要脱离多点广播地址。
    void joinGroup(InetAddress mcastaddr) :加入多播组。 
    void leaveGroup(InetAddress mcastaddr) :离开多播组。 
    void setLoopbackMode(boolean disable) :启用/禁用多播数据报的本地回送。true 表示禁用 LoopbackMode。
    

      

    案例:群聊

     1 import java.io.IOException;
     2 import java.net.DatagramPacket;
     3 import java.net.InetAddress;
     4 import java.net.MulticastSocket;
     5 import java.util.Scanner;
     6 
     7 public class Chat {
     8     private volatile static boolean exit = false;
     9     private static Scanner input = new Scanner(System.in);
    10     private static String username;
    11     
    12     public static void main(String[] args) throws IOException {
    13         MulticastSocket socket = new MulticastSocket(9999);
    14         InetAddress ip = InetAddress.getByName("230.0.0.1");
    15         socket.joinGroup(ip);
    16         socket.setLoopbackMode(false);
    17         
    18         System.out.print("请输入用户名:");
    19         username = input.nextLine();
    20         
    21         SendThread s = new SendThread(socket,ip);
    22         ReceiveThread r = new ReceiveThread(socket);
    23         
    24         s.start();
    25         r.start();
    26         
    27         try {
    28             s.join();
    29         } catch (InterruptedException e) {
    30             e.printStackTrace();
    31         }
    32         
    33         socket.close();
    34         input.close();
    35     }
    36     static class SendThread extends Thread{
    37         private MulticastSocket socket;
    38         private InetAddress ip;
    39         
    40         public SendThread(MulticastSocket socket,InetAddress ip) {
    41             super();
    42             this.socket = socket;
    43             this.ip = ip;
    44         }
    45         public void run(){
    46             try {
    47                 while (!exit) {
    48                     System.out.print("输入广播消息:");
    49                     String message = input.nextLine();
    50                     if ("bye".equals(message)) {
    51                         exit = true;
    52                         break;
    53                     }
    54     
    55                     byte[] data = (username+":"+message).getBytes();
    56                     DatagramPacket dp = new DatagramPacket(data, data.length, ip, 9999);
    57     
    58                     socket.send(dp);
    59                 }
    60             } catch (IOException e) {
    61                 e.printStackTrace();
    62             }
    63         }
    64         
    65     }
    66     static class ReceiveThread extends Thread{
    67         private MulticastSocket socket;
    68         
    69         public ReceiveThread(MulticastSocket socket) {
    70             super();
    71             this.socket = socket;
    72         }
    73     
    74         public void run(){
    75             try {
    76                 while (!exit) {
    77                     byte[] data = new byte[1024];
    78                     DatagramPacket dp = new DatagramPacket(data, data.length);
    79                     socket.receive(dp);
    80                     String str = new String(data, 0, dp.getLength());
    81                     System.out.println(str);
    82                 }
    83             } catch (IOException e) {
    84                 exit = false;
    85             }
    86         }
    87         
    88     }
    89 }
  • 相关阅读:
    创建大顶堆
    大顶堆的实现
    二叉树的前序,中序,后序,层序实现
    链表实现多项式相加
    225. Implement Stack using Queues
    232. Implement Queue using Stacks
    JavaScript 实现队列操作
    Vue 路由守卫
    回文数 & {}[]() 符号判断

  • 原文地址:https://www.cnblogs.com/niujifei/p/14880178.html
Copyright © 2020-2023  润新知