InetAddress


Object Hierarchy:

Object hierarchy for InetAddress

Description:

public class InetAddress : Object

InetAddress represents an IPv4 or IPv6 internet address.

Use lookup_by_name or lookup_by_name_async to look up the InetAddress for a hostname. Use lookup_by_address or lookup_by_address_async to look up the hostname for a InetAddress.

To actually connect to a remote host, you will need a InetSocketAddress (which includes a InetAddress as well as a port number).

Example: InetAddress:

public static int main (string[] args) {
Resolver resolver = Resolver.get_default ();

// Example 1:
InetAddress address1 = new InetAddress.from_bytes ({208, 80, 152, 201}, SocketFamily.IPV4);

try {
// Output: ``wikipedia-lb.pmtpa.wikimedia.org`` (Wed Oct 24, 20012)
string hostname = resolver.lookup_by_address (address1, null);
print ("host: %s\n", hostname);
} catch (Error e) {
print ("Error: %s\n", e.message);
}


// Example 2:
InetAddress address2 = new InetAddress.from_string ("208.80.152.201");

try {
// Output: ``wikipedia-lb.pmtpa.wikimedia.org`` (Wed Oct 24, 20012)
string hostname = resolver.lookup_by_address (address2, null);
print ("host: %s\n", hostname);
} catch (Error e) {
print ("Error: %s\n", e.message);
}


// Example 3:
try {
// Resolve hostname to IP address:
string host = "www.valadoc.org";
List<InetAddress> addresses = resolver.lookup_by_name (host, null);
InetAddress address4 = addresses.nth_data (0);

// Connect:
SocketClient client = new SocketClient ();
SocketConnection conn = client.connect (new InetSocketAddress (address4, 80));

// Send HTTP GET request
string message = @"GET / HTTP/1.1\r\nHost: %s\r\n\r\n".printf (host);
conn.output_stream.write (message.data);

// Receive response
DataInputStream response = new DataInputStream (conn.input_stream);
string status_line = response.read_line (null).strip ();
print ("Received status line: %s\n", status_line);
} catch (Error e) {
print ("Error: %s\n", e.message);
}

return 0;
}

valac --pkg gio-2.0 GLib.InetAddress.vala


Namespace: GLib
Package: gio-2.0

Content:

Properties:

Creation methods:

Methods:

Inherited Members: