accept
Description:
Blocks waiting for a client to connect to any of the sockets added to the listener.
Returns a SocketConnection for the socket that was accepted.
If source_object
is not null it will be filled out with the source object specified when the
corresponding socket or address was added to the listener.
If cancellable
is not null, then the operation can be cancelled by triggering the cancellable
object from another thread. If the operation was cancelled, the error g_io_error_cancelled will be returned.
Example: Socket Listener, sync:
Use telnet localhost 1024
or telnet localhost 1025
to create connections
Send "shutdown" to close the server.
public class Source : Object {
public uint16 port { private set; get; }
public Source (uint16 port) {
this.port = port;
}
}
public class Worker : Object {
private SocketConnection connection;
private Cancellable cancellable;
private Source source;
public Worker (SocketConnection connection, Source source, Cancellable cancellable) {
this.cancellable = cancellable;
this.connection = connection;
this.source = source;
}
public int run () {
try {
print ("Thread started (Port: %d)\n", this.source.port);
// Wait for a message:
DataInputStream istream = new DataInputStream (this.connection.input_stream) ;
string message = istream.read_line (null, this.cancellable);
print ("Received: %s\n", message);
stdout.flush ();
// Response:
OutputStream ostream = this.connection.output_stream;
ostream.write (message.data, this.cancellable);
ostream.write ("\n".data, this.cancellable);
if (message._strip () == "shutdown") {
this.cancellable.cancel ();
}
} catch (IOError e) {
print ("IOError: %s\n", e.message);
}
return 0;
}
}
public static int main (string[] args) {
try {
SocketListener listener = new SocketListener ();
// Used to shutdown the program:
Cancellable cancellable = new Cancellable ();
// Listen on port 1024 and 1025.
// Source is used as source-identifier.
listener.add_inet_port (1024, new Source (1024));
listener.add_inet_port (1025, new Source (1025));
SocketConnection? connection = null;
Object? source = null;
// Wait for connections:
while ((connection = listener.accept (out source, cancellable)) != null) {
// Spawn a thread for each request:
Worker worker = new Worker (connection, source as Source, cancellable);
new Thread<int>.try (null, () => { return worker.run (); });
connection = null;
}
} catch (Error e) {
print ("Error: %s\n", e.message);
}
return 0;
}
valac --pkg gio-2.0 GLib.SocketListener.accept.vala --target-glib=2.32
Parameters:
this | |
source_object |
location where Object pointer will be stored, or null |
cancellable |
optional Cancellable object, null to ignore. |
Returns:
a SocketConnection on success, null on error. |