多点传送和multicastsocket类
前面的例子显示了服务器程序线程发送单一的消息(通过流套接字或自寻址套接字)给唯一的客户端程序,这种行为被称为单点传送(unicasting),多数情况都不适合于单点传送,比如,摇滚歌手举办一场音乐会将通过互联网进行播放,画面和声音的质量依赖于传输速度,服务器程序要传送大约10亿字节的数据给客户端程序,使用单点传送,那么每个客户程序都要要复制一份数据,如果,互联网上有10000个客户端要收看这个音乐会,那么服务器程序通过internet要传送10000g的数据,这必然导致网络阻塞,降低网络的传输速度。
如果服务器程序要将同一信息发送给多个客户端,那么服务器程序和客户程序可以利用多点传送(multicasting)方式进行通信。多点传送就是服务程序对专用的多点传送组的ip地址和端口发送一系列自寻址数据包,通过加入操作ip地址被多点传送socket注册,通过这个点客户程序可以接收发送给组的自寻址包(同样客户程序也可以给这个组发送自寻址包),一旦客户程序读完所有要读的自寻址数据包,那么可以通过离开组操作离开多点传送组。
注意: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的实际长度就是数据的实际长度,通过调用datagrampackets 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 platforms
// 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 packets
// buffer.
dgp.setdata (buffer);
// establish the byte arrays length as the length of the
// datagram packets 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类。在完成本文后就可以编写基本的底层通讯程序。
