扫一扫
分享文章到微信
扫一扫
关注官方公众号
至顶头条
在本页阅读全文(共4页)
Listing 7: MCServer.java // MCServer.java import java.io.*; import java.net.*; class MCServer { public static void main (String[] args) throws IOException { System.out.println ("Server starting...\n"); // Create a MulticastSocket not bound to any port. MulticastSocket s = new MulticastSocket (); // Because MulticastSocket subclasses DatagramSocket, it is // legal to replace MulticastSocket s = new MulticastSocket (); // with the following line. // DatagramSocket s = new DatagramSocket (); // Obtain an InetAddress object that contains the multicast // group address 231.0.0.1. The InetAddress object is used by // DatagramPacket. InetAddress group = InetAddress.getByName ("231.0.0.1"); // Create a DatagramPacket object that encapsulates a reference // to a byte array (later) and destination address // information. The destination address consists of the // multicast group address (as stored in the InetAddress object) // and port number 10000 -- the port to which multicast datagram // packets are sent. (Note: The dummy array is used to prevent a // NullPointerException object being thrown from the // DatagramPacket constructor.) byte [] dummy = new byte [0]; DatagramPacket dgp = new DatagramPacket (dummy, 0, group, 10000); // Send 30000 Strings to the port. for (int i = 0; i < 30000; i++) { // Create an array of bytes from a String. The platform's // default character set is used to convert from Unicode // characters to bytes. byte [] buffer = ("Video line " + i).getBytes (); // Establish the byte array as the datagram packet's // buffer. dgp.setData (buffer); // Establish the byte array's length as the length of the // datagram packet's buffer. dgp.setLength (buffer.length); // Send the datagram to all members of the multicast group // that listen on port 10000. s.send (dgp); } // Close the socket. s.close (); } } |
如果您非常迫切的想了解IT领域最新产品与技术信息,那么订阅至顶网技术邮件将是您的最佳途径之一。
现场直击|2021世界人工智能大会
直击5G创新地带,就在2021MWC上海
5G已至 转型当时——服务提供商如何把握转型的绝佳时机
寻找自己的Flag
华为开发者大会2020(Cloud)- 科技行者