Java 类java.net.StandardProtocolFamily 实例源码

项目:jdk8u-jdk    文件:JdpBroadcaster.java   
/**
 * 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);
    }
}
项目:OpenJSharp    文件:SocketOptionRegistry.java   
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;                                                        
}
项目:OpenJSharp    文件:JdpBroadcaster.java   
/**
 * 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);
    }
}
项目:OpenJSharp    文件:SocketOptionRegistry.java   
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;                                                        
}
项目:bt    文件:AddressUtils.java   
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;
    }
}
项目:bt    文件:OpentrackerLiveSync.java   
@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);
    });

}
项目:bt    文件:LocalServiceDiscoveryInfo.java   
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);
}
项目:netcrusher-java    文件:DatagramBulkClient.java   
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);
}
项目:jdk8u_jdk    文件:JdpBroadcaster.java   
/**
 * 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);
    }
}
项目:lookaside_java-1.8.0-openjdk    文件:JdpBroadcaster.java   
/**
 * 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);
    }
}
项目:parity-extras    文件:MarketData.java   
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);
}
项目:mldht    文件:AddressUtils.java   
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;
    }
}
项目:mldht    文件:OpentrackerLiveSync.java   
@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);
    });

}
项目:wot_gateways    文件:BasicMulticastSocketHandler.java   
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();
}
项目:openhab-ulux-binding    文件:UluxBinding.java   
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);

}
项目:openhab-ulux-binding    文件:AudioChannel.java   
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?
}
项目:infobip-open-jdk-8    文件:JdpBroadcaster.java   
/**
 * 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);
    }
}
项目:jdk8u-dev-jdk    文件:JdpBroadcaster.java   
/**
 * 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);
    }
}
项目:OLD-OpenJDK8    文件:JdpBroadcaster.java   
/**
 * 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);
    }
}
项目:openjdk-jdk7u-jdk    文件:JdpBroadcaster.java   
/**
 * 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);
    }
}
项目:openjdk-icedtea7    文件:JdpBroadcaster.java   
/**
 * 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);
    }
}
项目:URingPaxos    文件:Acceptor.java   
/**
 * 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));
    }
}
项目:URingPaxos    文件:Learner.java   
/**
 * 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));
}
项目:MyDMAM    文件:NetDiscover.java   
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());
    }
}
项目:yass    文件:PriceReceiver.java   
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);
    }
}
项目:javapgm    文件:SourcePathMessage.java   
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;
        }
项目:openjdk-jdk10    文件:JdpBroadcaster.java   
/**
 * 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);
    }
}
项目:openjdk9    文件:JdpBroadcaster.java   
/**
 * 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);
    }
}
项目:bt    文件:AnnounceGroupChannel.java   
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;
}
项目:bt    文件:InternetProtocolUtils.java   
/**
 * @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);
    }
}
项目:netcrusher-java    文件:CrusherDatagramSocat6Test.java   
@Before
public void setUp() throws Exception {
    reactor = new NioReactor();

    crusher = DatagramCrusherBuilder.builder()
        .withReactor(reactor)
        .withBindAddress(ADDR_LOOPBACK6, PORT_DIRECT)
        .withConnectAddress(ADDR_LOOPBACK6, PORT_PROXY)
        .withProtocolFamily(StandardProtocolFamily.INET6)
        .withCreationListener((addr) -> LOGGER.info("Client is created <{}>", addr))
        .withDeletionListener((addr, byteMeters, packetMeters) -> LOGGER.info("Client is deleted <{}>", addr))
        .buildAndOpen();
}
项目:netcrusher-java    文件:CrusherDatagramSocat4Test.java   
@Before
public void setUp() throws Exception {
    reactor = new NioReactor();

    crusher = DatagramCrusherBuilder.builder()
        .withReactor(reactor)
        .withBindAddress(ADDR_LOOPBACK4, PORT_DIRECT)
        .withConnectAddress(ADDR_LOOPBACK4, PORT_PROXY)
        .withProtocolFamily(StandardProtocolFamily.INET)
        .withCreationListener((addr) -> LOGGER.info("Client is created <{}>", addr))
        .withDeletionListener((addr, byteMeters, packetMeters) -> LOGGER.info("Client is deleted <{}>", addr))
        .buildAndOpen();
}
项目:netcrusher-java    文件:DatagramBulkReflector.java   
public DatagramBulkReflector(String name, InetSocketAddress bindAddress,
                             long limit,
                             CyclicBarrier readBarrier) throws IOException {
    this.channel = DatagramChannel.open(StandardProtocolFamily.INET);
    this.channel.configureBlocking(true);
    this.channel.bind(bindAddress);

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

    this.reflector = new Reflector(channel, name, limit, readBarrier);
}
项目:angler    文件:ExampleApplication.java   
private static DatagramChannel multicastListener(final InetAddress address, final int port) throws IOException
{
    final NetworkInterface networkInterface = getMulticastCapableNetworkInterface();
    final DatagramChannel channel = DatagramChannel.open(StandardProtocolFamily.INET);
    channel.bind(new InetSocketAddress(address, port));
    channel.setOption(StandardSocketOptions.SO_REUSEADDR, true);
    channel.setOption(StandardSocketOptions.IP_MULTICAST_IF, networkInterface);
    channel.join(address, networkInterface);

    return channel;
}
项目:angler    文件:ExampleApplication.java   
private static DatagramChannel createListeningChannelOnPort(final InetSocketAddress local) throws IOException
{
    return DatagramChannel.open(StandardProtocolFamily.INET).
            setOption(StandardSocketOptions.SO_REUSEADDR, true).
            setOption(StandardSocketOptions.SO_RCVBUF, 4096).
            bind(local);
}
项目:netty4.0.27Learn    文件:ProtocolFamilyConverter.java   
/**
 * 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();
    }
}
项目:wot_gateways    文件:BasicMulticastSocketHandler.java   
public BasicMulticastSocketHandler(ChannelManager channelManager)
        throws IOException {
    this.channelManager = channelManager;
    mcastChannel = DatagramChannel.open(StandardProtocolFamily.INET);
    mcastChannel.socket().bind(new InetSocketAddress(0)); // port can be 0,
                                                            // then a free
                                                            // port is
                                                            // chosen
    this.localPort = mcastChannel.socket().getLocalPort();
    mcastChannel.configureBlocking(false);

    workerThread = new WorkerThread();
    workerThread.start();

}
项目:eborp    文件:AutoDetectingServerConfig.java   
private DatagramChannel createReceiveChannel() throws IOException {
    final String nicName = properties.getProperty(PropertiesReader.NETWORK_INTERFACE);
    final NetworkInterface networkInterface = getNetworkInterface(nicName);

    final DatagramChannel channel = DatagramChannel.open(StandardProtocolFamily.INET);
    channel.setOption(StandardSocketOptions.SO_REUSEADDR, true)
            .bind(new InetSocketAddress(ANNOUNCE_PORT))
            .setOption(StandardSocketOptions.IP_MULTICAST_IF, networkInterface);

    final InetAddress group = InetAddress.getByName(MULTICAST_GROUP);
    channel.join(group, networkInterface);

    return channel;
}
项目:eborp    文件:UdpDataSink.java   
public UdpDataSink(ServerConfig serverConfig) {
    System.out.println("Opening UDP channel for sending");
    try {
        channel = DatagramChannel.open(StandardProtocolFamily.INET);
        channel.connect(new InetSocketAddress(serverConfig.getInetAddress(), serverConfig.getPort()));
    } catch (IOException e) {
        throw new IllegalStateException("Unable to start UDP channel for sending", e);
    }
}
项目:nls-net-ssdp    文件:SsdpChannel.java   
private DatagramChannel createChannel(NetworkInterface networkIf, InetSocketAddress address, SsdpSelector selector)
        throws IOException {
    DatagramChannel channel = DatagramChannel.open(StandardProtocolFamily.INET)
            .setOption(StandardSocketOptions.SO_REUSEADDR, true)
            .bind(address)
            .setOption(StandardSocketOptions.IP_MULTICAST_IF, networkIf);
    channel.join(SSDP_MCAST_ADDRESS.getAddress(), networkIf);
    channel.configureBlocking(false);
    selector.register(this, channel);
    return channel;
}