3

我正在学习使用 GLib 编写简单、高效的套接字服务器的基础知识。我正在试验 GSocketService。到目前为止,我似乎只能接受连接,但它们会立即关闭。从文档中我无法弄清楚我错过了哪一步。我希望有人可以为我阐明这一点。

运行以下命令时:

# telnet localhost 4000
Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'.
Connection closed by foreign host.
# telnet localhost 4000
Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'.
Connection closed by foreign host.
# telnet localhost 4000
Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'.
Connection closed by foreign host.

服务器的输出:

# ./server
New Connection from 127.0.0.1:36962
New Connection from 127.0.0.1:36963
New Connection from 127.0.0.1:36965

当前代码:

/*
 * server.c
 *
 *  Created on: Mar 10, 2010
 *      Author: mark
 */
#include <glib.h>
#include <gio/gio.h>

gchar *buffer;

gboolean
network_read(GIOChannel *source,
            GIOCondition cond,
            gpointer data)
{
  GString *s = g_string_new(NULL);
  GError *error;
  GIOStatus ret = g_io_channel_read_line_string(source, s, NULL, &error);
  if (ret == G_IO_STATUS_ERROR)
    g_error ("Error reading: %s\n", error->message);
  else
    g_print("Got: %s\n", s->str);

}

gboolean
new_connection(GSocketService *service,
              GSocketConnection *connection,
              GObject *source_object,
              gpointer user_data)
{
  GSocketAddress *sockaddr = g_socket_connection_get_remote_address(connection, NULL);
  GInetAddress *addr = g_inet_socket_address_get_address(G_INET_SOCKET_ADDRESS(sockaddr));
  guint16 port = g_inet_socket_address_get_port(G_INET_SOCKET_ADDRESS(sockaddr));

  g_print("New Connection from %s:%d\n", g_inet_address_to_string(addr), port);

  GSocket *socket = g_socket_connection_get_socket(connection);

  gint fd = g_socket_get_fd(socket);
  GIOChannel *channel = g_io_channel_unix_new(fd);
  g_io_add_watch(channel, G_IO_IN, (GIOFunc) network_read, NULL);
  return TRUE;
}

int main(int argc, char **argv) {
  g_type_init();
  GSocketService *service = g_socket_service_new();
  GInetAddress *address = g_inet_address_new_from_string("127.0.0.1");
  GSocketAddress *socket_address = g_inet_socket_address_new(address, 4000);
  g_socket_listener_add_address(G_SOCKET_LISTENER(service), socket_address, G_SOCKET_TYPE_STREAM,
          G_SOCKET_PROTOCOL_TCP, NULL, NULL, NULL);

  g_object_unref(socket_address);
  g_object_unref(address);
  g_socket_service_start(service);

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

  GMainLoop *loop = g_main_loop_new(NULL, FALSE);
  g_main_loop_run(loop);
}
4

3 回答 3

5

GSocketConnection 必须在传入回调中引用,这将使连接保持活动状态。您可以将其传递给数据结构、类或作为 user_data 传递给 watch 回调。

gboolean
new_connection(...)
{
  ...

  g_object_ref (connection);
  GSocket *socket = g_socket_connection_get_socket(connection);

  gint fd = g_socket_get_fd(socket);
  GIOChannel *channel = g_io_channel_unix_new(fd);
  // Pass connection as user_data to the watch callback
  g_io_add_watch(channel, G_IO_IN, (GIOFunc) network_read, connection);
  return TRUE;
}

您没有在手表回调 network_read() 中返回,您必须以“return true”结束它。从文档中:“如果应删除事件源,该函数应返回 FALSE ”。

100% CPU 是由于在连接关闭时通道仍处于活动状态。确保在不再需要时正确删除事件源。

gboolean
network_read(GIOChannel *source,
             GIOCondition cond,
             gpointer data)
{
  GString *s = g_string_new(NULL);
  GError *error = NULL;
  GIOStatus ret = g_io_channel_read_line_string(source, s, NULL, &error);

  if (ret == G_IO_STATUS_ERROR) {
    //g_error ("Error reading: %s\n", error->message);
    g_warning ("Error reading: %s\n", error->message);
    // Drop last reference on connection
    g_object_unref (data);
    // Remove the event source
    return FALSE;
  }
  else
    g_print("Got: %s\n", s->str);

  if (ret == G_IO_STATUS_EOF) {
    return FALSE;
  }
于 2010-07-18T05:24:18.700 回答
2

它没有记录在 GSocketService 文档中(我必须通过 GLib 源代码才能找到它),但是调用回调的例程(在这种情况下为 new_connection)*在连接对象*上执行 g_object_unref() 返回后。这有效地立即关闭连接 new_connection() 返回它。

我不知道为什么会这样,但解决方案是在进入回调时添加一个 g_object_ref() :

gboolean
new_connection(GSocketService *service,
              GSocketConnection *connection,
              GObject *source_object,
              gpointer user_data)
{

  g_object_ref(connection);    /* Tell glib not to disconnect */

  GSocketAddress *sockaddr = g_socket_connection_get_remote_address(connection, NULL);
  GInetAddress *addr =
      g_inet_socket_address_get_address(G_INET_SOCKET_ADDRESS(sockaddr));
  guint16 port = g_inet_socket_address_get_port(G_INET_SOCKET_ADDRESS(sockaddr));

如果没有添加,在主循环中轮询文件描述符只会返回 POLLNVAL,因为连接已关闭。在没有针对该结果的处理程序的情况下,它会不断地这样做——这就是导致 100% CPU 负载的原因。

于 2011-10-27T18:01:47.427 回答
1

来自GIO 文档

GIOStream 对象拥有输入和输出流,而不是相反,因此保持子流活动不会保持 GIOStream 对象活动。如果 GIOStream 对象被释放,它将被关闭,从而关闭子流,因此即使子流保持活动状态,它们也将始终为所有操作返回 G_IO_ERROR_CLOSED。

于 2010-05-22T07:11:30.303 回答