Asynchronously receive GSocket UDP Datagram messagse

I’m writing a voice chat client for Discord and I’m trying to figure out how to asynchronously receive UDP messages using GSocket or GSocketClient (Though GSocketClient documentation says “It doesn’t make sense to specify a type of G_SOCKET_TYPE_DATAGRAM, as GSocketClient is used for connection oriented services.”).

Is there a method of asynchronously listening for UDP messages or should I just use another thread?

Maybe g_socket_listener_accept_socket_async()? Try that.

Alternatively: set the GSocket’s blocking property to FALSE, use g_socket_create_source() to get a GSource, and call g_socket_accept() in the callback if the condition is G_IO_IN. But that’s more effort, and I bet that’s what g_socket_listener_accept_socket_async() will do for you. I might be wrong. Give it a try.

I think that’s intended for the server, not the client.

Indeed, also it only works for accepting connection-oriented sockets, not datagram sockets.

For UDP, I think you’re just supposed to g_socket_new() and then read from it? Use the nonblocking mode, and get a GSource using g_socket_create_source() so that you only need to read when the source is dispatched. No need to mess with threads: that’s so error-prone.


Alright, thanks, that worked.