gpt4 book ai didi

java - 通过 Java 进行多播,地址错误

转载 作者:行者123 更新时间:2023-12-04 05:49:50 26 4
gpt4 key购买 nike

我们的老师让我们修改 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.x.x.x 并保留了一些地址,但我想我需要使用本地机器地址将其发送给自己。或者通过使用 244.x.x.x 地址创建我的多播套接字,我的路由器(计算机网络接口(interface)卡?)知道我的地址是 127.0.0.0,并且发送者使用相同的路由器(NIC?)创建多播套接字知道发送它又给我了?

** 我曾经发布过这个问题,但无济于事。我意识到我收到的错误来自这一行
if (!multicastAddress.isMulticastAddress())

我只是在学习所有这些东西,并试图弄清楚如何实际运行这个程序,以便我可以真正开始我的任务。我所需要的只是快速解释要使用的地址和原因。

使用 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!!!
}
}
}

最佳答案

127.0.0.1 不是多播它是本地主机(单播)
多播是旧的 D 类空间。所以从 224.0.0.0 到 239.255.255.255

https://www.rfc-editor.org/rfc/rfc1112了解更多信息
或者
http://www.iana.org/assignments/multicast-addresses/multicast-addresses.xml
因此,在 ADHOC 组中选择一些东西,你应该很高兴。
-edit- 回应评论。
在 IP 多播中,目标地址将其标识为多播。因此,如果您让 MIS 将其发送到源为 127.0.0.1 的多播地址,并且接收器也在同一台计算机上监听该地址,则应该可以正常工作。真的没有什么神奇的,除了一旦你加入一个多播组,你的 TCP 堆栈表明它对该地址的数据包感兴趣并且它被传递。根据本地网络的复杂性/设置,多播数据包将到达第一个启用 IGMP 的交换机或第一个路由器(除非它正在运行多播路由协议(protocol),否则它们将被丢弃/忽略)。
因此,如果您尝试使用实际的多播地址,它不起作用吗?

关于java - 通过 Java 进行多播,地址错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10209200/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com