0

I want to implement a GIO socket client server program where the server can serve up to say 5 clients at the same time. Is this even possible? How can I modify the following standard server to allow multiple clients to connect in a thread safe way? The incoming_callback() function will receive data from a client and do some processing and respond an acknowledgement and this will continue till the client sends an "exit" message at which point the server will close the client connection. I want to do this for a max of say 5 connections at a time for the server. Is it possible and if so how?

int
main (int argc, char **argv)
{
/* initialize glib */
  g_type_init();

  GError * error = NULL;

  /* create the new socketservice */
  GSocketService * service = g_socket_service_new ();

  /* connect to the port */
  g_socket_listener_add_inet_port ((GSocketListener*)service,
                                1500, /* your port goes here */
                                NULL,
                                &error);

  /* don't forget to check for errors */
  if (error != NULL)
  {
     g_error (error->message);
  }

  /* listen to the 'incoming' signal */
  g_signal_connect (service,
                   "incoming",
                   G_CALLBACK (incoming_callback),
                   NULL);

 /* start the socket service */
 g_socket_service_start (service);

 /* enter mainloop */
 g_print ("Waiting for client!\n");
 GMainLoop *loop = g_main_loop_new(NULL, FALSE);
 g_main_loop_run(loop);
 return 0;
}
user2399453
  • 2,930
  • 5
  • 33
  • 60

1 Answers1

0

The magic should happen within your incoming_callback, returning as fast as possible and pushing the work into another GThread (or even better, a GThreadPool)

drahnr
  • 6,782
  • 5
  • 48
  • 75