g_io_channel and UDP sockets

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



I have an app that talks UDP, and I'd like to use a glib-2.0 main
event loop to handle communication.  (I can handle it myself using
sendto and recvfrom, but I'd like the main event loop to do polling
for me and then call my handler functions.

So I (apparently naively) write the following (with error checking
mostly stripped for this email).


        sock = socket(AF_INET, SOCK_DGRAM, 0);

        memset(&saddr, 0, sizeof(saddr));
        saddr.sin_family = AF_INET;
        saddr.sin_addr.s_addr = htonl(INADDR_ANY); /* anyone can talk to us */
        saddr.sin_port = port;
        ret = bind(sock, (struct sockaddr *)&saddr, sizeof(saddr));

        ioc = g_io_channel_unix_new(sock);
        ret = g_io_add_watch(ioc, G_IO_IN | G_IO_PRI, serve_input_handler, 0);


Good so far, serve_input_handler is called as soon as data appears on
the socket.  Problem, though: it never reads it.


    static gboolean serve_input_handler(GIOChannel *source,
                                        GIOCondition condition,
                                         gpointer data)
    {
        GIOStatus iostat;
        gchar *str_return;
        gsize length;
        GError *err;
        
        iostat = g_io_channel_read_to_end(source,  &str_return, &length, &err);
        if(err) {
            g_warning("Error in serve_input_handler: %s", err->message);
            return TRUE;    /* or FALSE? */
        }


And every time err is non-NULL (and err->message is garbage!).

Anyone know what I'm doing wrong?

I'd be happy to have some io_channel magic read my UDP socket and give
me the packet, or to recover the file descriptor and do it myself.  I
guess I could pass the fd in the user data.

Any suggestions?

Thanks much in advance.

-- 
 Jeff

 Jeff Abrahamson  <http://www.purple.com/jeff/>
 GPG fingerprint: 1A1A BA95 D082 A558 A276  63C6 16BF 8C4C 0D1D AE4B
_______________________________________________

gtk-list@xxxxxxxxx
http://mail.gnome.org/mailman/listinfo/gtk-list

[Index of Archives]     [Touch Screen Library]     [GIMP Users]     [Gnome]     [KDE]     [Yosemite News]     [Steve's Art]

  Powered by Linux