java类java.net.StandardProtocolFamily的实例源码

JdpBroadcaster.java 文件源码 项目:jdk8u-jdk 阅读 37 收藏 0 点赞 0 评论 0
/**
 * 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);
    }
}
SocketOptionRegistry.java 文件源码 项目:OpenJSharp 阅读 18 收藏 0 点赞 0 评论 0
private static Map<RegistryKey,OptionKey> options() {                  
    Map<RegistryKey,OptionKey> map =                                   
        new HashMap<RegistryKey,OptionKey>();                          
    map.put(new RegistryKey(StandardSocketOptions.SO_BROADCAST, Net.UNSPEC), new OptionKey(1, 6));
    map.put(new RegistryKey(StandardSocketOptions.SO_KEEPALIVE, Net.UNSPEC), new OptionKey(1, 9));
    map.put(new RegistryKey(StandardSocketOptions.SO_LINGER, Net.UNSPEC), new OptionKey(1, 13));
    map.put(new RegistryKey(StandardSocketOptions.SO_SNDBUF, Net.UNSPEC), new OptionKey(1, 7));
    map.put(new RegistryKey(StandardSocketOptions.SO_RCVBUF, Net.UNSPEC), new OptionKey(1, 8));
    map.put(new RegistryKey(StandardSocketOptions.SO_REUSEADDR, Net.UNSPEC), new OptionKey(1, 2));
    map.put(new RegistryKey(StandardSocketOptions.TCP_NODELAY, Net.UNSPEC), new OptionKey(6, 1));
    map.put(new RegistryKey(StandardSocketOptions.IP_TOS, StandardProtocolFamily.INET), new OptionKey(0, 1));
    map.put(new RegistryKey(StandardSocketOptions.IP_MULTICAST_IF, StandardProtocolFamily.INET), new OptionKey(0, 32));
    map.put(new RegistryKey(StandardSocketOptions.IP_MULTICAST_TTL, StandardProtocolFamily.INET), new OptionKey(0, 33));
    map.put(new RegistryKey(StandardSocketOptions.IP_MULTICAST_LOOP, StandardProtocolFamily.INET), new OptionKey(0, 34));
    map.put(new RegistryKey(StandardSocketOptions.IP_TOS, StandardProtocolFamily.INET6), new OptionKey(41, 67));
    map.put(new RegistryKey(StandardSocketOptions.IP_MULTICAST_IF, StandardProtocolFamily.INET6), new OptionKey(41, 17));
    map.put(new RegistryKey(StandardSocketOptions.IP_MULTICAST_TTL, StandardProtocolFamily.INET6), new OptionKey(41, 18));
    map.put(new RegistryKey(StandardSocketOptions.IP_MULTICAST_LOOP, StandardProtocolFamily.INET6), new OptionKey(41, 19));
    map.put(new RegistryKey(ExtendedSocketOption.SO_OOBINLINE, Net.UNSPEC), new OptionKey(1, 10));
    return map;                                                        
}
JdpBroadcaster.java 文件源码 项目:OpenJSharp 阅读 24 收藏 0 点赞 0 评论 0
/**
 * 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);
    }
}
SocketOptionRegistry.java 文件源码 项目:OpenJSharp 阅读 19 收藏 0 点赞 0 评论 0
private static Map<RegistryKey,OptionKey> options() {                  
    Map<RegistryKey,OptionKey> map =                                   
        new HashMap<RegistryKey,OptionKey>();                          
    map.put(new RegistryKey(StandardSocketOptions.SO_BROADCAST, Net.UNSPEC), new OptionKey(SocketOptionLevel.Socket, SocketOptionName.Broadcast));
    map.put(new RegistryKey(StandardSocketOptions.SO_KEEPALIVE, Net.UNSPEC), new OptionKey(SocketOptionLevel.Socket, SocketOptionName.KeepAlive));
    map.put(new RegistryKey(StandardSocketOptions.SO_LINGER, Net.UNSPEC), new OptionKey(SocketOptionLevel.Socket, SocketOptionName.Linger));
    map.put(new RegistryKey(StandardSocketOptions.SO_SNDBUF, Net.UNSPEC), new OptionKey(SocketOptionLevel.Socket, SocketOptionName.SendBuffer));
    map.put(new RegistryKey(StandardSocketOptions.SO_RCVBUF, Net.UNSPEC), new OptionKey(SocketOptionLevel.Socket, SocketOptionName.ReceiveBuffer));
    map.put(new RegistryKey(StandardSocketOptions.SO_REUSEADDR, Net.UNSPEC), new OptionKey(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress));
    map.put(new RegistryKey(StandardSocketOptions.TCP_NODELAY, Net.UNSPEC), new OptionKey(SocketOptionLevel.Tcp, SocketOptionName.NoDelay));
    map.put(new RegistryKey(StandardSocketOptions.IP_TOS, StandardProtocolFamily.INET), new OptionKey(SocketOptionLevel.IP, SocketOptionName.TypeOfService));
    map.put(new RegistryKey(StandardSocketOptions.IP_MULTICAST_IF, StandardProtocolFamily.INET), new OptionKey(SocketOptionLevel.IP, SocketOptionName.MulticastInterface));
    map.put(new RegistryKey(StandardSocketOptions.IP_MULTICAST_TTL, StandardProtocolFamily.INET), new OptionKey(SocketOptionLevel.IP, SocketOptionName.IpTimeToLive));
    map.put(new RegistryKey(StandardSocketOptions.IP_MULTICAST_LOOP, StandardProtocolFamily.INET), new OptionKey(SocketOptionLevel.IP, SocketOptionName.MulticastLoopback));
    map.put(new RegistryKey(StandardSocketOptions.IP_TOS, StandardProtocolFamily.INET6), new OptionKey(SocketOptionLevel.IPv6, ikvm.internal.Winsock.IPV6_TCLASS));
    map.put(new RegistryKey(StandardSocketOptions.IP_MULTICAST_IF, StandardProtocolFamily.INET6), new OptionKey(SocketOptionLevel.IPv6, SocketOptionName.MulticastInterface));
    map.put(new RegistryKey(StandardSocketOptions.IP_MULTICAST_TTL, StandardProtocolFamily.INET6), new OptionKey(SocketOptionLevel.IPv6, SocketOptionName.IpTimeToLive));
    map.put(new RegistryKey(StandardSocketOptions.IP_MULTICAST_LOOP, StandardProtocolFamily.INET6), new OptionKey(SocketOptionLevel.IPv6, SocketOptionName.MulticastLoopback));
    map.put(new RegistryKey(ExtendedSocketOption.SO_OOBINLINE, Net.UNSPEC), new OptionKey(SocketOptionLevel.Socket, SocketOptionName.OutOfBandInline));
    return map;                                                        
}
AddressUtils.java 文件源码 项目:bt 阅读 24 收藏 0 点赞 0 评论 0
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;
    }
}
OpentrackerLiveSync.java 文件源码 项目:bt 阅读 16 收藏 0 点赞 0 评论 0
@Override
public void start(Collection<DHT> dhts, ConfigReader config) {
    try {
        channel = DatagramChannel.open(StandardProtocolFamily.INET);
        channel.setOption(StandardSocketOptions.IP_MULTICAST_TTL, 1);
        channel.setOption(StandardSocketOptions.SO_REUSEADDR, true);
        // we only need to send, not to receive, so need to bind to a specific port
        channel.bind(new InetSocketAddress(0));
        channel.connect(new InetSocketAddress(InetAddress.getByAddress(new byte[] {(byte) 224,0,23,5}), 9696));
    } catch (IOException e) {
        e.printStackTrace();
        return;
    }

    t.setDaemon(true);
    t.setName("opentracker-sync");
    t.start();

    // OT-sync only supports ipv4 atm
    dhts.stream().filter(d -> d.getType().PREFERRED_ADDRESS_TYPE == Inet4Address.class).forEach(d -> {
        d.addIncomingMessageListener(this::incomingPacket);
    });

}
LocalServiceDiscoveryInfo.java 文件源码 项目:bt 阅读 15 收藏 0 点赞 0 评论 0
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);
}
DatagramBulkClient.java 文件源码 项目:netcrusher-java 阅读 15 收藏 0 点赞 0 评论 0
public DatagramBulkClient(String name,
                          InetSocketAddress bindAddress,
                          InetSocketAddress connectAddress,
                          long limit,
                          CyclicBarrier readBarrier,
                          CyclicBarrier sentBarrier) throws IOException
{
    this.channel = DatagramChannel.open(StandardProtocolFamily.INET);
    this.channel.configureBlocking(true);
    this.channel.bind(bindAddress);

    if (LOGGER.isDebugEnabled()) {
        LOGGER.debug("Bulk client {}: BIND<{}> CONNECT<{}>",
            new Object[]{name, bindAddress, connectAddress});
    }

    this.consumer = new Consumer(channel, name, limit, connectAddress, readBarrier);
    this.producer = new Producer(channel, name, limit, connectAddress, sentBarrier);
}
JdpBroadcaster.java 文件源码 项目:jdk8u_jdk 阅读 28 收藏 0 点赞 0 评论 0
/**
 * 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);
    }
}
JdpBroadcaster.java 文件源码 项目:lookaside_java-1.8.0-openjdk 阅读 27 收藏 0 点赞 0 评论 0
/**
 * 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);
    }
}
MarketData.java 文件源码 项目:parity-extras 阅读 14 收藏 0 点赞 0 评论 0
public static MarketData open(NetworkInterface multicastInterface,
        InetSocketAddress multicastGroup, InetSocketAddress requestAddress,
        long instrument) throws IOException {
    DatagramChannel channel = DatagramChannel.open(StandardProtocolFamily.INET);

    channel.setOption(StandardSocketOptions.SO_REUSEADDR, true);
    channel.bind(new InetSocketAddress(multicastGroup.getPort()));
    channel.join(multicastGroup.getAddress(), multicastInterface);
    channel.configureBlocking(false);

    DatagramChannel requestChannel = DatagramChannel.open(StandardProtocolFamily.INET);

    requestChannel.configureBlocking(false);

    return new MarketData(channel, requestChannel, requestAddress, instrument);
}
AddressUtils.java 文件源码 项目:mldht 阅读 23 收藏 0 点赞 0 评论 0
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;
    }
}
OpentrackerLiveSync.java 文件源码 项目:mldht 阅读 18 收藏 0 点赞 0 评论 0
@Override
public void start(Collection<DHT> dhts, ConfigReader config) {
    try {
        channel = DatagramChannel.open(StandardProtocolFamily.INET);
        channel.setOption(StandardSocketOptions.IP_MULTICAST_TTL, 1);
        channel.setOption(StandardSocketOptions.SO_REUSEADDR, true);
        // we only need to send, not to receive, so need to bind to a specific port
        channel.bind(new InetSocketAddress(0));
        channel.connect(new InetSocketAddress(InetAddress.getByAddress(new byte[] {(byte) 224,0,23,5}), 9696));
    } catch (IOException e) {
        e.printStackTrace();
        return;
    }

    t.setDaemon(true);
    t.setName("opentracker-sync");
    t.start();

    // OT-sync only supports ipv4 atm
    dhts.stream().filter(d -> d.getType().PREFERRED_ADDRESS_TYPE == Inet4Address.class).forEach(d -> {
        d.addIncomingMessageListener(this::incomingPacket);
    });

}
BasicMulticastSocketHandler.java 文件源码 项目:wot_gateways 阅读 15 收藏 0 点赞 0 评论 0
public BasicMulticastSocketHandler(ChannelManager channelManager,
        int port, InetAddress multicastIP) throws IOException {
    logger.addAppender(new ConsoleAppender(new SimpleLayout()));
    // ALL | DEBUG | INFO | WARN | ERROR | FATAL | OFF:
    logger.setLevel(Level.WARN);

    NetworkInterface interf = NetworkInterface.getByInetAddress(InetAddress.getLocalHost());
    //NetworkInterface interf = NetworkInterface.getByName("eth0");

    this.channelManager = channelManager;
    mcastChannel = DatagramChannel.open(StandardProtocolFamily.INET);
    mcastChannel.setOption(StandardSocketOptions.SO_REUSEADDR, true);
    mcastChannel.bind(new InetSocketAddress(port));     
    mcastChannel.setOption(StandardSocketOptions.IP_MULTICAST_IF, interf);
    mcastChannel.join(multicastIP, interf);
    mcastChannel.configureBlocking(false);

    this.localPort = mcastChannel.socket().getLocalPort();
    this.localAddr = multicastIP;

    workerThread = new WorkerThread();
    workerThread.start();
}
UluxBinding.java 文件源码 项目:openhab-ulux-binding 阅读 15 收藏 0 点赞 0 评论 0
private void startListenerThread() {
    try {
        this.channel = DatagramChannel.open(StandardProtocolFamily.INET);
        this.channel.socket().bind(configuration.getBindSocketAddress());
    } catch (final IOException e) {
        throw new UluxException("Could not open UDP port for listening!", e);
    }

    this.thread = new Thread(this);
    this.thread.start();

    this.audioChannel = new AudioChannel(configuration, providers);
    this.audioChannel.start();

    this.commandHandler.activate(audioChannel.getChannel(), channel, executorService);
    this.stateUpdateHandler.activate(audioChannel.getChannel(), channel, executorService);

}
AudioChannel.java 文件源码 项目:openhab-ulux-binding 阅读 19 收藏 0 点赞 0 评论 0
public void start() {
    this.audioSource = new AudioReceiver(configuration, providers);
    this.audioSource.start();

    try {
        this.channel = DatagramChannel.open(StandardProtocolFamily.INET);
        this.channel.socket().bind(configuration.getAudioSocketAddress());
    } catch (final IOException e) {
        throw new UluxException("Could not open UDP port for listening!", e);
    }

    this.listenerThread = new Thread(this, "u::Lux audio listener");
    this.listenerThread.start();

    // TODO AudioReceiver?
}
JdpBroadcaster.java 文件源码 项目:infobip-open-jdk-8 阅读 38 收藏 0 点赞 0 评论 0
/**
 * 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);
    }
}
JdpBroadcaster.java 文件源码 项目:jdk8u-dev-jdk 阅读 35 收藏 0 点赞 0 评论 0
/**
 * 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);
    }
}
JdpBroadcaster.java 文件源码 项目:OLD-OpenJDK8 阅读 25 收藏 0 点赞 0 评论 0
/**
 * 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);
    }
}
JdpBroadcaster.java 文件源码 项目:openjdk-jdk7u-jdk 阅读 29 收藏 0 点赞 0 评论 0
/**
 * 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);
    }
}
JdpBroadcaster.java 文件源码 项目:openjdk-icedtea7 阅读 28 收藏 0 点赞 0 评论 0
/**
 * 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);
    }
}
Acceptor.java 文件源码 项目:URingPaxos 阅读 14 收藏 0 点赞 0 评论 0
/**
 * Public constructor
 * 
 * @param id proposer id
 * @param config path to config file
 * @throws IOException 
 */
public Acceptor(int id,String config) throws IOException{
    this.ID = id;
    if(Configuration.getConfiguration().isEmpty()){
        Configuration.read(config);
    }

    NetworkInterface i = NetworkInterface.getByName(Configuration.getInterface());
    channel = DatagramChannel.open(StandardProtocolFamily.INET)
         .setOption(StandardSocketOptions.SO_REUSEADDR, true)
         .bind(Configuration.getGroup(PaxosRole.Acceptor))
         .setOption(StandardSocketOptions.IP_MULTICAST_IF, i);
    channel.configureBlocking(false);
    channel.join(Configuration.getGroup(PaxosRole.Acceptor).getAddress(), i);

    for(int n=0;n<threadCount;n++){
        executer.execute(new AcceptorListener(this));
    }
}
Learner.java 文件源码 项目:URingPaxos 阅读 19 收藏 0 点赞 0 评论 0
/**
 * Public constructor
 * 
 * @param id proposer id
 * @param config path to config file
 * @throws IOException 
 */
public Learner(int id,String config) throws IOException{
    this.ID = id;
    if(Configuration.getConfiguration().isEmpty()){
        Configuration.read(config);
    }
    NetworkInterface i = NetworkInterface.getByName(Configuration.getInterface());
    this.channel = DatagramChannel.open(StandardProtocolFamily.INET)
         .setOption(StandardSocketOptions.SO_REUSEADDR, true)
         .bind(Configuration.getGroup(PaxosRole.Learner))
         .setOption(StandardSocketOptions.IP_MULTICAST_IF, i);
    this.channel.configureBlocking(false);
    this.channel.join(Configuration.getGroup(PaxosRole.Learner).getAddress(), i);
    for(int n=0;n<threadCount;n++){
        executer.execute(new LearnerListener(this));
    }
    writer.execute(new LearnerWriter(this));
}
NetDiscover.java 文件源码 项目:MyDMAM 阅读 16 收藏 0 点赞 0 评论 0
Group(InetSocketAddress group_socket) throws IOException {
    this.group_socket = group_socket;

    if (group_socket.getAddress() instanceof Inet4Address) {
        pf = StandardProtocolFamily.INET;
    } else {
        pf = StandardProtocolFamily.INET6;
    }

    InetAddress first_addrnetwork_interface = network_interface.getInterfaceAddresses().stream().map(inet -> {
        return inet.getAddress();
    }).filter(inet_addr -> {
        return pf == StandardProtocolFamily.INET & inet_addr instanceof Inet4Address | pf == StandardProtocolFamily.INET6 & inet_addr instanceof Inet6Address;
    }).findFirst().orElseThrow(() -> new IOException("Can't found IPv4/6 addr in a IPv6/4 link"));

    bind_to = new InetSocketAddress(group_socket.getPort());
    if (SystemUtils.IS_OS_WINDOWS) {
        bind_to = new InetSocketAddress(first_addrnetwork_interface, group_socket.getPort());
    }
}
PriceReceiver.java 文件源码 项目:yass 阅读 14 收藏 0 点赞 0 评论 0
public static void main(final String... args) throws Exception {
    final SimpleTransportSetup setup = new SimpleTransportSetup(
        Config.MESSAGE_SERIALIZER,
        new Server(
            Config.INITIATOR.priceListener.service(new PriceListener() {
                @Override public void newPrices(final List<Price> prices) {
                    final Price price = prices.get(0);
                    System.out.println("received " + price.kind + ": " + price.value);
                }
            })
        )
    );
    final DatagramChannel channel = DatagramChannel.open(StandardProtocolFamily.INET)
        .setOption(StandardSocketOptions.SO_REUSEADDR, true)
        .bind(new InetSocketAddress(PriceSender.PORT));
    final NetworkInterface networkInterface = NetworkInterface.getByInetAddress(InetAddress.getLocalHost());
    channel.join(InetAddress.getByName(PriceSender.GROUP_BID), networkInterface);
    channel.join(InetAddress.getByName(PriceSender.GROUP_ASK), networkInterface);
    while (true) {
        DatagramTransport.invoke(setup, channel, 128);
    }
}
SourcePathMessage.java 文件源码 项目:javapgm 阅读 16 收藏 0 点赞 0 评论 0
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;
        }
JdpBroadcaster.java 文件源码 项目:openjdk-jdk10 阅读 25 收藏 0 点赞 0 评论 0
/**
 * 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);
    }
}
JdpBroadcaster.java 文件源码 项目:openjdk9 阅读 29 收藏 0 点赞 0 评论 0
/**
 * 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);
    }
}
AnnounceGroupChannel.java 文件源码 项目:bt 阅读 15 收藏 0 点赞 0 评论 0
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;
}
InternetProtocolUtils.java 文件源码 项目:bt 阅读 15 收藏 0 点赞 0 评论 0
/**
 * @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);
    }
}


问题


面经


文章

微信
公众号

扫码关注公众号