SocketClient


Object Hierarchy:

GLib.SocketClient GLib.SocketClient GLib.SocketClient GLib.Object GLib.Object GLib.Object->GLib.SocketClient

Description:

[ CCode ( type_id = "g_socket_client_get_type ()" ) ]
[ Version ( since = "2.22" ) ]
public class SocketClient : Object

`GSocketClient` is a lightweight high-level utility class for connecting to a network host using a connection oriented socket type.

You create a `GSocketClient` object, set any options you want, and then call a sync or async connect operation, which returns a [ class@Gio.SocketConnection] subclass on success.

The type of the [class@Gio.SocketConnection] object returned depends on the type of the underlying socket that is in use. For instance, for a TCP/IP connection it will be a [class@Gio.TcpConnection].

As `GSocketClient` is a lightweight object, you don't need to cache it. You can just create a new one any time you need one.

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 (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);
}
}

public static int main (string[] args) {
http_get_sync ("www.google.at");
return 0;
}

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


Namespace: GLib
Package: gio-2.0

Content:

Properties:

Creation methods:

Methods:

Signals:

Inherited Members: