socketsclientgio

GIO socket-server / -client example


I would like to create a server and client application that communicate via sockets using GIO. GSocketService and GSocketClient seem be perfect for this purpose but unfortunately I couldn't find some tutorial or example code (that a GLib, GIO,... newbie can understand). Does anybody know some good resources or can post example code here?


Solution

  • I finally managed to create both a simple server and client using glib and gio.
    My server looks like this:

    #include <glib.h>
    #include <gio/gio.h>
    
    /* this function will get called everytime a client attempts to connect */
    gboolean
    incoming_callback  (GSocketService *service,
                        GSocketConnection *connection,
                        GObject *source_object,
                        gpointer user_data)
    {
      g_print("Received Connection from client!\n");
      GInputStream * istream = g_io_stream_get_input_stream (G_IO_STREAM (connection));
      gchar message[1024];
      g_input_stream_read  (istream,
                            message,
                            1024,
                            NULL,
                            NULL);
      g_print("Message was: \"%s\"\n", message);
      return FALSE;
    }
    
    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;
    }
    

    and this is the corresponding client:

    #include <glib.h>
    #include <gio/gio.h>
    
    int
    main (int argc, char *argv[])
    {
       /* initialize glib */
      g_type_init ();
    
      GError * error = NULL;
    
      /* create a new connection */
      GSocketConnection * connection = NULL;
      GSocketClient * client = g_socket_client_new();
    
      /* connect to the host */
      connection = g_socket_client_connect_to_host (client,
                                                   (gchar*)"localhost",
                                                    1500, /* your port goes here */
                                                    NULL,
                                                    &error);
    
      /* don't forget to check for errors */
      if (error != NULL)
      {
          g_error (error->message);
      }
      else
      {
          g_print ("Connection successful!\n");
      }
    
      /* use the connection */
      GInputStream * istream = g_io_stream_get_input_stream (G_IO_STREAM (connection));
      GOutputStream * ostream = g_io_stream_get_output_stream (G_IO_STREAM (connection));
      g_output_stream_write  (ostream,
                              "Hello server!", /* your message goes here */
                              13, /* length of your message */
                              NULL,
                              &error);
      /* don't forget to check for errors */
      if (error != NULL)
      {
          g_error (error->message);
      }
      return 0;
    }
    

    Note though, that I am still new to glib, gio and even C, so double check my code before using it.