在多播Java中不使用VirtualBox作为网络接口?
我在java中使用多播时遇到了一个问题。我正在尝试使用一个简单的发送方-接收方程序来测试多播,该程序应该在同一子网中的两台计算机上的Windows上运行(请参阅后面的代码)。问题是它使用了我的一个VirtualBox接口作为在多播Java中不使用VirtualBox作为网络接口?,java,networking,multicast,Java,Networking,Multicast,我在java中使用多播时遇到了一个问题。我正在尝试使用一个简单的发送方-接收方程序来测试多播,该程序应该在同一子网中的两台计算机上的Windows上运行(请参阅后面的代码)。问题是它使用了我的一个VirtualBox接口作为网络接口,而不是我的无线网卡,因此无法通过两台计算机发送数据(我尝试过)。如果我手动设置要使用的接口(请参阅代码中的注释),它可以在同一子网中的两台计算机上完美工作(这是我的目标)。 我的问题是:是否有任何解决方法可以在默认情况下不选择VirtualBox接口,或者迭代所有接
网络接口
,而不是我的无线网卡,因此无法通过两台计算机发送数据(我尝试过)。如果我手动设置要使用的接口(请参阅代码中的注释),它可以在同一子网中的两台计算机上完美工作(这是我的目标)。
我的问题是:是否有任何解决方法可以在默认情况下不选择VirtualBox接口,或者迭代所有接口(知道VB接口与我的无线网卡具有完全相同的属性…甚至isVirtual
returnfalse
)
这是发件人:
public class MulticastSender extends Thread {
final static String INET_ADDR = "239.192.0.1";
MulticastSocket serverSocket;
public void run() {
// Get the address that we are going to connect to.
InetAddress addr = null;
try {
addr = InetAddress.getByName(INET_ADDR);
} catch (UnknownHostException e) {
e.printStackTrace();
}
// Open a new MulticastSocket, which will be used to send the data.
try {
serverSocket = new MulticastSocket();
// if I set it manually it works (here goes the IP of my PC n°1)
//serverSocket.setInterface(InetAddress.getByName("IP of my first PC"));
serverSocket.joinGroup(addr);
for (int i = 0; i < 100; i++) {
String msg = "Sent message no " + i;
// Create a packet that will contain the data
// (in the form of bytes) and send it.
DatagramPacket msgPacket = new DatagramPacket(msg.getBytes(),
msg.getBytes().length, addr, 8080);
serverSocket.send(msgPacket);
System.out.println("Server sent packet with msg: " + msg);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
serverSocket.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
public class MulticastReceiver implements Runnable {
final static String INET_ADDR = "239.192.0.1";
public void run() {
// Get the address that we are going to connect to.
InetAddress address = null;
try {
address = InetAddress.getByName(INET_ADDR);
MulticastSocket clientSocket = new MulticastSocket(8080);
// if I set it manually it works (here goes the IP of my PC n°2)
//clientSocket.setInterface(InetAddress.getByName("IP of my second PC"));
// Create a buffer of bytes, which will be used to store
// the incoming bytes containing the information from the server.
// Since the message is small here, 256 bytes should be enough.
byte[] buf = new byte[256];
//Join the Multicast group.
clientSocket.joinGroup(address);
while (true) {
// Receive the information and print it.
DatagramPacket msgPacket = new DatagramPacket(buf, buf.length);
clientSocket.receive(msgPacket);
String msg = new String(buf, 0, buf.length);
System.out.println("Received msg: " + msg);
}
} catch (IOException ex) {
ex.printStackTrace();
}
}
}