我有一个Java类,旨在促进计算机之间的连接,而无需显式定义对等地址。
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.MulticastSocket;
import java.net.SocketException;
public class Broadcasts {
private final Runnable receiver;
private final Runnable sender;
private boolean run = true;
public Broadcasts(TheThing parent) {
receiver = new Runnable() {
public void run() {
byte data[] = new byte[0];
DatagramPacket packet = new DatagramPacket(data, data.length);
try {
DatagramSocket socket = new DatagramSocket();
while (run) {
socket.receive(packet);
parent.newAddress(packet.getAddress());
}
} catch (SocketException ex) {
ex.printStackTrace();
parent.quit();
} catch (IOException ex) {
ex.printStackTrace();
parent.quit();
}
}
};
sender = new Runnable() {
public void run() {
byte data[] = new byte[0];
DatagramPacket packet = new DatagramPacket(data, data.length);
try {
MulticastSocket socket = new MulticastSocket();
socket.setBroadcast(true);
socket.joinGroup(Globals.publicAddress);
while (run) {
socket.send(packet);
Thread.sleep(Globals.UDPINTERVAL);
}
} catch (IOException ex) {
ex.printStackTrace();
parent.quit();
} catch (InterruptedException ex) {
ex.printStackTrace();
parent.quit();
}
}
};
new Thread(receiver).start();
new Thread(sender).start();
}
public void quit() {
run = false;
}
}
我收到以下错误:
java.net.SocketException: Not a multicast address
at java.net.MulticastSocket.joinGroup(MulticastSocket.java:310)
at the.thing.Broadcasts$2.run(Broadcasts.java:42)
at java.lang.Thread.run(Thread.java:745)
我面临的问题是定义为
Globals.publicAddress
的地址是多播地址。我在这里想念什么?Globals.publicAddress
通过以下方法获得。它应该是DHCP分配的地址:private static InetAddress getPublicIface() {
InetAddress result = null;
Enumeration e;
try {
e = NetworkInterface.getNetworkInterfaces();
} catch (SocketException ex) {
return null;
}
while (e.hasMoreElements() && result == null) {
NetworkInterface n = (NetworkInterface) e.nextElement();
Enumeration ee = n.getInetAddresses();
while (ee.hasMoreElements()) {
InetAddress i = (InetAddress) ee.nextElement();;
if (i.getHostAddress().startsWith("192.168.")) {
result = i;
break;
}
}
}
return result;
我还尝试了以下方法来获取有效的广播地址:
private static InetAddress getPublicIface() {
InetAddress result = null;
Enumeration e;
try {
e = NetworkInterface.getNetworkInterfaces();
} catch (SocketException ex) {
return null;
}
while(e.hasMoreElements() && result == null) {
NetworkInterface iface = (NetworkInterface) e.nextElement();
try {
if(iface == null || iface.isLoopback() || !iface.isUp() ||
iface.isVirtual() || !iface.supportsMulticast())
continue;
} catch (SocketException ex) {
continue;
}
Iterator it = iface.getInterfaceAddresses().iterator();
while (it.hasNext()) {
InterfaceAddress address = (InterfaceAddress) it.next();
if(address == null || address.getBroadcast() == null)
continue;
result = address.getBroadcast();
break;
}
}
return result;
}
最佳答案
我不了解NetworkInterface
,所以对我来说这是一件很酷的事情。因此,我四处寻找并整理了一些似乎有效的代码。它需要清理,但是它可以在我的系统上运行,并且似乎可以广播。
重点:
您无需收听广播地址。由于它是广播的,所以无论您监听哪个IP地址,都可以得到它。
您确实必须发送到广播地址,由于某种原因,这似乎仅作为DatagramPacket
的参数起作用,而不是DatagramSocket
。我不知道为什么,但这就是API似乎想要它的方式。
祝好运。
public class BroadcastTest
{
private static final int RANDOM_PORT = 4444;
public static void main( String[] args )
throws Exception
{
InetAddress addr = getBroadcastAddrs().get(0);
System.err.println( addr );
new Thread( new BroadcastServer( RANDOM_PORT ) ).start();
DatagramSocket dsock = new DatagramSocket();
byte[] send = "Hello World".getBytes( "UTF-8" );
DatagramPacket data = new DatagramPacket( send, send.length, addr, RANDOM_PORT );
dsock.send( data );
}
public static List<InetAddress> getBroadcastAddrs() throws SocketException {
Set<InetAddress> set = new LinkedHashSet<>();
Enumeration<NetworkInterface> nicList = NetworkInterface.
getNetworkInterfaces();
for( ; nicList.hasMoreElements(); ) {
NetworkInterface nic = nicList.nextElement();
if( nic.isUp() && !nic.isLoopback() ) {
for( InterfaceAddress ia : nic.getInterfaceAddresses() )
set.add( ia.getBroadcast() );
}
}
return Arrays.asList( set.toArray( new InetAddress[0] ) );
}
}
class BroadcastServer implements Runnable {
private final int port;
public BroadcastServer( int port )
{
this.port = port;
}
@Override
public void run()
{
try {
DatagramSocket dsock = new DatagramSocket( port );
DatagramPacket data = new DatagramPacket( new byte[2048], 2048 );
dsock.receive( data );
System.out.println( new String( data.getData(), "UTF-8" ) );
} catch( SocketException ex ) {
Logger.getLogger( BroadcastServer.class.getName() ).
log( Level.SEVERE, null, ex );
} catch( IOException ex ) {
Logger.getLogger( BroadcastServer.class.getName() ).
log( Level.SEVERE, null, ex );
}
}
}
关于java - UDP广播和接收,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/30417879/