实验七 组播编程

 

实验七 组播编程

实验目的:

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、撰写实验报告

实验报告包含:实验报告首页、实验步骤(每步的内容,有程序的要求有源码和运行结果及结果描述)、实验心得。

 

你可能感兴趣的:(网络通信编程)