Re: g_io_channel and UDP sockets



On Tue, Feb 24, 2004 at 07:15:30PM +0100, Sven Neumann wrote:
>   [39 lines, 175 words, 1301 characters]  Top characters: _enoiald
> 
> Hi,
> 
> Jeff Abrahamson <jeff purple com> writes:
> 
> > 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);
> 
> You will need to unset the io channel's encoding (it defaults to UTF-8):
> 
>  g_io_channel_set_encoding (ioc, NULL, NULL);
> 
> If you are looking for some example code that does UDP with
> GIOChannels, take a look at BSender and BReceiver in Blib:
> 
>   http://sven.gimp.org/blinkenlights/blib-1.1.5.tar.gz

Thanks for the tip and the source code.

Setting the encoding unfortunately just gives me a different error
stream.  Now I see an infinite stream of these:

    (process:12739): GLib-CRITICAL **: file giochannel.c: line 1662 (g_io_channel_read_to_end): assertion `(error == NULL) || (*error == NULL)' failed

I get this by calling

    GError *err;
    iostat = g_io_channel_read_to_end(source,  &str_return, &length, &err);



I note that in blib you do the same work-around I had settled on:

    req_fd = g_io_channel_unix_get_fd (io);
    buf_read = recvfrom (req_fd, buf, sizeof (buf), 0,
                        (struct sockaddr *) &req_addr, &req_addr_length);

;-)

Maybe that's the way I should do it, since I have that working.  I was
mostly curious if glib could somehow save me from having to know quite
so much about sockets.  But it's no big loss, as I already know so
much about sockets...

-- 
 Jeff

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



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