우리 선생님은 MulticastImageSender (MIS)와 MulticastImageReceiver (MIR) 클래스에서 두 가지 프로그램을 수정하도록하고 있습니다. 유일한 문제는 그 프로그램을 제대로 제공 할 수 없다는 것입니다. 나는 내 주소가 멀티 캐스트 주소가 아니라는 말을 계속 듣고있다. MIS에 대한자바를 통해 UDP로 작은 이미지를 멀티 캐스팅합니다.
인수 : MIS.jar의 127.0.0.0 1234 4 "/home/kevin/Pictures/ME.png"MIR에 대한 인수 -jar 자바 : 자바 -jar MIR.jar의 127.0.0.0 1234
내가 먼저 수신기를 실행하지만 이러한 출력 MIS에 대한
출력 점점 계속 : MIR에 대한
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)
이제 멀티 캐스트 주소가 244.x.x.x로 예약되어 있지만, 로컬 컴퓨터 주소를 사용하여 직접 보내야한다는 것을 알고 있습니다. 또는 내 멀티 캐스트 소켓을 244.xxx 주소로 생성하면 라우터 (컴퓨터 네트워크 인터페이스 카드)는 내 주소가 127.0.0.0임을 알고 발신자는 동일한 라우터 (NIC?)를 사용하여 멀티 캐스트 소켓을 만들어 전송한다는 것을 알고 있습니다. 다시 나에게? 우분투 Oneric, 이클립스 IDE를 사용
, 자바
출처 : 물론,
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();
}
}
MIR 스레드
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!!!
}
}
}
내 MIR이 내 멀티 캐스트에 가입 중이므로 보낸 사람의 멀티 캐스트 주소가 필요합니다. 발신자가 멀티 캐스트를 생성하므로 MIS를 시작해야합니다. 그러면 즉시 MIR을 시작하여 244.0.0.x를 입력하면 MIR이 MIS에 가입 할 수 있습니다. –