`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
- public void add_application_proxy (string protocol)
Enable proxy protocols to be handled by the application.
- public SocketConnection connect (SocketConnectable connectable, Cancellable? cancellable = null) throws Error
Tries to resolve the connectable
and make a network connection
to it.
- public async SocketConnection connect_async (SocketConnectable connectable, Cancellable? cancellable = null) throws Error
This is the asynchronous version of
connect.
- public SocketConnection connect_to_host (string host_and_port, uint16 default_port, Cancellable? cancellable = null) throws Error
This is a helper function for
connect.
- public async SocketConnection connect_to_host_async (string host_and_port, uint16 default_port, Cancellable? cancellable = null) throws Error
- public SocketConnection connect_to_service (string domain, string service, Cancellable? cancellable = null) throws Error
Attempts to create a TCP connection to a service.
- public async SocketConnection connect_to_service_async (string domain, string service, Cancellable? cancellable = null) throws Error
- public SocketConnection connect_to_uri (string uri, uint16 default_port, Cancellable? cancellable = null) throws Error
This is a helper function for
connect.
- public async SocketConnection connect_to_uri_async (string uri, uint16 default_port, Cancellable? cancellable = null) throws Error
- public bool get_enable_proxy ()
- public SocketFamily get_family ()
Gets the socket family of the socket client.
- public unowned SocketAddress? get_local_address ()
Gets the local address of the socket client.
- public SocketProtocol get_protocol ()
Gets the protocol name type of the socket client.
- public unowned ProxyResolver get_proxy_resolver ()
- public SocketType get_socket_type ()
Gets the socket type of the socket client.
- public uint get_timeout ()
Gets the I/O timeout time for sockets created by
this.
- public bool get_tls ()
Gets whether this creates TLS connections.
- public TlsCertificateFlags get_tls_validation_flags ()
Gets the TLS validation flags used creating TLS connections via
this.
- public void set_enable_proxy (bool enable)
Sets whether or not this attempts to make
connections via a proxy server.
- public void set_family (SocketFamily family)
Sets the socket family of the socket client.
- public void set_local_address (SocketAddress? address)
Sets the local address of the socket client.
- public void set_protocol (SocketProtocol protocol)
Sets the protocol of the socket client.
- public void set_proxy_resolver (ProxyResolver? proxy_resolver)
- public void set_socket_type (SocketType type)
Sets the socket type of the socket client.
- public void set_timeout (uint timeout)
Sets the I/O timeout for sockets created by this
.
- public void set_tls (bool tls)
Sets whether this creates TLS (aka SSL)
connections.
- public void set_tls_validation_flags (TlsCertificateFlags flags)
Sets the TLS validation flags used when creating TLS connections via
this.