Problem with GLib Source Poll

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

 



Hi all,
i need to do a TCP server and i want to use GLib functions. This is my code:

=======================
typedef struct
{
    GSource source; 
    GPollFD *PollFD;
} GSourceWrapped;

GSourceFunc *SOCKET_WriteQueuedFrame() {
[cut]
    return TRUE;
}

static gboolean source_prepare(GSource *source,gint *timeout) {
    GSourceWrapped *s = (GSourceWrapped *)source;
    printf("source_prepare fd:%d\n",s->PollFD->fd);
    return FALSE;
}
	    
static gboolean source_check(GSource *source) {
    GSourceWrapped *s = (GSourceWrapped *)source;
    printf("source_check fd:%d\n",s->PollFD->fd);
    if(s->PollFD->revents != 0) {
	return TRUE;
    } else {
	return FALSE;  
    }
}
		      
static gboolean source_dispatch(GSource *source,GSourceFunc callback,gpointer 
user_data) {
    GSourceWrapped *s = (GSourceWrapped *)source;
    printf("source_dispatch fd:%d\n",s->PollFD->fd);
    return TRUE;
}

GSourceFuncs source_funcs = {
    source_prepare,
    source_check,
    source_dispatch,
    NULL
};

GThreadFunc *SOCKET_MainThread(void *arg) {
    
    GMainLoop *gMainLoop;
    GMainContext *gMainContext;
    GSourceWrapped *gSocketSource;
    GSource *gQueueSource;
    GPollFD *gListenerFD;
    guint gSocketID,gQueueID;
    
    /* Main Loop */
    gMainContext = (GMainContext *)g_main_context_new(); /* crea il contesto*/
    gMainLoop = (GMainLoop *)g_main_loop_new(gMainContext,FALSE); /* Crea il 
MAIN Loop */

    gSocketSource = (GSourceWrapped 
*)g_source_new(&source_funcs,sizeof(GSourceWrapped));
    gSocketSource->PollFD = g_new(GPollFD, 1);

    /* Crea il SOCKET di ascolto */
    gSocketSource->PollFD->revents = G_IO_IN | G_IO_ERR | G_IO_HUP;
    gSocketSource->PollFD->fd = CreateListenSocket(SOCKET_PORT,INADDR_ANY);
    if(gSocketSource->PollFD->fd == (int)NULL) {
	g_thread_exit(0);    
    }
    if(listen(gSocketSource->PollFD->fd,10)==-1) {
	g_thread_exit(0);
    }
	
    g_source_add_poll((GSource *)gSocketSource,gSocketSource->PollFD);
    gSocketID = g_source_attach((GSource *)gSocketSource,gMainContext);

    gQueueSource = (GSource *)g_timeout_source_new(200); /* Queue event every 
second */
    g_source_set_callback(gQueueSource,
(GSourceFunc)SOCKET_WriteQueuedFrame,NULL,NULL);    
    gQueueID = g_source_attach(gQueueSource,gMainContext);

    /* Esegui il main loop */
    g_main_loop_run(gMainLoop);
}

int CreateListenSocket(int port,struct sockaddr *bind_to)
{
 	int listener,yes=1;
 	struct sockaddr_in myaddr;
 
 	if((listener = socket(AF_INET, SOCK_STREAM, 0))==-1) 
 	{
 		return (int)NULL;
 	}
 	if(setsockopt(listener, SOL_SOCKET,SO_REUSEADDR, &yes, sizeof(int))==-1)
 	{
  		return (int)NULL;
 	}
 	myaddr.sin_family=AF_INET;
 	myaddr.sin_addr.s_addr=(int)bind_to;
 	myaddr.sin_port=htons(port);
 	memset(&(myaddr.sin_zero),'\0',8);
 	if(bind(listener,(struct sockaddr *)&myaddr,sizeof(myaddr))==-1) 
 	{
  		return (int)NULL; 
 	}
 	return (int)listener;
}

=======================================
I see that all works until i connect to server ! If i use telnet to connect 
the loop "hang".

Someone can help me ? Thanks !

Oz
-- 
----
O-Zone ! No (C) 2005
WEB @ http://www.zerozone.it
BLOG @ http://blog.zerozone.it/user/o-zone
HOBBY @ http://peggy.altervista.org
_______________________________________________

gtk-list@xxxxxxxxx
http://mail.gnome.org/mailman/listinfo/gtk-list

[Index of Archives]     [Touch Screen Library]     [GIMP Users]     [Gnome]     [KDE]     [Yosemite News]     [Steve's Art]

  Powered by Linux