- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我们的老师让我们修改 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
Exception in thread "main" java.lang.IllegalArgumentException: Not a multicast address
at MulticastImageReceiver.main(MulticastImageReceiver.java:35)
if (!multicastAddress.isMulticastAddress())
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();
}
}
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/
我是一名优秀的程序员,十分优秀!