本文整理汇总了Java中java.net.MulticastSocket.setNetworkInterface方法的典型用法代码示例。如果您正苦于以下问题:Java MulticastSocket.setNetworkInterface方法的具体用法?Java MulticastSocket.setNetworkInterface怎么用?Java MulticastSocket.setNetworkInterface使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类java.net.MulticastSocket
的用法示例。
在下文中一共展示了MulticastSocket.setNetworkInterface方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: main
import java.net.MulticastSocket; //导入方法依赖的package包/类
public static void main(String[] args) throws Exception {
boolean passed = true;
try {
MulticastSocket ms = new MulticastSocket();
Enumeration<NetworkInterface> networkInterfaces = NetworkInterface
.getNetworkInterfaces();
while (networkInterfaces.hasMoreElements()) {
NetworkInterface netIf = networkInterfaces.nextElement();
if (isNetworkInterfaceTestable(netIf)) {
printNetIfDetails(netIf);
ms.setNetworkInterface(netIf);
NetworkInterface msNetIf = ms.getNetworkInterface();
if (netIf.equals(msNetIf)) {
System.out.println(" OK");
} else {
System.out.println("FAILED!!!");
printNetIfDetails(msNetIf);
passed = false;
}
System.out.println("------------------");
}
}
} catch (IOException e) {
e.printStackTrace();
passed = false;
}
if (!passed) {
throw new RuntimeException("Test Fail");
}
System.out.println("Test passed ");
}
示例2: createSockets
import java.net.MulticastSocket; //导入方法依赖的package包/类
/**
* Creates an array of sockets with TTL and network interface set.
*
* @param mcastTTL multicast TTL.
* @return an array of multicast sockets to broadcast on.
* @throws IOException if I/O error occurred while creating a multicast socket.
* @noinspection SocketOpenedButNotSafelyClosed, ConstantConditions
*/
private static MulticastSocket[] createSockets(final int mcastTTL) throws IOException {
Exception lastException = null; // Records last error in case we could not create any sockets
final List<MulticastSocket> socketList = new ArrayList<MulticastSocket>(11);
final Enumeration<NetworkInterface> enumeration = NetworkInterface.getNetworkInterfaces();
while (enumeration.hasMoreElements()) {
try {
final NetworkInterface netIf = enumeration.nextElement();
if (netIf.supportsMulticast()) {
final MulticastSocket socket = new MulticastSocket(); // NOPMD
socket.setTimeToLive(mcastTTL);
socket.setNetworkInterface(netIf);
socket.setSendBufferSize(SEND_BUFFER_SIZE);
socketList.add(socket);
}
} catch (final Exception e) {
lastException = e;
ExceptionUtils.ignoreException(e, "continue to connect to those we can");
}
}
if (socketList.isEmpty()) {
throw new IOException("Could not create at least one multicast socket. Last error: " + lastException);
}
return socketList.toArray(new MulticastSocket[socketList.size()]);
}
示例3: start
import java.net.MulticastSocket; //导入方法依赖的package包/类
/**
* start the discovery agent
*
* @throws Exception
*/
public void start() throws Exception {
if (started.compareAndSet(false, true)) {
if (group == null || group.length() == 0) {
throw new IOException("You must specify a group to discover");
}
String type = getType();
if (!type.endsWith(".")) {
LOG.warn("The type '" + type + "' should end with '.' to be a valid Discovery type");
type += ".";
}
if (discoveryURI == null) {
discoveryURI = new URI(DEFAULT_DISCOVERY_URI_STRING);
}
if (LOG.isTraceEnabled())
LOG.trace("start - discoveryURI = " + discoveryURI);
String myHost = discoveryURI.getHost();
int myPort = discoveryURI.getPort();
if( DEFAULT_HOST_STR.equals(myHost) )
myHost = DEFAULT_HOST_IP;
if(myPort < 0 )
myPort = DEFAULT_PORT;
if (LOG.isTraceEnabled()) {
LOG.trace("start - myHost = " + myHost);
LOG.trace("start - myPort = " + myPort);
LOG.trace("start - group = " + group );
LOG.trace("start - interface = " + mcInterface );
LOG.trace("start - network interface = " + mcNetworkInterface );
LOG.trace("start - join network interface = " + mcJoinNetworkInterface );
}
this.inetAddress = InetAddress.getByName(myHost);
this.sockAddress = new InetSocketAddress(this.inetAddress, myPort);
mcast = new MulticastSocket(myPort);
mcast.setLoopbackMode(loopBackMode);
mcast.setTimeToLive(getTimeToLive());
if (mcJoinNetworkInterface != null) {
mcast.joinGroup(sockAddress, NetworkInterface.getByName(mcJoinNetworkInterface));
}
else {
mcast.joinGroup(inetAddress);
}
mcast.setSoTimeout((int)keepAliveInterval);
if (mcInterface != null) {
mcast.setInterface(InetAddress.getByName(mcInterface));
}
if (mcNetworkInterface != null) {
mcast.setNetworkInterface(NetworkInterface.getByName(mcNetworkInterface));
}
runner = new Thread(this);
runner.setName(this.toString() + ":" + runner.getName());
runner.setDaemon(true);
runner.start();
doAdvertizeSelf();
}
}