林建刚 ·yesky
IP协议是Internet上所有信息的传播手段,UDP(User Datagram Protocol,用户数据报协议)数据报被封装在IP包中,发送到网络上适当的机器。众所周知,大多数IP使用单点发送,即从一个主机发送一个包到另一个主机上。然而,IP协议也具有多点发送的能力,若要使用多点发送时,一个报文标有一组目标主机地址,当报文发出后,整个组都能收到。为支持多点发送,一定范围的IP地址被单独划分出来。这些IP地址是D类地址,其范围是224.0.0.0至239.255.255.255。
IP多点发送(或多点广播)是一种相当新的技术,它是对简单广播的改进。多点广播功能类似于一个单一信息发送到多个接收者的广播,但仅发送给那些等待它的接收者。其思想就是设置一组网络地址作为多点广播地址,其范围是225.0.0.0到239.255.255.255。每一个多点广播地址都被看作一个组。当用户需要从某个地址信息时,加入到该组即可。例如,可能为其股票报价系统设置了地址225.23.32.36作为多点广播地址,如果一个想要接收股票查询报价程序,必须加入到225.23.32.36组中。
一、制作多点发送的基本知识
在使用Java进行多点发送时,它的MulticastSocket类是实现这一功能的关键。MulticastSocket类允许用户发送和接收使用多点发送IP的数据报。若要发送或接收多点广播数据,必须首先创建一个多点广播套接字(多点广播套接字类似于数据报套接字,事实上MulticastSocket是DatagramSocket的一个子类)。若要发送一个数据报时,可使用缺省的端口号创建多点广播套接字,也可以在构造器指定端口号来创建多点广播套接字:
public MulticastSocket()throws IOException
public MulticastSocket(int portNumber)throws IOException
要加入到一个多点广播地址,可使用jionGroup方法;若要脱离一个组,相使用leaveGroup方法:
public void jionGroup(InetAddress multicastAddr)throws IOException
public void leaveGroup(InetAddress multicastAddr)throws IOException
在某些系统中,可能有多重网络接口。这可能会对多点广播带来问题,因为用户需要在一个指定的接口上侦听,通过调用setInterface可选择多点广播套接字所使用的接口:
public void setInterface(InetAddress interface)throws SocketException
通过调用getInterface方法可查询多点广播套接字的接口:
public InetAddress getInterface()throws SocketException
用户在发送多点发送的数据时,使用send方法:
public synchronized void send(DatagramPacket packet, byte timeTolive)throws IOException
其中TTL值指定了数据包应该跨过多少个网络,当TTL为0时,指定数据包应停留在本地主机;当TTL的值为1时,指定数据包发送到本地网络;当TTL的值为32时,意味着只应发送到本站点的网络上;当TTL为64时,意味着数据包应保留在本地区;当TTL的值为128时,意味着数据应保留在本大洲;当TTL为255时,意味着数据包应发送到所有地方;若使用缺省的send方法时,TTL的值为1。
二、制作多点发送应用程序
下面就运用Java制作一个多点发送应用程序。其中程序1(MulticastCastSender.java)是一个发送数据报到一个指定多点发送IP地址的程序,该程序运行时使用两个参数:第一个指定发送数据报的多点发送IP地址,另一个则指定侦听应用程序的UDP端口。其中Main()方法保证了这些参数收到后,实例化一个MultiCastSender对象。另外,构造器使用多点发送IP地址的String对象,创建一个InetAddress实例,然后再在动态分配的端口上为发送数据报创建一个MulticastSocket,然后构造器进入一个while循环,从标准输入上逐入。该程序将每一行的前512个字节包装到一标有地址的DatagramPacket中,并通过MulticastSocket发送该数据报。
其中程序MultiCastSender.java的源代码如下:
import java.net.*; // Import package names used.
import java.io.*;
class MultiCastSender {
private static final byte TTL = 1;
private static final int DATAGRAM_BYTES = 1024;
private int mulcastPort;
private InetAddress mulcastIP;
private BufferedReader input;
private MulticastSocket mulcastSocket;
public static void main(String[] args) {
// This must be the same port and IP address used by the receivers.
if (args.length != 2) {
System.out.print("Usage: MultiCastSender " + " \n\t can be one of 224.x.x.x " + "- 239.x.x.x\n");
System.exit(1);
}
MultiCastSender send = new MultiCastSender(args);
System.exit(0);
}
public MultiCastSender(String[] args) {
DatagramPacket mulcastPacket; // UDP datagram.
String nextLine; // Line from STDIN.
byte[] mulcastBuffer; file:// Buffer for datagram.
byte[] lineData; // The data typed in.
int sendLength; file:// Length of line.
input = new BufferedReader(new InputStreamReader(System.in));
try {
// Create a multicasting socket.
mulcastIP = InetAddress.getByName(args[0]);
mulcastPort = Integer.parseInt(args[1]);
mulcastSocket = new MulticastSocket();
} catch(UnknownHostException excpt) {
System.err.println("Unknown address: " + excpt);
System.exit(1);
} catch(IOException excpt) {
System.err.println("Unable to obtain socket: " + excpt);
System.exit(1);
}
try {
file:// Loop and read lines from standard input.
while ((nextLine = input.readLine()) != null) {
mulcastBuffer = new byte[DATAGRAM_BYTES];
file:// If line is longer than your buffer, use the length of the buffer available.
if (nextLine.length() > mulcastBuffer.length) {
endLength = mulcastBuffer.length;
// Otherwise, use the line's length.
}
else {
sendLength = nextLine.length();
}
// Convert the line of input to bytes.
lineData = nextLine.getBytes();
// Copy the data into the blank byte array
file://which you will use to create the DatagramPacket.
for (int i = 0; i < sendLength; i++) {
mulcastBuffer[i] = lineData[i];
}
mulcastPacket=new DatagramPacket (mulcastBuffer, mulcastBuffer.length, mulcastIP, mulcastPort);
// Send the datagram.
try {
System.out.println("Sending:\t" + nextLine);
mulcastSocket.send(mulcastPacket,TTL);
} catch(IOException excpt) {
System.err.println("Unable to send packet: " + excpt);
}
}
} catch(IOException excpt) {
System.err.println("Failed I/O: " + excpt);
}
mulcastSocket.close(); file:// Close the socket.
}
}
程序2(MultiCastReceiver.java)通过接收多点发送的数据报实现了发送者。该应用程序有两个参数,这两个参数必须对应于IP地址和用来激活MultiCastSender的端口。Main()方法首先检查命令的参数,然后创建一个MultiCastReceiver对象。该对象的构造器用在激活该应用程序的端口上创建一个InetAddress和一个MulticastSocket。在包含于InetAddress的地址加入多点发送者,然后进入一个循环。该对象的构造器从一个套接字接收数据报并打印出包含在数据报中数据,包括发送数据包的计算机和端口。
程序2(MultiCastReceiver)的源代码如下:
import java.net.*; // Import package names used.
import java.io.*;
class MultiCastReceiver {
private static final int DATAGRAM_BYTES = 1024;
private int mulcastPort;
private InetAddress mulcastIP;
private MulticastSocket mulcastSocket;
private boolean keepReceiving = true;
public static void main(String[] args) {
// This must be the same port and IP address used by the sender.
if (args.length != 2) {
System.out.print("Usage: MultiCastReceiver \n\t can be one of " + "224.x.x.x - 239.x.x.x\n");
System.exit(1);
}
multiCastReceiver send = new MultiCastReceiver(args);
System.exit(0);
}
public MultiCastReceiver(String[] args) {
DatagramPacket mulcastPacket; // Packet to receive.
byte[] mulcastBuffer; // byte[] array buffer
InetAddress fromIP; file:// Sender address.
int fromPort; // Sender port.
String mulcastMsg; // String of message.
try {
// First, set up your receiving socket.
mulcastIP = InetAddress.getByName(args[0]);
mulcastPort = Integer.parseInt(args[1]);
mulcastSocket = new MulticastSocket(mulcastPort);
// Join the multicast group.
mulcastSocket.joinGroup(mulcastIP);
} catch(UnknownHostException excpt) {
System.err.println("Unknown address: " + excpt);
System.exit(1);
} catch(IOException excpt) {
System.err.println("Unable to obtain socket: " + excpt);
System.exit(1);
}
while (keepReceiving) {
try {
// Create a new datagram.
mulcastBuffer = new byte[DATAGRAM_BYTES];
mulcastPacket = new DatagramPacket(mulcastBuffer, mulcastBuffer.length);
// Receive the datagram.
mulcastSocket.receive(mulcastPacket);
fromIP = mulcastPacket.getAddress();
fromPort = mulcastPacket.getPort();
mulcastMsg = new String(mulcastPacket.getData());
// Print out the data.
System.out.println("Received from " + fromIP + " on port " + fromPort + ": " + mulcastMsg);
} catch(IOException excpt) {
system.err.println("Failed I/O: " + excpt);
}
}
try {
mulcastSocket.leaveGroup(mulcastIP); file:// Leave the group.
} catch(IOException excpt) {
System.err.println("Socket problem leaving group: " + excpt);
}
mulcastSocket.close(); // Close the socket.
}
public void stop() {
if (keepReceiving) {
keepReceiving = false;
}
}
}
若要运用该应用程序,先编译MultiCastSender和MultiCastReceiver,然后将MultiCastReceiver传送到其它机器,以便能够演示多个参与者接收报文的情况,最后Java解释器运行该应用程序。例如:要向225.2.32.6组在端口1111上发送多点发送报文,应按如下方法进行:
-/class ->java MultiCastSender225.2.32.6 1111
Here is just to test multicast message.
Sending: Here is just to test multicast message.
Do you received it?
Sending: Do you received it?
要接收这些报文,应在一个或多个系统上运行MultiCastReceiver应用程序,这些系统应该加入同一个多点发送组225.2.32.6,并在相同的端口1111上侦听:
-/class ->java MultiCastReceiver 225.2.32.6 1111
Received from 225.106.36.32 on port 32911: Here is just to test multicast message.
Received from 225.106.36.32 on port 32911: Do you received it?
……