Java Code Examples for sun.net.util.IPAddressUtil#toScopedAddress()

The following examples show how to use sun.net.util.IPAddressUtil#toScopedAddress() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: AbstractPlainSocketImpl.java    From Bytecoder with Apache License 2.0 6 votes vote down vote up
/**
 * Creates a socket and connects it to the specified address on
 * the specified port.
 * @param address the address
 * @param port the specified port
 */
protected void connect(InetAddress address, int port) throws IOException {
    // recording this.address as supplied by caller before calling connect
    this.address = address;
    this.port = port;
    if (address.isLinkLocalAddress()) {
        address = IPAddressUtil.toScopedAddress(address);
    }

    try {
        connectToAddress(address, port, timeout);
        isConnected = true;
        return;
    } catch (IOException e) {
        // everything failed
        close();
        throw e;
    }
}
 
Example 2
Source File: AbstractPlainDatagramSocketImpl.java    From Bytecoder with Apache License 2.0 5 votes vote down vote up
/**
 * Binds a datagram socket to a local port.
 */
protected synchronized void bind(int lport, InetAddress laddr)
    throws SocketException {
    if (laddr.isLinkLocalAddress()) {
        laddr = IPAddressUtil.toScopedAddress(laddr);
    }
    bind0(lport, laddr);
}
 
Example 3
Source File: AbstractPlainDatagramSocketImpl.java    From Bytecoder with Apache License 2.0 5 votes vote down vote up
/**
 * Sends a datagram packet. The packet contains the data and the
 * destination address to send the packet to.
 * @param p the packet to be sent.
 */
protected void send(DatagramPacket p) throws IOException {
    InetAddress orig = p.getAddress();
    if (orig.isLinkLocalAddress()) {
        InetAddress scoped = IPAddressUtil.toScopedAddress(orig);
        if (orig != scoped) {
            p = new DatagramPacket(p.getData(), p.getOffset(),
                                   p.getLength(), scoped, p.getPort());
        }
    }
    send0(p);
}
 
Example 4
Source File: AbstractPlainDatagramSocketImpl.java    From Bytecoder with Apache License 2.0 5 votes vote down vote up
/**
 * Connects a datagram socket to a remote destination. This associates the remote
 * address with the local socket so that datagrams may only be sent to this destination
 * and received from this destination.
 * @param address the remote InetAddress to connect to
 * @param port the remote port number
 */
protected void connect(InetAddress address, int port) throws SocketException {
    if (address.isLinkLocalAddress()) {
        address = IPAddressUtil.toScopedAddress(address);
    }
    connect0(address, port);
    connectedAddress = address;
    connectedPort = port;
    connected = true;
}
 
Example 5
Source File: AbstractPlainSocketImpl.java    From Bytecoder with Apache License 2.0 5 votes vote down vote up
/**
 * Creates a socket and connects it to the specified port on
 * the specified host.
 * @param host the specified host
 * @param port the specified port
 */
protected void connect(String host, int port)
    throws UnknownHostException, IOException
{
    boolean connected = false;
    try {
        InetAddress address = InetAddress.getByName(host);
        // recording this.address as supplied by caller before calling connect
        this.address = address;
        this.port = port;
        if (address.isLinkLocalAddress()) {
            address = IPAddressUtil.toScopedAddress(address);
        }

        connectToAddress(address, port, timeout);
        connected = true;
    } finally {
        if (!connected) {
            try {
                close();
            } catch (IOException ioe) {
                /* Do nothing. If connect threw an exception then
                   it will be passed up the call stack */
            }
        }
        isConnected = connected;
    }
}
 
Example 6
Source File: AbstractPlainSocketImpl.java    From Bytecoder with Apache License 2.0 5 votes vote down vote up
/**
 * Creates a socket and connects it to the specified address on
 * the specified port.
 * @param address the address
 * @param timeout the timeout value in milliseconds, or zero for no timeout.
 * @throws IOException if connection fails
 * @throws  IllegalArgumentException if address is null or is a
 *          SocketAddress subclass not supported by this socket
 * @since 1.4
 */
protected void connect(SocketAddress address, int timeout)
        throws IOException {
    boolean connected = false;
    try {
        if (address == null || !(address instanceof InetSocketAddress))
            throw new IllegalArgumentException("unsupported address type");
        InetSocketAddress addr = (InetSocketAddress) address;
        if (addr.isUnresolved())
            throw new UnknownHostException(addr.getHostName());
        // recording this.address as supplied by caller before calling connect
        InetAddress ia = addr.getAddress();
        this.address = ia;
        this.port = addr.getPort();
        if (ia.isLinkLocalAddress()) {
            ia = IPAddressUtil.toScopedAddress(ia);
        }
        connectToAddress(ia, port, timeout);
        connected = true;
    } finally {
        if (!connected) {
            try {
                close();
            } catch (IOException ioe) {
                /* Do nothing. If connect threw an exception then
                   it will be passed up the call stack */
            }
        }
        isConnected = connected;
    }
}
 
Example 7
Source File: AbstractPlainSocketImpl.java    From Bytecoder with Apache License 2.0 5 votes vote down vote up
/**
 * Binds the socket to the specified address of the specified local port.
 * @param address the address
 * @param lport the port
 */
protected synchronized void bind(InetAddress address, int lport)
    throws IOException
{
   synchronized (fdLock) {
        if (!closePending && !isBound) {
            NetHooks.beforeTcpBind(fd, address, lport);
        }
    }
    if (address.isLinkLocalAddress()) {
        address = IPAddressUtil.toScopedAddress(address);
    }
    socketBind(address, lport);
    isBound = true;
}
 
Example 8
Source File: Net.java    From Bytecoder with Apache License 2.0 5 votes vote down vote up
static void bind(ProtocolFamily family, FileDescriptor fd,
                 InetAddress addr, int port) throws IOException
{
    boolean preferIPv6 = isIPv6Available() &&
        (family != StandardProtocolFamily.INET);
    if (addr.isLinkLocalAddress()) {
        addr = IPAddressUtil.toScopedAddress(addr);
    }
    bind0(fd, preferIPv6, exclusiveBind, addr, port);
}
 
Example 9
Source File: Net.java    From Bytecoder with Apache License 2.0 5 votes vote down vote up
static int connect(ProtocolFamily family, FileDescriptor fd, InetAddress remote, int remotePort)
    throws IOException
{
    if (remote.isLinkLocalAddress()) {
        remote = IPAddressUtil.toScopedAddress(remote);
    }
    boolean preferIPv6 = isIPv6Available() &&
        (family != StandardProtocolFamily.INET);
    return connect0(preferIPv6, fd, remote, remotePort);
}