g_io_add_watch on multiple sockets



Hi,

I'm writing a UDP server which needs to listen to a number of ports that
various clients send datagrams to.  I'm trying to use g_io_add_watch()
to register a callback which gets fired whenever there is something to
read on any of the numerous ports.  So I set up a GIOChannel for each of
the ports and register it using g_io_add_watch().

The problem I've encountered is that my callback only ever gets fired
when data arrives at the *last* port that I register using
g_io_add_watch().  I know for sure that data is being sent to all of the
ports by my clients.

As an example, I've included some code which does the same thing as my
server is trying to do and which shows the problem.  

Is there something obvious I'm doing wrong?

Thanks for any help or suggestions.

Mark


const int             size      = 64 * 1024;
GIOChannel           *udp_sock  = NULL;
GIOChannel           *udp_sock2 = NULL;
guint                 id;
guint                 id2;
int                   sockfd;
int                   sockfd2;
struct sockaddr_in    addr;
struct sockaddr_in    addr2;

/**********************/
/* Set up first port  */
/**********************/
sockfd = socket(PF_INET, SOCK_DGRAM, 0);
setsockopt( sockfd, SOL_SOCKET, SO_RCVBUF, (char*)&size, sizeof(size));


memset((void *)&addr, 0, sizeof(addr));
addr.sin_family      = AF_INET;
addr.sin_addr.s_addr = htonl(INADDR_ANY);
addr.sin_port        = htons(28002);

bind(sockfd, (struct sockaddr *)&addr, sizeof(addr));


/* Make read non-blocking */
fcntl( sockfd, F_SETFL, O_NONBLOCK );

udp_sock = g_io_channel_unix_new(sockfd);
  
g_io_channel_set_line_term(udp_sock, "\0", 1);
      
id = g_io_add_watch(udp_sock, G_IO_IN, udp_sock_receive_cb, NULL);
g_io_channel_unref (udp_sock);

                 
/**********************/
/* Set up second port */
/**********************/
                      
sockfd2 = socket(PF_INET, SOCK_DGRAM, 0);
setsockopt( sockfd2, SOL_SOCKET, SO_RCVBUF, (char*)&size, sizeof(size));


memset((void *)&addr2, 0, sizeof(addr2));
addr2.sin_family      = AF_INET;
addr2.sin_addr.s_addr = htonl(INADDR_ANY);
addr2.sin_port        = htons(28003);

bind(sockfd2, (struct sockaddr *)&addr2, sizeof(addr2));


/* Make read non-blocking */
fcntl( sockfd2, F_SETFL, O_NONBLOCK );

udp_sock2 = g_io_channel_unix_new(sockfd2);
  
g_io_channel_set_line_term(udp_sock2, "\0", 1);
      
id2 = g_io_add_watch(udp_sock2, G_IO_IN, udp_sock_receive_cb, NULL);
g_io_channel_unref (udp_sock2);



-- 
Mark Stevens <mstevens csl co uk>
Concept Systems Ltd




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