實驗七 組播編程
實驗目的:
1、理解基於組播編程的基本原理;
2、學會組播編程實例的代碼編寫及理解各語句內容;
3、掌握MulticastSocket類的相關知識;
4、掌握基於組播編程的具體應用。
實驗要求:
1、建立兩個獨立的基於組播編程的JAVA工程,用於服務器和客戶端系統;
2、在工程中建立相關的JAVA類,拷貝代碼後調試運行,使其能正確運行且能實現組播通信;
3、對組播程序的功能進行拓廣,使用於某一具體的應用。
實驗內容:
1、建立服務器端工程和三個JAVA類,類程序的原代碼爲:
//******************************************************************************************************//
public class ServerTest {
public static void main(String[] args) throws java.io.IOException {
new SonServerThreadTest().start();
}
}
//******************************************************************************************************//
import java.io.*;
import java.net.*;
import java.util.*;
class ServerThread
extends Thread {
protected DatagramSocket socket = null;
protected BufferedReader in = null;
protected boolean moreQuotes = true;
public ServerThread() throws IOException {
this("ServerThread");
}
public ServerThread(String name) throws IOException {
super(name);
socket = new DatagramSocket(4445);
try {
in = new BufferedReader(new FileReader("one-liners.txt"));
}
catch (FileNotFoundException e) {
System.err.println("Could not open quote file. Serving time instead.");
}
}
public void run() {
while (moreQuotes) {
try {
byte[] buf = new byte[256];
// receive request
DatagramPacket packet = new DatagramPacket(buf, buf.length);
socket.receive(packet);
// figure out response
String dString = null;
if (in == null) {
dString = new Date().toString();
}
else {
dString = getNextQuote();
}
buf = dString.getBytes();
// send the response to the client at "address" and "port"
InetAddress address = packet.getAddress();
int port = packet.getPort();
packet = new DatagramPacket(buf, buf.length, address, port);
socket.send(packet);
}
catch (IOException e) {
e.printStackTrace();
moreQuotes = false;
}
}
socket.close();
}
protected String getNextQuote() {
String returnValue = null;
try {
if ( (returnValue = in.readLine()) == null) {
in.close();
moreQuotes = false;
returnValue = "No more quotes. Goodbye.";
}
}
catch (IOException e) {
returnValue = "IOException occurred in server.";
}
return returnValue;
}
}
//******************************************************************************************************//
import java.io.*;
import java.net.*;
import java.util.*;
class SonServerThreadTest
extends ServerThread {
private long FIVE_SECONDS = 5000;
public SonServerThreadTest() throws IOException {
super("SonServerThreadTest");
}
public void run() {
while (moreQuotes) {
try {
byte[] buf = new byte[256];
// construct quote
String dString = null;
if (in == null) {
dString = new Date().toString();
}
else {
dString = getNextQuote();
}
buf = dString.getBytes();
// send it
InetAddress group = InetAddress.getByName("10.189.0.88");
DatagramPacket packet = new DatagramPacket(buf, buf.length, group,
4446);
socket.send(packet);
// sleep for a while
try {
sleep( (long) (Math.random() * FIVE_SECONDS));
}
catch (InterruptedException e) {}
}
catch (IOException e) {
e.printStackTrace();
moreQuotes = false;
}
}
socket.close();
}
}
//******************************************************************************************************//
2、建立客戶端工程和JAVA類,類程序的原代碼爲:
import java.io.*;
import java.net.*;
public class ClientTest {
public static void main(String[] args) throws IOException {
MulticastSocket socket = new MulticastSocket(4446);
InetAddress address = InetAddress.getByName("10.189.0.88");
socket.joinGroup(address);
DatagramPacket packet;
for (int i = 0; i < 5; i++) {
byte[] buf = new byte[256];
packet = new DatagramPacket(buf, buf.length);
socket.receive(packet);
String received = new String(packet.getData());
System.out.println("Quote of the Moment: " + received);
}
socket.leaveGroup(address);
socket.close();
}
}
3、調試使以上兩個工程能正常運行,使服務端和客戶端能正常通信(注意:運行時可以先運行多個客戶端,再運行服務器端)。
4、對服務器端和客戶端的程序代碼進行閱讀和理解,要求做到每條語句都能明白其具體含義,每個類都瞭解其功能。
5、對以上程序進行適當的修改,要求數據報通信功能保持實現。
(修改的方向有:組播竊聽器、組內通告等。)
6、撰寫實驗報告
實驗報告包含:實驗報告首頁、實驗步驟(每步的內容,有程序的要求有源碼和運行結果及結果描述)、實驗心得。