Re: Networking Application

On Mon, 6 Nov 2000, Chris Nystrom x8269 wrote:

> It looks like I need to use a GLib IO Channel and add it to the main event 
> loop. Are there any examples of how to use one? Can someone tell me of an
> application that uses one?

It's pretty simple. First, create a GIOChannel:

	int fd;
	GIOChannel *channel;

	channel = g_io_channel_unix_new(fd);

Then, attach a watch to it:

	g_io_add_watch(channel, G_IO_IN, read_data, NULL);

G_IO_IN is the condition that you want to watch on. It means that the
callback (in this case "read_data") will be called anytime there is data
to read from the channel. The NULL is the standard user data that you pass
to callbacks.

And finally, write the callback function:

	static gboolean read_data(GIOChannel *channel, GIOCondition cond,
				  gpointer data)
		GIOError err;
		char buffer[4096];
		int bytes;

		err = g_io_channel_read(channel, buf, 4096, &bytes);


Hope this helps.


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