0

我们的老师让我们修改 MulticastImageSender (MIS) 和 MulticastImageReceiver (MIR) 类中的两个程序。唯一的问题是,我似乎无法弄清楚如何让它们像他给我们时那样正常工作。我一直被告知我的地址不是多播地址。

MIS 的参数:java -jar MIS.jar 127.0.0.0 1234 4 "/home/kevin/Pictures/ME.png" MIR 的参数:java -jar MIR.jar 127.0.0.0 1234

然后我先运行接收器,但我不断得到这些输出

MIS 的输出:

Sent /home/kevin/Pictures/ME.png to 127.0.0.0 on port 1234

MIR 的输出:

Exception in thread "main" java.lang.IllegalArgumentException: Not a multicast address
at MulticastImageReceiver.main(MulticastImageReceiver.java:35)

现在我知道多播地址是 244.xxx 并保留了一些地址,但我想我需要使用本地机器地址将其发送给自己。或者通过使用 244.xxx 地址创建我的多播套接字,我的路由器(计算机网络接口卡?)知道我的地址是 127.0.0.0,并且发送者使用相同的路由器(NIC?)创建多播套接字知道发送它又给我了?

使用 Ubuntu Oneric、Eclipse IDE、Java

资源:

MIS

public class MulticastImageSender 
{
private static final int TIMEOUT = 3000; // Time between sends (milliseconds)
private static final int MAXFILELEN = 65000; // File must fit in single datagram

public static void main(String[] args) throws IOException, InterruptedException 
{
    if (args.length < 4) // Test for correct # of args
        throw new IllegalArgumentException("Parameter(s): <Multicast Address> <Port> <TTL> <Image File> [<Image File>...]");

    InetAddress multicastAddress = InetAddress.getByName(args[0]);
    int destPort = Integer.parseInt(args[1]);
    // Destination port of multicast packets
    int TTL = Integer.parseInt(args[2]);
    // Create a UDP multicast socket with any available local port
    MulticastSocket socket = new MulticastSocket();
    socket.setTimeToLive(TTL);
    // Set the TTL
    for (int i=3; i < args.length; i++)
    {
        RandomAccessFile file = new RandomAccessFile(args[i], "r");
        if (file.length() > MAXFILELEN)
            throw new IOException("File too big");
        byte [] fileBuffer = new byte[(int) file.length()];
        file.read(fileBuffer);
        file.close();
        // Create a datagram to send
        DatagramPacket sendPacket = new DatagramPacket(fileBuffer, fileBuffer.length, multicastAddress, destPort);
        socket.send(sendPacket); // Send the echo string
        System.out.println("Sent " + args[i] + " to " + sendPacket.getAddress().getHostAddress() + " on port " + sendPacket.getPort());
        Thread.sleep(TIMEOUT);
    }
    socket.close();
}
}

MIR 及其支持线程

import java.awt.event.*;
import javax.swing.*;
import java.net.*;
import java.io.*;

public class MulticastImageReceiver extends JFrame 
{

private static final long serialVersionUID = 1L;
private JLabel picture;
// Label to contain image
public MulticastImageReceiver() 
{
    super("Multicast Image Receiver"); // Set the window title
    setSize(300, 300);
    // Set the window size
    picture = new JLabel("No image", SwingConstants.CENTER);
    JScrollPane scrollPane = new JScrollPane(picture);
    getContentPane().add(scrollPane, "Center");
    addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent evt){ System.exit(0);}});
}

public JLabel getPicture() 
{
    return picture;
}

public static void main(String[] args) throws IOException 
{
    if (args.length != 2) // Test for correct # of args
        throw new IllegalArgumentException( "Parameter(s): <Multicast Address> <Port>");

    final InetAddress multicastAddress = InetAddress.getByName(args[0]);
    if (!multicastAddress.isMulticastAddress())
        throw new IllegalArgumentException("Not a multicast address");

    int port = Integer.parseInt(args[1]);
    // Destination port of multicast packets
    MulticastImageReceiver multicastImageReceiver = new MulticastImageReceiver();
    multicastImageReceiver.setVisible(true);
    new Thread(new MulticastImageReceiverThread(multicastImageReceiver, multicastAddress, port,"No Image")).start();
}
}

米尔螺纹

import java.awt.*;
import javax.swing.*;
import java.net.*;
import java.io.*;


class MulticastImageReceiverThread implements Runnable 
{
private static final int MAXFILELEN = 65000;
// File must fit in single datagram
private InetAddress multicastAddress;
// Sender multicast address
private int port;
// Sender port
Runnable updateImage;
// Anonymous class for Swing event queue to update label
String imageText;
// Label text
byte[] image = new byte[MAXFILELEN];
// Bytes of image
boolean imageValid = false;
// True if image contains valid bytes

public MulticastImageReceiverThread(final MulticastImageReceiver frame, InetAddress multicastAddress, int port, String initialImageText) 
{
    this.multicastAddress = multicastAddress;
    this.port = port;
    this.imageText = initialImageText;
    updateImage = new Runnable() 
    {
        public void run() 
        {
            JLabel picture = frame.getPicture();
            picture.setText(imageText);
            if (imageValid) 
            {
                ImageIcon newImage = new ImageIcon(image);
                picture.setIcon(newImage);
                picture.setPreferredSize(new Dimension(newImage.getIconWidth(), newImage.getIconHeight()));
            } 
            else
                picture.setIcon(null);

            picture.revalidate();
        }
    };
}

public void changeImage() 
{
    try 
    {
        SwingUtilities.invokeAndWait(updateImage);
        // Put update in queue and wait until handled
    } 
    catch (Exception e) 
    { 
        System.out.println(e.getMessage());}
}

public void run() 
{
    // Create a datagram to receive
    DatagramPacket recvPacket = new DatagramPacket(image, MAXFILELEN);
    MulticastSocket socket;
    // Create a UDP multicast socket with the specified local port
    try 
    {
        socket = new MulticastSocket(port);
        socket.joinGroup(multicastAddress); // Join the multicast group
    } 
    catch (IOException e) 
    {
        imageText = "Problem with multicast socket";
        imageValid = false; changeImage(); return;
    }
    for (;;) 
    {
        try 
        {
            socket.receive(recvPacket); // Receive the image
        } 
        catch (IOException e) 
        { break; }
        // Assume exception due to file closing
        imageText = "";
        imageValid = true;
        changeImage();
        recvPacket.setLength(MAXFILELEN);
        // You have to reset this!!!
    }
}
}
4

1 回答 1

0

显然,异常消息来自这一行:

if (!multicastAddress.isMulticastAddress())
    throw new IllegalArgumentException("Not a multicast address");

那是因为你给的地址(127.0.0.1)不是多播地址,你可以点击这个链接多播地址找出什么是正确的多播地址。

于 2012-04-18T01:55:22.073 回答