java启动绑定网卡_JAVA设置/从多个(UDP)中选择特定的网卡

这篇博客介绍了一种在Java中通过MulticastSocket绑定特定网络接口的方法,以确保UDP广播在正确接口上工作。主要代码展示了如何查找以169.254.*.*开头的IP地址的网络接口,并使用它来创建和绑定MulticastSocket,从而解决多网卡环境下UDP通信的问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

这里是我以前大多是完整的代码。就我而言,我知道我的连接使用的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...

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值