【问题标题】:GIO example for opening a server port?用于打开服务器端口的 GIO 示例?
【发布时间】:2010-01-23 17:13:51
【问题描述】:

能否请您告诉我是否有任何使用 GIO Server Socket 的示例 (我可以打开一个端口并监听套接字请求的那个)? 我想用它来“远程控制”我的 GTK+ 应用程序。

【问题讨论】:

    标签: gtk


    【解决方案1】:

    我认为你应该这样做:

    #define MY_PORT 47110
    
    /* Listener callback, this gets called by GTK+ when
     * there's socket activity to handle.
    */
    static gboolean cb_listener(GIOChannel *source, GIOCondition condition, gpointer data
    {
      switch(condition)
      {
      case G_IO_IN:
        /* There's data to be read. */
        break;
      default:
        /* An error has occured, or socket is closed. */
        return FALSE; /* This tells GIO to remove the source, might be drastic. */
      }
      return TRUE; /* This tells GIO that all is fine. */
    }
    

    然后在其他地方(在一个函数中,可能是main()):

    GSocketListener *listener;
    
    listener = g_socket_listener_new();
    g_socket_listener_add_inet_port(listener, MY_PORT, NULL, NULL);
    g_io_add_watch(G_IO_CHANNEL(listener), G_IO_IN | G_IO_ERR | G_IO_HUP, cb_listener, NULL);
    

    【讨论】:

      猜你喜欢
      • 2013-06-14
      • 2014-08-20
      • 1970-01-01
      • 1970-01-01
      • 2021-06-22
      • 2015-09-08
      • 2016-12-08
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多