[jdev] gmainloop and listening on a port

Tim Fulcher - Clickatell tim.fulcher at clickatell.com
Tue Aug 2 08:57:17 CDT 2005


Hi all
 
I've been at this a few days now and getting frustrated with gmainloop.
I'm by no means an gcc expert but I'm trying to integrate a socket
listener into a component that I've hacked out of yahoo-transport. This
component needs to perform an http post (which I've gotten right using
Curl) and needs to listen on an another port for a callback.
 
Much googling and I managed to get an app that would listen going using
gmainloop and g_io_add_watch, but when this app was intgrated into my
hacked main() of yahoo-transport it would either post or listen but not
both.
 
Here's the abridged main() code:
 
int main(int argc, char *argv[]) {
  extern char *optarg;
  extern int optind, opterr, optopt;
  int inBackground = 0;
  int rc, pid, c, childpid;
  int message_mask_set = 0;
  int message_stderr_set = 0;
  int fd = 0, fdlimit = 0;
  struct sigaction act;
  FILE *pid_stream;
  struct stat st;
  char *config_file = NULL;
  pool p;
  GMainLoop     *gmain;   /* the receive packet event loop */
    
  jcr = (jcr_instance)malloc(sizeof(_jcr_instance));
 
  g_thread_init(NULL);
  
  ///////////////////////////////////////
    //Lots of stuff cut out here//
    ///////////////////////////////////////
 
  sigemptyset(&act.sa_mask);
  sigaddset(&act.sa_mask, SIGTERM);
  sigaddset(&act.sa_mask, SIGINT);
  sigaddset(&act.sa_mask, SIGKILL);
  act.sa_handler = jcr_server_shutdown;
//act.sa_restorer = NULL;
  act.sa_flags = 0;
 
  sigaction(SIGINT, &act, NULL);
  sigaction(SIGTERM, &act, NULL);
  sigaction(SIGKILL, &act, NULL);
 
  p = pool_new();
  jcr->jcr_i = (instance)pmalloc(p, sizeof(instance));
  jcr->jcr_i->p = p;
  jcr->jcr_i->id = pstrdup(p,
xmlnode_get_data(xmlnode_get_tag(jcr->config,"host")));
 
  /* The component call */
  yahoo_transport(jcr->jcr_i, NULL);
 
  jcr->fd = -1;
  
  while(jcr_socket_connect()) {
    sleep(2);
  }
  
  Set_Up_Listener();
  
  listen_sock = g_io_channel_unix_new(sock);
  g_io_add_watch(listen_sock, G_IO_IN, read_sock, NULL);
  
  log_warn(JDBG, "Main loop starting.");
  jcr_main_new_stream();
  g_main_loop_run(gmain);
  log_warn(JDBG, "Main loop exiting.");
 
  return 0;
}
 
Here's The code for Set_Up_Listener:
 
void Set_Up_Listener()
{
 printf("Setting up listener\n");
 
 //get a socket
  if ((sock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP)) == -1) {
    perror("socket");
    exit(1);
  }
 
  //make it reusable
  if (setsockopt(sock,SOL_SOCKET,SO_REUSEADDR,&sockopt_on,sizeof(int))
== -1) {
    perror("setsockopt");
    exit(1);
  }
 
  //first zero the struct
  memset((char *) &my_addr, 0, sa_in_size);
 
  //now fill in the fields we need
  my_addr.sin_family = PF_INET;
  my_addr.sin_port = htons(LISTENPORT);
  my_addr.sin_addr.s_addr = htonl(INADDR_ANY);
 
  //bind our socket to the port
  if (bind(sock,(struct sockaddr *)&my_addr, sa_in_size) == -1) {
    perror("bind");
    exit(1);
  }
 
  //start listening for incoming connections
  if (listen(sock,BACKLOG) == -1) {
    perror("listen");
    exit(1);
  }
}
 
and finally the code for read_sock():
 
static void read_sock(gpointer data)
{
 char *servername;
 char  response[80];
 
 conn = accept(sock, (struct sockaddr *)&client_addr, &sa_in_size);
        
 if (conn == -1) {
  perror("accept");
  exit(1);
 }
 
 //log the connecter
 servername = (char*)inet_ntoa(client_addr.sin_addr);
 
 //get the reply
 if (recv(conn, &response, 80, 0) == -1) {
   perror("recv");
 }
  
 printf("The client [%s] says \"%s\"\n",servername, &response);
  
 close(conn);
}
 
So if anybody has the slightest inkling of how I can get this going I
would be most appreciative.
 
Kind Regards

Tim Fulcher



More information about the JDev mailing list