尝试使用GUI程序创建gsocket服务器

时间:2018-05-22 03:29:15

标签: sockets gtk gnome gio

我正在尝试在Raspberry Pi上设置使用GTK的gui服务器。该计划只是一个概念验证演示。 我创建一个套接字,客户端可以连接和发送数据,我在wireshark中看到它。但是在客户端断开连接之前,读取不会完成。我的代码使用g_data_input_stream_read_line_async和g_data_input_stream_read_line_finish。程序显示客户端断开连接后客户端发送的数据。 这是程序的输出(即客户端发送的正确数据)。

g_socket_listener_add_inet_port状态:1

g_signal_connect状态:2(此处等待客户端连接)

建立套接字连接!

在g_data_input_stream_new

之后

在g_data_input_stream_set_newline_type

之后

在g_data_input_stream_read_line_async之后(在客户端连接后等待,并在客户端发送数据后等待)

在g_data_input_stream_read_line_finish之前(在客户端断开连接后显示剩余的输出行)

在g_data_input_stream_read_line_finish

之后

长度:14

0 5 0 4 0 1 0 1 0 6 0 0 0 7

我怀疑自收到数据后我遗漏了一些简单的东西,但是我花了不少时间试图找出丢失的东西。

// compiles with:
// gcc `pkg-config --cflags gtk+-3.0` gtk3serv.c -o gtk3serv `pkg-config --libs gtk+-3.0`


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

static void
print_hello (GtkWidget *widget,
             gpointer   data)
{
  g_print ("Hello World\n");
}

static void
activate (GtkApplication *app,
          gpointer        user_data)
{
  GtkWidget *window;
  GtkWidget *button;
  GtkWidget *button_box;

  window = gtk_application_window_new (app);
  gtk_window_set_title (GTK_WINDOW (window), "Window");
  gtk_window_set_default_size (GTK_WINDOW (window), 200, 200);

  button_box = gtk_button_box_new (GTK_ORIENTATION_HORIZONTAL);
  gtk_container_add (GTK_CONTAINER (window), button_box);

  button = gtk_button_new_with_label ("Hello World");
  g_signal_connect (button, "clicked", G_CALLBACK (print_hello), NULL);
  g_signal_connect_swapped (button, "clicked", G_CALLBACK (gtk_widget_destroy), window);
  gtk_container_add (GTK_CONTAINER (button_box), button);

  gtk_widget_show_all (window);
}

// v<>v<>v<>v<>v<>v<>v<>v<>v<>v<>v

static void on_input_read_finish(GObject      *object,
                                 GAsyncResult *result,
                                 gpointer     user_data)
{
  gchar *clientdata;
  gsize length = -1;
  gsize i;

  g_print("before g_data_input_stream_read_line_finish\n"); // only for debug

  clientdata = g_data_input_stream_read_line_finish(G_DATA_INPUT_STREAM(object), 
                                                    result,
                                                    &length, 
                                                    NULL);
  g_print("after g_data_input_stream_read_line_finish\n"); // only for debug

  g_print("length: %d\n", length); // only for debug

  for(i=0; i<length; i++)
  {
    g_print("%x ", clientdata[i]);
  }
  g_print("\n");

}


gboolean sockconnectionestablished(GSocketService *sockservice,
                                   GSocketConnection *connection,
                                   GObject *source_object,
                                   gpointer user_Data)
{
  char *clientdata;
  gsize length = -1;
  gsize i;

  g_print("socket connection established!\n"); // only for debug

  GDataInputStream *gis = g_data_input_stream_new(g_io_stream_get_input_stream(G_IO_STREAM(connection)));

  g_print("after g_data_input_stream_new\n"); // only for debug

  g_data_input_stream_set_newline_type(G_DATA_INPUT_STREAM(gis), G_DATA_STREAM_NEWLINE_TYPE_ANY);

  g_print("after g_data_input_stream_set_newline_type\n"); // only for debug

  g_data_input_stream_read_line_async(G_DATA_INPUT_STREAM(gis),
                                      G_PRIORITY_DEFAULT,
                                      NULL,
                                      on_input_read_finish,
                                      NULL);
  g_print("after g_data_input_stream_read_line_async\n"); // only for debug

  return 1;
}                                   

// ^<>^<>^<>^<>^<>^<>^<>^<>^<>^<>^

int
main (int    argc,
      char **argv)
{
  GtkApplication *app;
  int status;

// v<>v<>v<>v<>v<>v<>v<>v<>v<>v<>v
  GError *sockerror = NULL;
  GSocketService *sockservice;
  gboolean sockstatus;

  sockservice = g_socket_service_new();

  sockstatus =  g_socket_listener_add_inet_port(G_SOCKET_LISTENER(sockservice),
                                                8888,
                                                NULL,
                                                NULL);

  g_print("g_socket_listener_add_inet_port status: %d\n", sockstatus); // only for debug

  status = g_signal_connect(sockservice,
                   "incoming",
                   G_CALLBACK(sockconnectionestablished),
                   NULL);
  g_print("g_signal_connect status: %d\n", status); // only for debug

// ^<>^<>^<>^<>^<>^<>^<>^<>^<>^<>^

  app = gtk_application_new ("org.gtk.example", G_APPLICATION_FLAGS_NONE);
  g_signal_connect (app, "activate", G_CALLBACK (activate), NULL);
  status = g_application_run (G_APPLICATION (app), argc, argv);
  g_object_unref (app);

  return status;
}

0 个答案:

没有答案