注意:IP地址224.0.0.1 到 239.255.255.255(包括)均为保留的多点传送组地址。
网络API通过MulticastSocket类和MulticastSocket,以及一些辅助类(比如NetworkInterface) 支持多点传送,当一个客户程序要加入多点传送组时,就创建一个MulticastSocket对象。MulticastSocket(int port) 构造函数允许应用程序指定端口(通过port参数)接收自寻址包,端口必须与服务程序的端口号相匹配,要加入多点传送组,客户程序调用两个 joinGroup()方法中的一个,同样要离开传送组,也要调用两个leaveGroup()方法中的一个。
由于MulticastSocket扩展了DatagramSocket类,一个MulticastSocket对象就有权访问DatagramSocket方法。
List6是MCClient的源代码,这段代码示范了一个客户端加入多点传送组的例子。
Listing 6: MCClient.java
// MCClient.java
import java.io.*;
import java.net.*;
class MCClient
{
public static void main (String [] args) throws IOException
{
// Create a MulticastSocket bound to local port 10000. All
// multicast packets from the server program are received
// on that port.
MulticastSocket s = new MulticastSocket (10000);
// 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");
// Join the multicast group so that datagram packets can be
// received.
s.joinGroup (group);
// Read several datagram packets from the server program.
for (int i = 0; i < 10; i++)
{
// No line will exceed 256 bytes.
byte [] buffer = new byte [256];
// The DatagramPacket object needs no addressing
// information because the socket contains the address.
DatagramPacket dgp = new DatagramPacket (buffer,
buffer.length);
// Receive a datagram packet.
s.receive (dgp);
// Create a second byte array with a length that matches
// the length of the sent data.
byte [] buffer2 = new byte [dgp.getLength ()];
// Copy the sent data to the second byte array.
System.arraycopy (dgp.getData (),
0,
buffer2,
0,
dgp.getLength ());
// Print the contents of the second byte array. (Try
// printing the contents of buffer. You will soon see why
// buffer2 is used.)
System.out.println (new String (buffer2));
}
// Leave the multicast group.
s.leaveGroup (group);
// Close the socket.
s.close ();
}
}
MCClient创建了一个绑定端口号10000的MulticastSocket对象,接下来他获得了一个InetAddress子类对象,该子类 对象包含多点传送组的IP地址231.0.0.0,然后通过joinGroup(InetAddress addr)方法加入多点传送组中,接下来 MCClient接收10个自寻址包,同时输出他们的内容,然后使用leaveGroup(InetAddress addr)方法离开传送组,最后关闭 套接字。
也许你对使用两个字节数组buffer 和 buffer2感到奇怪,当接收到一个自寻址包后,getData()方法返回一个引用,自寻址包 的长度是256个字节,如果要输出所有数据,在输出完实际数据后会有很多空格,这显然是不合理的,所以我们必须去掉这些空格,因此我们创建一个小的字节数 组buffer2,buffer2的实际长度就是数据的实际长度,通过调用DatagramPacket's getLength()方法来得到这个长 度。从buffer 到 buffer2快速复制getLength()的长度的方法是调用System.arraycopy()方法。
List7 MCServer的源代码显示了服务程序是怎样工作的。
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 ();
}
}
MCServer创建了一个MulticastSocket对象,由于他是DatagramPacket对象的一部分,所以他没有绑定端口 号,DatagramPacket有多点传送组的IP地址(231.0.0.0),一旦创建DatagramPacket对象,MCServer就进入一 个发送30000条的文本的循环中,对文本的每一行均要创建一个字节数组,他们的引用均存储在前面创建的DatagramPacket对象中,通过 send()方法,自寻址包发送给所有的组成员。
在编译了MCServer 和 MCClient后,通过输入java MCServer开始运行MCServer,最后再运行一个或多个MCClient。
结论
本文通过研究套接字揭示了Java的网络API的应用方法,我们介绍了套接自的慨念和套接字的组成,以及流套接字和自寻址套接字,以及如何使用 InetAddress, Socket, ServerSocket, DatagramPacket, DatagramSocket和 MulticastSocket类。在完成本文后就可以编写基本的底层通讯程序。
文章来源于领测软件测试网 https://www.ltesting.net/