当前位置: 首页>>代码示例>>Java>>正文


Java ProtocolFamily类代码示例

本文整理汇总了Java中java.net.ProtocolFamily的典型用法代码示例。如果您正苦于以下问题:Java ProtocolFamily类的具体用法?Java ProtocolFamily怎么用?Java ProtocolFamily使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。


ProtocolFamily类属于java.net包,在下文中一共展示了ProtocolFamily类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: JdpBroadcaster

import java.net.ProtocolFamily; //导入依赖的package包/类
/**
 * Create a new broadcaster
 *
 * @param address - multicast group address
 * @param srcAddress - address of interface we should use to broadcast.
 * @param port - udp port to use
 * @param ttl - packet ttl
 * @throws IOException
 */
public JdpBroadcaster(InetAddress address, InetAddress srcAddress, int port, int ttl)
        throws IOException, JdpException {
    this.addr = address;
    this.port = port;

    ProtocolFamily family = (address instanceof Inet6Address)
            ? StandardProtocolFamily.INET6 : StandardProtocolFamily.INET;

    channel = DatagramChannel.open(family);
    channel.setOption(StandardSocketOptions.SO_REUSEADDR, true);
    channel.setOption(StandardSocketOptions.IP_MULTICAST_TTL, ttl);

    // with srcAddress equal to null, this constructor do exactly the same as
    // if srcAddress is not passed
    if (srcAddress != null) {
        // User requests particular interface to bind to
        NetworkInterface interf = NetworkInterface.getByInetAddress(srcAddress);
        try {
            channel.bind(new InetSocketAddress(srcAddress, 0));
        } catch (UnsupportedAddressTypeException ex) {
            throw new JdpException("Unable to bind to source address");
        }
        channel.setOption(StandardSocketOptions.IP_MULTICAST_IF, interf);
    }
}
 
开发者ID:lambdalab-mirror,项目名称:jdk8u-jdk,代码行数:35,代码来源:JdpBroadcaster.java

示例2: getDefaultRoute

import java.net.ProtocolFamily; //导入依赖的package包/类
public static InetAddress getDefaultRoute(Class<? extends InetAddress> type) {
	InetAddress target = null;
	
	ProtocolFamily family = type == Inet6Address.class ? StandardProtocolFamily.INET6 : StandardProtocolFamily.INET;
	
	try(DatagramChannel chan=DatagramChannel.open(family)) {
		if(type == Inet4Address.class)
			target = InetAddress.getByAddress(new byte[] {8,8,8,8});
		if(type == Inet6Address.class)
			target = InetAddress.getByName("2001:4860:4860::8888");
		
		chan.connect(new InetSocketAddress(target,63));
		
		InetSocketAddress soa = (InetSocketAddress) chan.getLocalAddress();
		InetAddress local = soa.getAddress();
		
		if(type.isInstance(local) && !local.isAnyLocalAddress())
			return local;
		return null;
	} catch (IOException e) {
		e.printStackTrace();
		return null;
	}
}
 
开发者ID:atomashpolskiy,项目名称:bt,代码行数:25,代码来源:AddressUtils.java

示例3: LocalServiceDiscoveryInfo

import java.net.ProtocolFamily; //导入依赖的package包/类
public LocalServiceDiscoveryInfo(
        Set<SocketChannelConnectionAcceptor> socketAcceptors,
        Collection<AnnounceGroup> announceGroups) {

    this.localPorts = unmodifiableSet(collectLocalPorts(socketAcceptors));

    Collection<NetworkInterface> networkInterfaces = new HashSet<>();
    boolean acceptIP4 = false;
    boolean acceptIP6 = false;
    for (SocketChannelConnectionAcceptor acceptor : socketAcceptors) {
        networkInterfaces.add(acceptor.getNetworkInterface());
        InetSocketAddress address = acceptor.getLocalAddress();
        ProtocolFamily protocolFamily = InternetProtocolUtils.getProtocolFamily(address.getAddress());
        if (protocolFamily == StandardProtocolFamily.INET) {
            acceptIP4 = true;
        } else {
            acceptIP6 = true;
        }
        if (acceptIP4 && acceptIP6) {
            break; // no need to look further
        }
    }

    this.compatibleGroups = unmodifiableCollection(collectCompatibleGroups(announceGroups, acceptIP4, acceptIP6));
    this.networkInterfaces = unmodifiableCollection(networkInterfaces);
}
 
开发者ID:atomashpolskiy,项目名称:bt,代码行数:27,代码来源:LocalServiceDiscoveryInfo.java

示例4: create

import java.net.ProtocolFamily; //导入依赖的package包/类
public static SocketBuffer create (ProtocolFamily family, int flags) {
                int tpdu_length = Packet.SIZEOF_PGM_HEADER;
                if (StandardProtocolFamily.INET6 == family)
                        tpdu_length += SIZEOF_SPM6_HEADER;
                else
                        tpdu_length += SIZEOF_SPM_HEADER;
                if (Packet.PGM_OPT_FIN == flags)
                {
                        tpdu_length += Packet.SIZEOF_PGM_OPT_LENGTH;
/* End of session */
                        if (Packet.PGM_OPT_FIN == flags)
                                tpdu_length += Packet.SIZEOF_PGM_OPT_HEADER + Packet.SIZEOF_PGM_OPT_FIN;
                }
                SocketBuffer skb = new SocketBuffer (tpdu_length);
		skb.setHeaderOffset (0);
		skb.getHeader().setType (Packet.PGM_SPM);
		skb.reserve (Packet.SIZEOF_PGM_HEADER);
                return skb;
        }
 
开发者ID:steve-o,项目名称:javapgm,代码行数:20,代码来源:SourcePathMessage.java

示例5: getMulticastEnabledNodeAddress

import java.net.ProtocolFamily; //导入依赖的package包/类
public static InetAddress getMulticastEnabledNodeAddress (ProtocolFamily family) throws UnknownHostException, SocketException {
                InetAddress res[] = getNodeAddress (family);
            
/* iff one address return that independent of multicast support */
                if (res.length == 1)
                        return res[0];
                
                for (InetAddress addr : res) {
/* For each node address find matching interface and test flags */                
                        java.net.NetworkInterface ni = java.net.NetworkInterface.getByInetAddress (addr);
                        if (ni.supportsMulticast())
                                return addr;
                }
                
/* Use last address as fallback */
                return res[res.length - 1];
        }
 
开发者ID:steve-o,项目名称:javapgm,代码行数:18,代码来源:NetworkInterface.java

示例6: Socket

import java.net.ProtocolFamily; //导入依赖的package包/类
public Socket (ProtocolFamily family) throws IOException
{
               LOG.debug ("Socket (family: {})", family);
               
               this.family = family;
               this.canSendData = true;
               this.canSendNak = true;
               this.canReceiveData = true;
               this.dataDestinationPort = Packet.DEFAULT_DATA_DESTINATION_PORT;
               this.tsi = new TransportSessionId (null, Packet.DEFAULT_DATA_SOURCE_PORT);

               LOG.trace (NETWORK_MARKER, "Opening UDP encapsulated sockets.");
	this.recv_sock = DatagramChannel.open (this.family);
               LOG.trace (NETWORK_MARKER, "Set socket sharing.");
               this.recv_sock.setOption (StandardSocketOptions.SO_REUSEADDR, true);
               this.recv_sock.configureBlocking (false);

	this.send_sock = new MulticastSocket ();
               
               LOG.debug ("PGM socket successfully created.");
       }
 
开发者ID:steve-o,项目名称:javapgm,代码行数:22,代码来源:Socket.java

示例7: JdpBroadcaster

import java.net.ProtocolFamily; //导入依赖的package包/类
/**
 * Create a new broadcaster
 *
 * @param address - multicast group address
 * @param srcAddress - address of interface we should use to broadcast.
 * @param port - udp port to use
 * @param ttl - packet ttl
 * @throws IOException
 */
public JdpBroadcaster(InetAddress address, InetAddress srcAddress, int port, int ttl)
        throws IOException, JdpException {
    this.addr = address;
    this.port = port;

    ProtocolFamily family = (address instanceof Inet6Address)
            ? StandardProtocolFamily.INET6 : StandardProtocolFamily.INET;

    channel = DatagramChannel.open(family);
    channel.setOption(StandardSocketOptions.SO_REUSEADDR, true);
    channel.setOption(StandardSocketOptions.IP_MULTICAST_TTL, ttl);

    // with srcAddress equal to null, this constructor do exactly the same as
    // if srcAddress is not passed
    if (srcAddress != null) {
        // User requests particular interface to bind to
        NetworkInterface interf = NetworkInterface.getByInetAddress(srcAddress);

        if (interf == null) {
            throw new JdpException("Unable to get network interface for " + srcAddress.toString());
        }

        if (!interf.isUp()) {
            throw new JdpException(interf.getName() + " is not up.");
        }

        if (!interf.supportsMulticast()) {
            throw new JdpException(interf.getName() + " does not support multicast.");
        }

        try {
            channel.bind(new InetSocketAddress(srcAddress, 0));
        } catch (UnsupportedAddressTypeException ex) {
            throw new JdpException("Unable to bind to source address");
        }
        channel.setOption(StandardSocketOptions.IP_MULTICAST_IF, interf);
    }
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:48,代码来源:JdpBroadcaster.java

示例8: DHTtype

import java.net.ProtocolFamily; //导入依赖的package包/类
private DHTtype(String shortName, int nodeslength, int addresslength, Class<? extends InetAddress> addresstype, int header, int maxSize, ProtocolFamily family) {
	
	this.shortName = shortName;
	this.NODES_ENTRY_LENGTH = nodeslength;
	this.PREFERRED_ADDRESS_TYPE = addresstype;
	this.ADDRESS_ENTRY_LENGTH = addresslength;
	this.HEADER_LENGTH = header;
	this.MAX_PACKET_SIZE = maxSize;
	this.PROTO_FAMILY = family;
}
 
开发者ID:atomashpolskiy,项目名称:bt,代码行数:11,代码来源:DHT.java

示例9: getChannel

import java.net.ProtocolFamily; //导入依赖的package包/类
private synchronized DatagramChannel getChannel() throws IOException {
    if (channel == null || !channel.isOpen()) {
        if (shutdown.get()) {
            throw new IllegalStateException("Channel has been shut down");
        }
        ProtocolFamily protocolFamily = InternetProtocolUtils.getProtocolFamily(group.getAddress().getAddress());
        DatagramChannel _channel = selector.provider().openDatagramChannel(protocolFamily);
        _channel.setOption(StandardSocketOptions.SO_REUSEADDR, true);
        // bind to any-local before setting TTL
        int port = group.getAddress().getPort();
        if (protocolFamily == StandardProtocolFamily.INET) {
            _channel.bind(new InetSocketAddress(Inet4Address.getByName("0.0.0.0"), port));
        } else {
            _channel.bind(new InetSocketAddress(Inet6Address.getByName("[::]"), port));
        }
        int timeToLive = group.getTimeToLive();
        if (timeToLive != 1) {
            _channel.setOption(StandardSocketOptions.IP_MULTICAST_TTL, timeToLive);
        }

        for (NetworkInterface iface : networkInterfaces) {
            _channel.join(group.getAddress().getAddress(), iface);
        }

        _channel.configureBlocking(false);
        channel = _channel;
    }
    return channel;
}
 
开发者ID:atomashpolskiy,项目名称:bt,代码行数:30,代码来源:AnnounceGroupChannel.java

示例10: getProtocolFamily

import java.net.ProtocolFamily; //导入依赖的package包/类
/**
 * @return {@link StandardProtocolFamily#INET} for IPv4 address or {@link StandardProtocolFamily#INET6} for IPv6 address
 * @throws IllegalArgumentException if the address is neither IPv4 or IPv6
 * @since 1.6
 */
public static ProtocolFamily getProtocolFamily(InetAddress address) {
    if (address.getAddress().length == IP4_BYTES) {
        return StandardProtocolFamily.INET;
    } else if (address.getAddress().length == IP6_BYTES) {
        return StandardProtocolFamily.INET6;
    } else {
        throw new IllegalArgumentException("Can't determine protocol family for address: " + address);
    }
}
 
开发者ID:atomashpolskiy,项目名称:bt,代码行数:15,代码来源:InternetProtocolUtils.java

示例11: convert

import java.net.ProtocolFamily; //导入依赖的package包/类
/**
 * Convert the {@link InternetProtocolFamily}. This MUST only be called on jdk version >= 7.
 */
public static ProtocolFamily convert(InternetProtocolFamily family) {
    switch (family) {
    case IPv4:
        return StandardProtocolFamily.INET;
    case IPv6:
        return StandardProtocolFamily.INET6;
    default:
        throw new IllegalArgumentException();
    }
}
 
开发者ID:wuyinxian124,项目名称:netty4.0.27Learn,代码行数:14,代码来源:ProtocolFamilyConverter.java

示例12: convert

import java.net.ProtocolFamily; //导入依赖的package包/类
/**
 * Convert the {@link InternetProtocolFamily}. This MUST only be called on jdk version >= 7.
 */
public static ProtocolFamily convert(InternetProtocolFamily family) {
    switch (family) {
    case IPv4:
        return StandardProtocolFamily.INET;

    case IPv6:
        return StandardProtocolFamily.INET6;
    default:
        throw new IllegalArgumentException();
    }
}
 
开发者ID:nyankosama,项目名称:simple-netty-source,代码行数:15,代码来源:ProtocolFamilyConverter.java

示例13: setupMulticast

import java.net.ProtocolFamily; //导入依赖的package包/类
/**
 * Sets up the multicast channel
 * 
 * @throws IOException
 *             Something wrong occurred (bad address, bad port, ...)
 */
private void setupMulticast() throws IOException {

    // Compute the address family
    final ProtocolFamily family;
    if (pAddress instanceof Inet4Address) {
        // IPv4
        family = StandardProtocolFamily.INET;

    } else if (pAddress instanceof Inet6Address) {
        // IPv6
        family = StandardProtocolFamily.INET6;

    } else {
        // Unknown
        throw new SocketException("Unknown multicast group family");
    }

    // Create the UDP channel
    pChannel = DatagramChannel.open(family);
    pChannel.setOption(StandardSocketOptions.SO_REUSEADDR, true);
    pChannel.bind(new InetSocketAddress(pPort));

    try {
        // Join the group on all interfaces
        for (final NetworkInterface itf : getMulticastInterfaces()) {
            pJoinedGroups.add(pChannel.join(pAddress, itf));
        }

    } catch (final SocketException ex) {
        // Be nice...
        pChannel.close();
        throw ex;
    }
}
 
开发者ID:cohorte,项目名称:cohorte-remote-services,代码行数:41,代码来源:MulticastHandler.java

示例14: getNodeAddress

import java.net.ProtocolFamily; //导入依赖的package包/类
public static InetAddress[] getNodeAddress (ProtocolFamily family) throws UnknownHostException {
        LinkedList<InetAddress> list = new LinkedList<> ();
        String nodename = InetAddress.getLocalHost().getHostName();
        for (InetAddress addr : InetAddress.getAllByName (nodename)) {
                if (StandardProtocolFamily.INET == family && addr instanceof Inet4Address)
                        list.add (addr);
                else if (StandardProtocolFamily.INET6 == family && addr instanceof Inet6Address)
                        list.add (addr);
        }
        return list.toArray (new InetAddress[list.size()]);
}
 
开发者ID:steve-o,项目名称:javapgm,代码行数:12,代码来源:NetworkInterface.java

示例15: create

import java.net.ProtocolFamily; //导入依赖的package包/类
public static SocketBuffer create (ProtocolFamily family, int tsdu_length) {
              int tpdu_length = Packet.calculateOffset (false, null) + tsdu_length;
              SocketBuffer skb = new SocketBuffer (tpdu_length);
skb.setHeaderOffset (0);
skb.getHeader().setType (Packet.PGM_ODATA);
              skb.getHeader().setTsduLength (tsdu_length);
skb.reserve (Packet.calculateOffset (false, null));
              skb.put (tsdu_length);
              skb.setOriginalDataOffset (Packet.SIZEOF_PGM_HEADER);
              return skb;
      }
 
开发者ID:steve-o,项目名称:javapgm,代码行数:12,代码来源:OriginalData.java


注:本文中的java.net.ProtocolFamily类示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。