GIO套接字服务器/客户端示例

17

我想创建一个通过GIO使用套接字通信的服务器和客户端应用程序。 GSocketService和GSocketClient似乎非常适合这个目的,但不幸的是我找不到一些教程或示例代码(适合GLib,GIO,...新手理解)。 有谁知道一些好的资源或可以在此发布示例代码吗?


你有没有取得任何进展?我也在找同样的东西。除了这个API和这个SO回答,我没有找到其他的资料。 - noisebleed
@noisebleed:是的,我确实取得了进展。其实我不明白为什么我第一次尝试时无法同时创建服务器和客户端。也许我不应该同时学习C、glib和OGRE。 - drakide
1
现在寻找任何gio示例的首选地点是glib存储库的gio/tests目录。那里有一些socket示例。 - dhanushka
2个回答

28

我终于成功使用glib和gio创建了一个简单的服务器和客户端。


我的服务器代码如下:

#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;
}

这是相应的客户端:

#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;
}

请注意,我对glib、gio甚至C语言都还很新,因此在使用之前,请再次检查我的代码。


1
@noisebleed:对于文档的看法也是一样。 - drakide
1
实际上,您可以使用这样一个命令“echo anyword |nc localhost 1500”作为客户端来测试服务器是否正常工作。 - mxi1
1
如果您能够从服务器读取响应,那将非常棒。 - diega
未定义引用至g_io_stream_get_type!求救! - Samuel
1
添加 pkg-config --cflags --libs glib-2.0 gio-2.0 - Samuel
显示剩余2条评论

6

从gio文档中可以看出,来自incoming的回调不应该阻塞。 "处理程序必须启动连接处理,但不得阻塞;实质上必须使用异步操作。"

我在异步版本中遇到了一些连接问题,必须由用户引用连接,否则连接将在回调返回后关闭。

以下是一个不会阻塞的服务器完整示例,基于之前给出的示例:

#include <gio/gio.h>
#include <glib.h>

#define BLOCK_SIZE 1024
#define PORT 2345

struct ConnData {
  GSocketConnection *connection;
  char message[BLOCK_SIZE];
};

void message_ready (GObject * source_object,
    GAsyncResult *res,
    gpointer user_data)
{
  GInputStream *istream = G_INPUT_STREAM (source_object);
  GError *error = NULL;
  struct ConnData *data = user_data;
  int count;

  count = g_input_stream_read_finish (istream,
      res,
      &error);

  if (count == -1) {
    g_error ("Error when receiving message");
    if (error != NULL) {
      g_error ("%s", error->message);
      g_clear_error (&error);
    }
  }
  g_message ("Message was: \"%s\"\n", data->message);
  g_object_unref (G_SOCKET_CONNECTION (data->connection));
  g_free (data);
}

static gboolean
incoming_callback (GSocketService *service,
    GSocketConnection * connection,
    GObject * source_object,
    gpointer user_data)
{
  g_message ("Received Connection from client!\n");
  GInputStream *istream = g_io_stream_get_input_stream (G_IO_STREAM (connection));
  struct ConnData *data = g_new (struct ConnData, 1);

  data->connection = g_object_ref (connection);

  g_input_stream_read_async (istream,
      data->message,
      sizeof (data->message),
      G_PRIORITY_DEFAULT,
      NULL,
      message_ready,
      data);
  return FALSE;
}

int main ()
{
  GSocketService *service;
  GError *error = NULL;
  gboolean ret;

  service = g_socket_service_new ();
  ret = g_socket_listener_add_inet_port (G_SOCKET_LISTENER (service),
      PORT, NULL, &error);

  if (ret && error != NULL)
  {
    g_error ("%s", error->message);
    g_clear_error (&error);
    return 1;
  }

  g_signal_connect (service,
      "incoming",
      G_CALLBACK (incoming_callback),
      NULL);

  g_socket_service_start (service);
  GMainLoop *loop = g_main_loop_new(NULL, FALSE);
  g_main_loop_run(loop);

  /* Stop service when out of the main loop*/
  g_socket_service_stop (service);
  return 0;
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接