public SocketConnection connect (SocketConnectable connectable, Cancellable? cancellable = null) throws Error

Tries to resolve the connectable and make a network connection to it.

Upon a successful connection, a new SocketConnection is constructed and returned. The caller owns this new object and must drop their reference to it when finished with it.

The type of the SocketConnection object returned depends on the type of the underlying socket that is used. For instance, for a TCP/IP connection it will be a TcpConnection.

The socket created will be the same family as the address that the connectable resolves to, unless family is set with set_family or indirectly via set_local_address. The socket type defaults to g_socket_type_stream but can be set with set_socket_type.

If a local address is specified with set_local_address the socket will be bound to this address before connecting.

Example: Socket client, sync:

public static void http_get_sync (string host) {
try {
// Resolve hostname to IP address:
Resolver resolver = Resolver.get_default ();
List<InetAddress> addresses = resolver.lookup_by_name (host, null);
InetAddress address = addresses.nth_data (0);

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

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

// 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);

public static int main (string[] args) {
http_get_sync ("");
return 0;

valac --pkg gio-2.0 GLib.SocketClient.connect.vala



a SocketClient.


a SocketConnectable specifying the remote address.


optional Cancellable object, null to ignore.


a SocketConnection on success, null on error.