GIO 套接字服务器/客户端示例 [英] GIO socket-server / -client example

查看:58
本文介绍了GIO 套接字服务器/客户端示例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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?

推荐答案

我终于设法使用 glib 和 gio 创建了一个简单的服务器和客户端.
我的服务器看起来像这样:

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!
");
  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"
", 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!
");
  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!
");
  }

  /* 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 仍然是新手,所以在使用之前请仔细检查我的代码.

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

这篇关于GIO 套接字服务器/客户端示例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆