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
SocketClient is a lightweight high-level utility class for connecting to a network host using a connection
oriented socket type.
You create a SocketClient object, set any options you want, and then call a sync or async connect operation,
which returns a SocketConnection subclass on success.
The type of the 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
TcpConnection .
As SocketClient 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 \n Host: %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
Content:
Properties:
Creation methods:
Methods:
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 .
Signals:
Inherited Members:
All known members inherited from class GLib.Object
@get
@new
@ref
@set
add_toggle_ref
add_weak_pointer
bind_property
connect
constructed
disconnect
dispose
dup_data
dup_qdata
force_floating
freeze_notify
get_class
get_data
get_property
get_qdata
get_type
getv
interface_find_property
interface_install_property
interface_list_properties
is_floating
new_valist
new_with_properties
newv
notify
notify_property
ref_count
ref_sink
remove_toggle_ref
remove_weak_pointer
replace_data
replace_qdata
set_data
set_data_full
set_property
set_qdata
set_qdata_full
set_valist
setv
steal_data
steal_qdata
thaw_notify
unref
watch_closure
weak_ref
weak_unref