FileDocCategorySizeDatePackage
NetworkUtils.javaAPI DocAndroid 1.5 API4951Wed May 06 22:41:54 BST 2009android.net

NetworkUtils

public class NetworkUtils extends Object
Native methods for managing network interfaces. {@hide}

Fields Summary
Constructors Summary
Methods Summary
public static native intaddHostRoute(java.lang.String interfaceName, int hostaddr)
Add a route to the specified host via the named interface.

public static booleanconfigureInterface(java.lang.String interfaceName, DhcpInfo ipInfo)
When static IP configuration has been specified, configure the network interface according to the values supplied.

param
interfaceName the name of the interface to configure
param
ipInfo the IP address, default gateway, and DNS server addresses with which to configure the interface.
return
{@code true} for success, {@code false} for failure

        return configureNative(interfaceName,
            ipInfo.ipAddress,
            ipInfo.netmask,
            ipInfo.gateway,
            ipInfo.dns1,
            ipInfo.dns2);
    
private static native booleanconfigureNative(java.lang.String interfaceName, int ipAddress, int netmask, int gateway, int dns1, int dns2)

public static native intdisableInterface(java.lang.String interfaceName)
Bring the named network interface down.

public static native intgetDefaultRoute(java.lang.String interfaceName)
Return the gateway address for the default route for the named interface.

public static native java.lang.StringgetDhcpError()
Return the last DHCP-related error message that was recorded.

NOTE: This string is not localized, but currently it is only used in logging.

return
the most recent error message, if any

public static intlookupHost(java.lang.String hostname)
Look up a host name and return the result as an int. Works if the argument is an IP address in dot notation. Obviously, this can only be used for IPv4 addresses.

param
hostname the name of the host (or the IP address)
return
the IP address as an {@code int} in network byte order

        InetAddress inetAddress;
        try {
            inetAddress = InetAddress.getByName(hostname);
        } catch (UnknownHostException e) {
            return -1;
        }
        byte[] addrBytes;
        int addr;
        addrBytes = inetAddress.getAddress();
        addr = ((addrBytes[3] & 0xff) << 24)
                | ((addrBytes[2] & 0xff) << 16)
                | ((addrBytes[1] & 0xff) << 8)
                |  (addrBytes[0] & 0xff);
        return addr;
    
public static native booleanreleaseDhcpLease(java.lang.String interfaceName)
Release the current DHCP lease.

param
interfaceName the name of the interface for which the lease should be released
return
{@code true} for success, {@code false} for failure

public static native intremoveDefaultRoute(java.lang.String interfaceName)
Remove the default route for the named interface.

public static native intremoveHostRoutes(java.lang.String interfaceName)
Remove host routes that uses the named interface.

public static native intresetConnections(java.lang.String interfaceName)
Reset any sockets that are connected via the named interface.

public static native booleanrunDhcp(java.lang.String interfaceName, DhcpInfo ipInfo)
Start the DHCP client daemon, in order to have it request addresses for the named interface, and then configure the interface with those addresses. This call blocks until it obtains a result (either success or failure) from the daemon.

param
interfaceName the name of the interface to configure
param
ipInfo if the request succeeds, this object is filled in with the IP address information.
return
{@code true} for success, {@code false} for failure

public static native intsetDefaultRoute(java.lang.String interfaceName, int gwayAddr)
Add a default route for the named interface.

public static native booleanstopDhcp(java.lang.String interfaceName)
Shut down the DHCP client daemon.

param
interfaceName the name of the interface for which the daemon should be stopped
return
{@code true} for success, {@code false} for failure