Gjs: Gio.IOErrorEnum: Invalid socket, not initialized

When trying to use a Gio.Socket, I see this error. Simplified code to demonstrate the issue is:

#!/usr/bin/env gjs
const { Gio } = imports.gi;
var socket = new Gio.Socket({ "family":Gio.SocketFamily.IPV4, "type":Gio.SocketType.DATAGRAM, "protocol":Gio.SocketProtocol.UDP});
var sa = Gio.InetSocketAddress.new_from_string('192.168.8.128', 48995);
socket.bind(sa, true);

The problem is in socket.bind() - commenting it out removes the error.

The full error is:

chris@d8:~/src/devicefinder-gjs$ ./utest.js 

(gjs:113277): Gjs-CRITICAL **: 19:13:42.766: JS ERROR: Gio.IOErrorEnum: Invalid socket, not initialized
@./utest.js:5:8


(gjs:113277): Gjs-CRITICAL **: 19:13:42.766: Script ./utest.js threw an exception

Could someone point out the obvious mistake please?

Thanks,
chris

As the error indicates, the socket is not initialized. Gio.Socket implements the Gio.Initable interface, so you have to initialize it after construction with the init() method.

Thanks Romain, that removed the error.

Although the documentation does say that Gio.Socket inherits from Gio.Initable, I had overlooked it - something I’ll pay more attention to in the future.

chris

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.