我有一个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;
}
答案 0 :(得分:1)
我不知道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 );
}
}
}