SocketService


Object Hierarchy:

Object hierarchy for SocketService

Description:

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

A SocketService 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 incoming signal is emitted.

A SocketService is a subclass of SocketListener and you need to add the addresses you want to accept connections on with the SocketListener APIs.

There are two options for implementing a network service based on SocketService. The first is to create the service using SocketService and to connect to the incoming signal. The second is to subclass SocketService 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 ThreadedSocketService.

The socket service runs on the main loop of the thread-default context 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: