Re: [gnet] Reading data from a socket



Thanks for the advice.  I see what I was doing wrong now.  
I also re-implemented the code using a GConn as you suggested and it was
easier to deal with.

On Wed, 2004-01-21 at 17:09, Tim Müller wrote:

> Apart from the hopefully obvious stuff [1], your problem seems to be that you 
> are not using the GLib main loop. All the GIOChannel stuff depends on the 
> main loop being run. You have two choices basically: 
> 
> (a) write your program events-based, ie. set up everything at the beginning, 
> then call g_main_loop_run(), and then handle everything as stuff happens, and 
> call g_main_quit() when you want to exit.
> 
> (b) do something like this:
> 
>           while (want_to_run)
>           {
>              while (g_main_context_pending(NULL))
>                 g_main_context_iteration(NULL, FALSE);
> 
>              /* do your own stuff here if you want */
>              g_usleep(500);
>           }
> 
> GIOChannels basically work like this:
> 
>  * you set up an iochannel from a file descriptor (pipe, socket, whatever)
>  * you tell GLib to 'watch' that iochannel for certain events 
>     (stuff to read, stuff to write, error, etc.)
>  * GLib will then call your callback function when one of the events you
>     are watching for has happened on the channel/socket.
>  * You then read from the socket if there is stuff to read, or write if there
>       is stuff to write, etc.
> 
> Attached a (hackish) variation of your program, so you see what I mean.
> 
> You might want to use GConn instead of GTcpSocket anyway, btw. I find the 
> interface much nicer and much less hassle.
> 
> Cheers
> -Tim
> 
> [1] 
>  * you do not check whether gnet_tcp_socket_connect() returned NULL
>  * sizeof(buf) will return sizeof(gchar *) = 4, and not the size that you
>     allocated dynamically with g_malloc()!

-- 
Loren Bandiera <lorenb mmgsecurity com>
MMG Security, Inc.




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