⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 example1sender.java

📁 JAVA分布式程序学习的课件(全英文)
💻 JAVA
字号:
import java.io.*; 
import java.net.*; 

/**
 * This example illustrates the basic syntax for basic multicast.
 * @author M. L. Liu
 */
public class Example1Sender {

// An application which uses a multicast socket to send
// a single message to a multicast group.
// The message is specified as a command-line argument

   public static void main(String[] args) {
      MulticastSocket s;
      InetAddress group;
      if (args.length != 1)
         System.out.println
            ("This program requires a command line argument");
      else {
         try {      
            // create the multicast socket
            group = InetAddress.getByName("239.1.2.3");
            s = new MulticastSocket(3456);
            s.setTimeToLive(32);   // restrict multicast to processes
                                   // running on hosts at the same site.
            String msg = args[0];
            DatagramPacket packet = 
               new DatagramPacket(msg.getBytes(), msg.length(),
                   group, 3456);
            s.send(packet);
            s.close();
         }
         catch (Exception ex) { // here if an error has occurred
            ex.printStackTrace( );
         } // end catch
      }//end else
   }// end main
}// end class

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -