集册 Java实例教程 使用DatagramSocket创建多播发送器

使用DatagramSocket创建多播发送器

欢马劈雪     最近更新时间:2020-01-02 10:19:05

414
使用DatagramSocket创建多播发件人

import java.io.*;

import java.net.*;
/*来自 
 时代Java公众号 - nowjava.com*/


public class MulticastSender {

  public static void main(String[] args) {

    byte[] outBuf;

    final int PORT = 8888;


    try {

      DatagramSocket socket = new DatagramSocket();

      long counter = 0;

      String msg;
      /**来自 
       nowjava - 时代Java**/


      while (true) {

        msg = "This is multicast! " + counter;

        counter++;

        outBuf = msg.getBytes();


        //Send to multicast IP address and port

        InetAddress address = InetAddress.getByName("224.2.2.3");

        DatagramPacket outPacket = new DatagramPacket(outBuf, outBuf.length, address, PORT);


        socket.send(outPacket);


        System.out.println("Server sends : " + msg);

        try {

   
展开阅读全文