SocketService


Object Hierarchy:

GLib.SocketService GLib.SocketService GLib.SocketService GLib.SocketListener GLib.SocketListener GLib.SocketListener->GLib.SocketService GLib.Object GLib.Object GLib.Object->GLib.SocketListener

Description:

[ CCode ( type_id = "g_socket_service_get_type ()" ) ]
[ Version ( since = "2.22" ) ]
public class SocketService : SocketListener

A `GSocketService` is an object that represents a service that is provided to the network or over local sockets.

When a new connection is made to the service the [signal@Gio.SocketService:GSocketService:incoming] signal is emitted.

A `GSocketService` is a subclass of [class@Gio.SocketListener] and you need to add the addresses you want to accept connections on with the [ class@Gio.SocketListener] APIs.

There are two options for implementing a network service based on `GSocketService`. The first is to create the service using [ ctor@Gio.SocketService.new] and to connect to the [signal@Gio.SocketService:GSocketService:incoming] signal. The second is to subclass `GSocketService` and override the default signal handler implementation.

In either case, the handler must immediately return, or else it will block additional incoming connections from being serviced. If you are interested in writing connection handlers that contain blocking code then see [class@Gio.ThreadedSocketService].

The socket service runs on the main loop of the thread-default context (see [method@GLib.MainContext.push_thread_default]) of the thread it is created in, and is not threadsafe in general. However, the calls to start and stop the service are thread-safe so these can be used from threads that handle incoming clients.

Example: Socket service:

Note:

Use telnet localhost 1024 or telnet localhost 1025 to create connections

Note:

Send "shutdown" to close the server.

public class Source : Object {
public uint16 port { private set; get; }

public Source (uint16 port) {
this.port = port;
}
}

public async void worker_func (SocketConnection connection, Source source, Cancellable cancellable) {
try {
DataInputStream istream = new DataInputStream (connection.input_stream);
DataOutputStream ostream = new DataOutputStream (connection.output_stream);

// Get the received message:
string message = yield istream.read_line_async (Priority.DEFAULT, cancellable);
message._strip ();
print ("Received: %s\n", message);

// Response:
ostream.put_string (message, cancellable);
ostream.put_byte ('\n', cancellable);

if (message == "shutdown") {
cancellable.cancel ();
}
} catch (Error e) {
print ("Error: %s\n", e.message);
}
}

public static int main (string[] args) {
try {
MainLoop loop = new MainLoop ();

// Create a new SocketService:
SocketService service = new SocketService ();

// Listen on port 1024 and 1025.
// Source is used as source-identifier.
service.add_inet_port (1024, new Source (1024));
service.add_inet_port (1025, new Source (1025));

// Used to shutdown the program:
Cancellable cancellable = new Cancellable ();
cancellable.cancelled.connect (() => {
service.stop ();
loop.quit ();
});

service.incoming.connect ((connection, source_object) => {
Source source = source_object as Source;

print ("Accepted! (Source: %d)\n", source.port);
worker_func.begin (connection, source, cancellable);
return false;
});

service.start ();
loop.run ();
} catch (Error e) {
print ("Error: %s\n", e.message);
}
return 0;
}

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

All known sub-classes:

Namespace: GLib
Package: gio-2.0

Content:

Properties:

Creation methods:

Methods:

Signals:

Inherited Members: