本文整理匯總了Java中java.net.InterfaceAddress類的典型用法代碼示例。如果您正苦於以下問題:Java InterfaceAddress類的具體用法?Java InterfaceAddress怎麽用?Java InterfaceAddress使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。
InterfaceAddress類屬於java.net包,在下文中一共展示了InterfaceAddress類的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: updateIpAddress
import java.net.InterfaceAddress; //導入依賴的package包/類
private void updateIpAddress() {
try {
Enumeration<NetworkInterface> b = NetworkInterface.getNetworkInterfaces();
ipAddress = null;
while( b.hasMoreElements()){
NetworkInterface iface = b.nextElement();
if (iface.getName().startsWith("dock")) {
continue;
}
for ( InterfaceAddress f : iface.getInterfaceAddresses()) {
if (f.getAddress().isSiteLocalAddress()) {
ipAddress = f.getAddress().getHostAddress();
}
}
}
} catch (SocketException e) {
e.printStackTrace();
}
}
示例2: main
import java.net.InterfaceAddress; //導入依賴的package包/類
public static void main(String[] args) throws Exception {
Enumeration<NetworkInterface> nics = NetworkInterface.getNetworkInterfaces();
while (nics.hasMoreElements()) {
NetworkInterface nic = nics.nextElement();
for (InterfaceAddress iaddr : nic.getInterfaceAddresses()) {
boolean valid = checkPrefix(iaddr);
if (!valid) {
passed = false;
debug(nic.getName(), iaddr);
}
InetAddress ia = iaddr.getAddress();
if (ia.isLoopbackAddress() && ia instanceof Inet4Address) {
// assumption: prefix length will always be 8
if (iaddr.getNetworkPrefixLength() != 8) {
out.println("Expected prefix of 8, got " + iaddr);
passed = false;
}
}
}
}
if (!passed)
throw new RuntimeException("Failed: some interfaces have invalid prefix lengths");
}
示例3: getBroadcastAddress
import java.net.InterfaceAddress; //導入依賴的package包/類
static List<String> getBroadcastAddress() throws BrowsingException, SocketException {
Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces();
List<String> broadcastAddresses = new ArrayList<>();
while (interfaces.hasMoreElements()) {
NetworkInterface networkInterface = interfaces.nextElement();
if (networkInterface.isLoopback()) {
continue;
}
for (InterfaceAddress interfaceAddress :
networkInterface.getInterfaceAddresses()) {
InetAddress broadcast = interfaceAddress.getBroadcast();
if (broadcast != null) {
broadcastAddresses.add(broadcast.toString().substring(1));
}
}
}
return broadcastAddresses;
}
示例4: getBroadcast
import java.net.InterfaceAddress; //導入依賴的package包/類
public static InetAddress getBroadcast(InetAddress myIpAddress) {
NetworkInterface temp;
InetAddress iAddr = null;
try {
temp = NetworkInterface.getByInetAddress(myIpAddress);
List<InterfaceAddress> addresses = temp.getInterfaceAddresses();
for (InterfaceAddress inetAddress : addresses) {
iAddr = inetAddress.getBroadcast();
}
System.out.println("iAddr=" + iAddr);
return iAddr;
} catch (SocketException e) {
e.printStackTrace();
System.out.println("getBroadcast" + e.getMessage());
}
return null;
}
示例5: getLocalCidrs
import java.net.InterfaceAddress; //導入依賴的package包/類
public static String[] getLocalCidrs() {
final String defaultHostIp = getDefaultHostIp();
final List<String> cidrList = new ArrayList<>();
try {
for (final NetworkInterface ifc : IteratorUtil.enumerationAsIterable(NetworkInterface.getNetworkInterfaces())) {
if (ifc.isUp() && !ifc.isVirtual() && !ifc.isLoopback()) {
for (final InterfaceAddress address : ifc.getInterfaceAddresses()) {
final InetAddress addr = address.getAddress();
final int prefixLength = address.getNetworkPrefixLength();
if (prefixLength < MAX_CIDR && prefixLength > 0) {
final String ip = addr.getHostAddress();
if (ip.equalsIgnoreCase(defaultHostIp)) {
cidrList.add(ipAndNetMaskToCidr(ip, getCidrNetmask(prefixLength)));
}
}
}
}
}
} catch (final SocketException e) {
s_logger.warn("UnknownHostException in getLocalCidrs().", e);
}
return cidrList.toArray(new String[0]);
}
示例6: getPrivateInetInetAddress
import java.net.InterfaceAddress; //導入依賴的package包/類
public static final List<InetAddress> getPrivateInetInetAddress() throws SocketException {
List<InetAddress> inetAddresses = new LinkedList<>();
for (Enumeration<NetworkInterface> e = NetworkInterface.getNetworkInterfaces(); e.hasMoreElements();) {
NetworkInterface netInterface = e.nextElement();
if (netInterface.isLoopback() || netInterface.isVirtual() || !netInterface.isUp()) {
continue;
}
for (InterfaceAddress address : netInterface.getInterfaceAddresses()) {
InetAddress inetAddress = address.getAddress();
if (inetAddress.isSiteLocalAddress() && !inetAddress.isLoopbackAddress()
&& (inetAddress instanceof Inet4Address)) {
inetAddresses.add(inetAddress);
}
}
}
return inetAddresses;
}
示例7: getPublicInetInetAddress
import java.net.InterfaceAddress; //導入依賴的package包/類
public static final List<InetAddress> getPublicInetInetAddress() throws SocketException {
List<InetAddress> inetAddresses = new LinkedList<>();
for (Enumeration<NetworkInterface> e = NetworkInterface.getNetworkInterfaces(); e.hasMoreElements();) {
NetworkInterface netInterface = e.nextElement();
if (netInterface.isLoopback() || netInterface.isVirtual() || !netInterface.isUp()) {
continue;
}
for (InterfaceAddress address : netInterface.getInterfaceAddresses()) {
InetAddress inetAddress = address.getAddress();
if (!inetAddress.isLoopbackAddress() && !inetAddress.isLinkLocalAddress()
&& !inetAddress.isSiteLocalAddress() && (inetAddress instanceof Inet4Address)) {
inetAddresses.add(inetAddress);
}
}
}
return inetAddresses;
}
示例8: getDataNodeAddress
import java.net.InterfaceAddress; //導入依賴的package包/類
public static InetSocketAddress getDataNodeAddress() throws IOException {
String ifname = TcpStorageConstants.STORAGE_TCP_INTERFACE;
int port = TcpStorageConstants.STORAGE_TCP_PORT;
NetworkInterface netif = NetworkInterface.getByName(ifname);
if (netif == null){
return null;
}
List<InterfaceAddress> addresses = netif.getInterfaceAddresses();
InetAddress addr = null;
for (InterfaceAddress address: addresses){
if (address.getBroadcast() != null){
InetAddress _addr = address.getAddress();
addr = _addr;
}
}
InetSocketAddress inetAddr = new InetSocketAddress(addr, port);
return inetAddr;
}
示例9: getDataNodeAddress
import java.net.InterfaceAddress; //導入依賴的package包/類
public static InetSocketAddress getDataNodeAddress() throws IOException {
String ifname = RdmaConstants.STORAGE_RDMA_INTERFACE;
int port = RdmaConstants.STORAGE_RDMA_PORT;
NetworkInterface netif = NetworkInterface.getByName(ifname);
if (netif == null){
return null;
}
List<InterfaceAddress> addresses = netif.getInterfaceAddresses();
InetAddress addr = null;
for (InterfaceAddress address: addresses){
// LOG.info("address* " + address.toString() + ", _addr " + _addr.toString() + ", isSiteLocal " + _addr.isSiteLocalAddress() + ", tmp " + tmp + ", size " + tmp.length + ", broadcast " + address.getBroadcast());
if (address.getBroadcast() != null){
InetAddress _addr = address.getAddress();
addr = _addr;
}
}
InetSocketAddress inetAddr = new InetSocketAddress(addr, port);
return inetAddr;
}
示例10: sendBroadcastRequest
import java.net.InterfaceAddress; //導入依賴的package包/類
private void sendBroadcastRequest(InterfaceAddress ia) {
try {
// add to my address list
myAddress.add(ia.getAddress().getHostAddress());
// data to send
byte[] sendData = Commons.toBytes(mDataToSend);
// Send the broadcast package!
for (int port : SubnetServer.PORTS) {
DatagramPacket sendPacket = new DatagramPacket(
sendData, sendData.length,
ia.getBroadcast(), port);
mSocket.send(sendPacket);
}
} catch (Exception ex) {
Logs.error(getClass(), "Failed to send broadcast. {0}", ex);
}
}
示例11: firstLocalNonLoopbackIpv4Address
import java.net.InterfaceAddress; //導入依賴的package包/類
/**
* @deprecated This method is no longer used by LittleProxy and may be removed in a future release.
*/
@Deprecated
public static InetAddress firstLocalNonLoopbackIpv4Address() {
try {
Enumeration<NetworkInterface> networkInterfaces = NetworkInterface
.getNetworkInterfaces();
while (networkInterfaces.hasMoreElements()) {
NetworkInterface networkInterface = networkInterfaces
.nextElement();
if (networkInterface.isUp()) {
for (InterfaceAddress ifAddress : networkInterface
.getInterfaceAddresses()) {
if (ifAddress.getNetworkPrefixLength() > 0
&& ifAddress.getNetworkPrefixLength() <= 32
&& !ifAddress.getAddress().isLoopbackAddress()) {
return ifAddress.getAddress();
}
}
}
}
return null;
} catch (SocketException se) {
return null;
}
}
示例12: getBroadcast
import java.net.InterfaceAddress; //導入依賴的package包/類
public static InetAddress getBroadcast(InetAddress myIpAddress) {
NetworkInterface temp;
InetAddress iAddr = null;
try {
temp = NetworkInterface.getByInetAddress(myIpAddress);
List<InterfaceAddress> addresses = temp.getInterfaceAddresses();
for (InterfaceAddress inetAddress : addresses) {
iAddr = inetAddress.getBroadcast();
}
Log.d(TAG, "iAddr=" + iAddr);
return iAddr;
} catch (SocketException e) {
e.printStackTrace();
Log.d(TAG, "getBroadcast" + e.getMessage());
}
return null;
}
示例13: getAllServerAddress
import java.net.InterfaceAddress; //導入依賴的package包/類
protected static List<InetAddress> getAllServerAddress() {
List<InetAddress> result = new LinkedList<>();
try {
Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces();
if (interfaces == null) {
return null;
}
while (interfaces.hasMoreElements()) {
NetworkInterface current = interfaces.nextElement();
List<InterfaceAddress> addresses = current.getInterfaceAddresses();
if (addresses.size() == 0) {
continue;
}
for (InterfaceAddress interfaceAddress : addresses) {
InetAddress address = interfaceAddress.getAddress();
result.add(address);
}
}
} catch (SocketException e) {
}
return result;
}
示例14: getBroadcastAddress
import java.net.InterfaceAddress; //導入依賴的package包/類
public static String getBroadcastAddress() {
System.setProperty("java.net.preferIPv4Stack", "true");
try {
for (Enumeration<NetworkInterface> niEnum = NetworkInterface.getNetworkInterfaces(); niEnum
.hasMoreElements(); ) {
NetworkInterface ni = niEnum.nextElement();
if (!ni.isLoopback()) {
for (InterfaceAddress interfaceAddress : ni.getInterfaceAddresses()) {
if (interfaceAddress.getBroadcast() != null) {
L.d(TAG, interfaceAddress.getBroadcast().toString().substring(1));
return interfaceAddress.getBroadcast().toString().substring(1);
}
}
}
}
} catch (SocketException e) {
e.printStackTrace();
}
return null;
}
示例15: getLocalAddress
import java.net.InterfaceAddress; //導入依賴的package包/類
public final InetAddress getLocalAddress() throws SocketException {
for (final Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces(); interfaces
.hasMoreElements();) {
final NetworkInterface networkInterface = interfaces.nextElement();
if (networkInterface.isLoopback()) {
continue;
}
for (final InterfaceAddress interfaceAddr : networkInterface.getInterfaceAddresses()) {
final InetAddress inetAddr = interfaceAddr.getAddress();
if (!(inetAddr instanceof Inet4Address)) {
continue;
}
return inetAddr;
}
}
return null;
}