Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7779225
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T18:35:47+00:00 2026-06-01T18:35:47+00:00

I would like to create a server and client application that communicate via sockets

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T18:35:48+00:00Added an answer on June 1, 2026 at 6:35 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'd like to create a web service that an application server can contact to
I would like to create a small Windows Messenger Client that I can trigger
I would like to create a COM object that runs in an out of
I would like to create a UIBarButtonItem on my iPhone application which has two
I would like to create a stored procedure with parameters that indicate which fields
I have a client application that connects to a server. The server uses hibernate
I am getting started with iPhone application development and would like to create an
I would like to create an exception handler for the specific case that happens
I'm using the TServerSocket component in my Delphi application. I would like to limit
I am using Flex Builder 3 to develop my first application that will communicate

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.