How to use GIOChannel to read an Unix socket
- From: silverburgh <silverburgh meryl gmail com>
- To: gtk-list gnome org
- Subject: How to use GIOChannel to read an Unix socket
- Date: Sun, 24 Jan 2010 00:02:49 -0800
Hi,
I create a GIOChannel which wraps around a socket on linux:
GIOChannel* channel = g_io_channel_unix_new(sock);
g_io_add_watch(channel, G_IO_IN,
			gio_read_socket, NULL));
g_io_add_watch(channel, G_IO_HUP,
			gio_close_socket, NULL));
And my function gio_read_socket() does get called whenever there is
data available on the socket. And I am able to retrieve data using
g_io_channel_read_line ().
But when the other side of the socket get closed().  My function
g_io_channel_read_line() get called infinite number of times with the
number of data read = 0. Can you please tell me how can I fix my
problem? And my function gio_close_socket() which registered for
G_IO_HUP were never get called.
static gboolean
gio_read_socket (GIOChannel *gio, GIOCondition condition, gpointer data)
{
       printf (" gio_read_socket \n");
        GIOStatus ret;
        GError *err = NULL;
        gchar *msg;
        gsize len;
        ret = g_io_channel_read_line (gio, &msg, &len, NULL, &err);
        if (ret == G_IO_STATUS_ERROR)
                g_error ("Error reading: %s\n", err->message);
        printf ("Read %u bytes: %s\n", len, msg);
        g_free (msg);
        // Try to close the socket when nothing is there.
        if (len == 0) {
              close(g_io_channel_unix_get_fd(gio));
	      g_io_channel_close(gio);
        }
        return TRUE;
}
Thank you.
[
Date Prev][
Date Next]   [
Thread Prev][
Thread Next]   
[
Thread Index]
[
Date Index]
[
Author Index]