glib IO Channels


 //gcc -o glib_io_channel glib_io_channel.c `pkg-config  --cflags --libs glib-2.0`

#include <glib.h>
#include <stdio.h>
#include <string.h>

gboolean read_in1(GIOChannel *gio, GIOCondition condition, gpointer data)
{
	GIOStatus ret;  
	GError *err = NULL;  
	gchar *msg = NULL;  
	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);  

	g_printf ("Read %u bytes: %s\n", len, msg);  

	g_free (msg); //free msg

	return TRUE;  
}


gboolean read_in(GIOChannel *gio, GIOCondition condition, gpointer data)
{
	g_printf("data = %p\n",data);
	int fd = g_io_channel_unix_get_fd(gio);
	char buff[1024];
	memset(buff,0,sizeof(buff));
	
	int readn = read(fd,buff,sizeof(buff));  

	g_printf ("Read %u bytes: %s", readn, buff);  

	return TRUE;  
}

int test(GSource *source,GMainContext *context)
{
	int i = 101;
	g_printf("pinter %p\n",&i);
	g_source_set_callback(source, (GSourceFunc)read_in, &i, NULL);
	g_source_attach(source, context);
	
}
int main(void)
{
	GSource *source = NULL;
	GIOChannel *channel = NULL;
    GMainLoop *loop = g_main_loop_new(NULL, TRUE);
    GMainContext *context = g_main_loop_get_context(loop);
	int stdin_fd = fileno(stdin);
	g_printf("stdin_fd = %d\n",stdin_fd);
	/* Create new channel to watch stdin */
	channel = g_io_channel_unix_new(stdin_fd);
	source = g_io_create_watch(channel, G_IO_IN);
 
	/* Set callback to be called when stdin is readable */
	test(source,context);
	g_printf("1111111111\n");
	
    g_main_loop_run(loop);
 
    g_main_context_unref(context);
    g_main_loop_unref(loop);
 
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值