这里是我以前大多是完整的代码。就我而言,我知道我的连接使用的IP地址前缀。您可能需要查找接口的名称,并将其与您存储在配置文件中的值进行比较。
请注意使用MulticastSocket而不是DatagramSocket,因此setNetworkInterface方法可用于绑定所需的接口。由于MulticastSocket是DatagramSocket的子类,交换机通常不会造成任何问题。
@Override
public void connect() throws InterruptedException
{
NetworkInterface iFace;
iFace = findNetworkInterface();
connectControlBoard(iFace);
connectUTBoards(iFace);
}//end of Capulin1::connect
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
// Capulin1:findNetworkInterface
//
// Finds the network interface for communication with the remotes. Returns
// null if no suitable interface found.
//
// The first interface which is connected and has an IP address beginning with
// 169.254.*.* is returned.
//
// NOTE: If more than one interface is connected and has a 169.254.*.*
// IP address, the first one in the list will be returned. Will need to add
// code to further differentiate the interfaces if such a set up is to be
// used. Internet connections will typically not have such an IP address, so
// a second interface connected to the Internet will not cause a problem with
// the existing code.
//
// If a network interface is not specified for the connection, Java will
// choose the first one it finds. The TCP/IP protocol seems to work even if
// the wrong interface is chosen. However, the UDP broadcasts for wake up calls
// will not work unless the socket is bound to the appropriate interface.
//
// If multiple interface adapters are present, enabled, and running (such as
// an Internet connection), it can cause the UDP broadcasts to fail.
//
public NetworkInterface findNetworkInterface()
{
logger.logMessage("");
NetworkInterface iFace = null;
try{
logger.logMessage("Full list of Network Interfaces:" + "\n");
for (Enumeration en =
NetworkInterface.getNetworkInterfaces(); en.hasMoreElements();) {
NetworkInterface intf = en.nextElement();
logger.logMessage(" " + intf.getName() + " " +
intf.getDisplayName() + "\n");
for (Enumeration enumIpAddr =
intf.getInetAddresses(); enumIpAddr.hasMoreElements();) {
String ipAddr = enumIpAddr.nextElement().toString();
logger.logMessage(" " + ipAddr + "\n");
if(ipAddr.startsWith("/169.254")){
iFace = intf;
logger.logMessage("==>> Binding to this adapter...\n");
}
}
}
}
catch (SocketException e) {
logger.logMessage(" (error retrieving network interface list)" + "\n");
}
return(iFace);
}//end of Capulin1::findNetworkInterface
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
// Capulin1::connectControlBoards
//
public void connectControlBoard(NetworkInterface pNetworkInterface)
throws InterruptedException
{
logger.logMessage("Broadcasting greeting to all Control boards...\n");
MulticastSocket socket;
try{
socket = new MulticastSocket(4445);
if (pNetworkInterface != null) {
try{
socket.setNetworkInterface(pNetworkInterface);
}catch (IOException e) {}//let system bind to default interface
}
}
catch (IOException e) {
logSevere(e.getMessage() + " - Error: 204");
logger.logMessage("Couldn't create Control broadcast socket.\n");
return;
}
...use the socket here...