Re: g_io_channel and UDP sockets



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


Sven



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