打开服务器端口的GIO示例?

时间:2010-01-23 17:13:51

标签: gtk

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

1 个答案:

答案 0 :(得分:2)

我认为你应该这样做:

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