异步 GIO 服务器/客户端

Posted

技术标签:

【中文标题】异步 GIO 服务器/客户端【英文标题】:Asynchronous GIO Server/Client 【发布时间】:2014-08-20 00:02:47 【问题描述】:

我想创建一个使用 GIO 通过套接字进行通信的异步服务器和客户端应用程序。由于我是 GIO 的新手,因此在浏览时我得到了以下代码 - 这是我的客户。我找不到服务器的任何示例。请在这方面指导我。

GMainLoop *loop;
GMainContext *ctx;

struct conn

    GSocketClient *client;

    GSocketConnection *conn;
    GInputStream *in;
    GOutputStream *out;

    gchar data[8192];
    unsigned int count;
;

static void
read_done_cb(GObject *source_object, GAsyncResult *res, gpointer user_data)


    printf("## %s\n", __FUNCTION__);

    struct conn *c = (struct conn *)user_data;
    gssize len = g_input_stream_read_finish(c->in, res, NULL);

    g_input_stream_read_async(c->in, c->data, sizeof c->data / sizeof *c->data, G_PRIORITY_DEFAULT, NULL, read_done_cb, c);
    if (c->count++ == 1) 
        printf("End of life as I know it...\n");
        g_main_loop_quit(loop);
    


static void
write_done_cb(GObject *source_object, GAsyncResult *res, gpointer user_data)



static void
connect_done_cb(GObject *source_object, GAsyncResult *res, gpointer user_data)

    printf("## %s\n", __FUNCTION__);

    struct conn *c = (struct conn *)user_data;
    c->conn = g_socket_client_connect_to_host_finish(c->client, res, NULL);

    printf("I'm\n");
    c->in  = g_io_stream_get_input_stream(G_IO_STREAM (c->conn));
    c->out = g_io_stream_get_output_stream(G_IO_STREAM (c->conn));

    char *data = "hello world!!!\n";

    printf("I'm here\n");

    g_output_stream_write_async(c->out, data, strlen(data), G_PRIORITY_DEFAULT, NULL, write_done_cb, c);
    g_input_stream_read_async(c->in, c->data, sizeof c->data / sizeof *c->data, G_PRIORITY_DEFAULT, NULL, read_done_cb, c);


int
main(int argc, char **argv)

    g_type_init();

    struct conn *c = g_malloc0(sizeof *c);
    ctx = g_main_context_new();
    loop = g_main_loop_new(ctx, FALSE);
    g_main_context_push_thread_default(ctx);

    c->client = g_socket_client_new();
    g_socket_client_connect_to_host_async(c->client, "localhost", 1500, NULL, connect_done_cb, c);

    g_main_loop_run(loop);

    g_io_stream_close(G_IO_STREAM(c->conn), NULL, NULL);
    g_object_unref(c->client);
    g_object_unref(c->conn);
    g_main_context_pop_thread_default(ctx);
    g_main_loop_unref(loop);
    g_main_context_unref(ctx);

    return 0;

【问题讨论】:

【参考方案1】:

希望对你有帮助

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

【讨论】:

以上是关于异步 GIO 服务器/客户端的主要内容,如果未能解决你的问题,请参考以下文章

如何使 D-Bus 服务器端调用异步?

使用 GIO 的异步 HTTP 请求

GIO socket-server / -client 示例

在 GIO 中,为啥这些异步文件 IO 操作永远不会完成? (适用于 C 和 Vala)

在 python 中使用 gio 访问 samba 共享

Open*** v3 Dbus 客户端未接收到所有信号