异步GIO服务器/客户端

时间:2014-06-29 12:47:47

标签: c asynchronous client-server gio

我想创建一个使用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 个答案:

答案 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;
}