Re: how to read data with g_io_read_channel throwed it through a normal socket
- From: James Scott Jr <skoona verizon net>
- To: nahuel9728 <nahuelsgk hotmail com>
- Cc: gtk-app-devel-list gnome org
- Subject: Re: how to read data with g_io_read_channel throwed it through a normal socket
- Date: Thu, 12 Apr 2007 23:43:36 -0400
On Thu, 2007-04-12 at 13:36 -0700, nahuel9728 wrote:
Hi everyone. Im developing a interface for a server program and I have some
problems.Step by step:
- I put the server in listen:
fd_dealer2server=socket......
bind....
listen(fd_dealer2server,MAX_CON)
- I create a channel and I add a watch to the the port binded for everytime
a data goes in
io = g_io_channel_unix_new (fd_dealer2server);
if (!g_io_add_watch (io, G_IO_IN, (GIOFunc) my_func, NULL)){g_error ....}
-Tll here everything its ok.... Later in my_func:
static void my_func (GIOChannel *gio, GIOCondition condition, gpointer
data){
guint mensaje[1];
gsize bytes_read;
if(g_io_channel_read (gio, (gchar *) mensaje , sizeof(mensaje) , &
bytes_read) != G_IO_ERROR_NONE){
g_warning("Error\n");
}
And Finally I have my client binded to the shame port....
fd=socket
connect(fd,
int sms = atoi(argv[2]);
send( fd, &sms, sizeof(int) ,0);
It catch the event G_IO_IN but g_io_read doesnt works..Always get error. Any
hint???
Regards to every1, Nahuel
Nahuel,
You need to perform the socket accept() before the
g_io_channel_unix_new() is called.
The http://gfhcmd.sourceforge.net/ package contains a gfhcmd server
module and the gfhcmc contains its matching client; I offer them as a
example of using g_io_channel... with sockets and gtk/glib.
Here is an example routine that picks up where you left off (at the
listen). You should find the select(), then accept(), then the
g_io_channel_unix_new() calls.
[CODE]
/*
* Accept a TCP connection, waiting for upto 80 seconds.
* Returns NULL on error, or connected GIOChannel
*
*/
extern GIOChannel *sknet_net_accept(PSKCOMM psk)
{
socklen_t clilen = sizeof(struct sockaddr_in);
int newfd;
struct sockaddr_in client_addr;
GIOChannel *ioc = NULL;
gint rc = 0, ecount = 0;
fd_set fdset;
struct timeval timev, *tv = NULL;
g_return_val_if_fail (psk != NULL, NULL);
/*
* Wait on an incoming connection
*/
ecount = 0;
tv = &timev;
rc = 0;
while ( rc <= 0) {
timev.tv_sec = 8;
timev.tv_usec = 0;
FD_ZERO(&fdset);
FD_SET(psk->fd_server, &fdset);
if ( ecount > 10 ) {
g_snprintf(psk->ch_error_msg, sizeof(psk->ch_error_msg),
"accept select(failed), retry=%d {%s}",
ecount, g_strerror(errno));
psk->ioc = NULL;
return NULL;
}
rc = select(psk->fd_server+1, &fdset, NULL, NULL, tv);
if ( rc == 0 ) { continue; } /* timed out */
if ( rc == -1 ) {
g_snprintf(psk->ch_error_msg, sizeof(psk->ch_error_msg),
"tv.sec=%d, retrying=%d {%s}",
(int)timev.tv_sec, ecount, g_strerror(errno));
sknet_util_log_msg("sknet_net_accept", "accept select()
failing",
psk->ch_error_msg);
g_usleep(1000000);
} /* error */
ecount++;
} /* end-while */
/*
* Prepare to accept the connection
*/
memset (&client_addr, 0, sizeof(struct sockaddr_in));
newfd = accept(psk->fd_server, (struct sockaddr *)&client_addr,
&clilen);
if (newfd < 0) {
sknet_util_log_msg("Net_Accept issue ", "newfd<0", (gchar
*)g_strerror(errno));
g_snprintf(psk->ch_error_msg,
sizeof(psk->ch_error_msg),"sknet_net_accept(%s)",
(gchar *) g_strerror (errno));
psk->ioc = NULL;
return NULL; /* error */
}
/*
* get session partners name */
getnameinfo ((struct sockaddr *)&client_addr, clilen,
psk->ch_ip_client, sizeof(psk->ch_ip_client),
psk->ch_ip_client_port, sizeof(psk->ch_ip_client_port),
NI_NUMERICSERV);
ioc = g_io_channel_unix_new( newfd);
g_io_channel_set_encoding (ioc, NULL, NULL);
g_io_channel_set_buffered (ioc, FALSE);
psk->ioc = ioc;
return ioc;
}
[/CODE]
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]