我正在学习使用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.
Run Code Online (Sandbox Code Playgroud)
服务器输出:
# ./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
Run Code Online (Sandbox Code Playgroud)
当前代码:
/*
* server.c
*
* Created on: Mar …Run Code Online (Sandbox Code Playgroud) 我想更改UTF-8字符(在gchar数组中),因此它根据标准获取下一个字符的值.我正在使用glib,我没有看到这样的功能.我正在考虑一个可能的解决方案,但它可能需要更多的努力,当然它不会是最有效的,因为我不太了解编码.有没有可以做到这一点的图书馆?谷歌搜索没有帮助.
GLib文档建议在malloc上使用GLib Slice Allocator:
"对于新编写的代码,建议使用新的g_slice API而不是g_malloc()和朋友,只要对象在其生命周期内没有调整大小,并且在分配时使用的对象大小在释放时仍然可用." - http://developer.gnome.org/glib/unstable/glib-Memory-Slices.html
但实际上g_slice明显快于Windows/Linux malloc(速度足以保证处理大小和GLib预处理器黑客的额外麻烦g_slice_new)?我打算在我的C++程序中使用GLib来处理INIish配置(GKeyFile)并获得对C++中不可用的数据结构的访问GHashTable,因此无论如何GLib依赖都无关紧要.
我正在按照这个过程进行测试,用GHC 7.2.2测试安装gtk2hs和相关套件,但我遇到了一些麻烦.我正在使用Mac OS X Lion,通过自制软件安装的glib 2.30.2和ghc 7.4(圣诞节前的候选版本).我想我的gcc版本可能是相关的
bash-3.2$ gcc --version
i686-apple-darwin11-llvm-gcc-4.2 (GCC) 4.2.1 (Based on Apple Inc. build 5658) (LLVM build 2336.1.00)
Run Code Online (Sandbox Code Playgroud)
在尝试安装glib依赖项时出现问题cabal install gtk.我想单独cabal unpack glib和cabal install,和我得到这个指针偏执的投诉.
bash-3.2$ cabal install
Resolving dependencies...
Configuring glib-0.12.2...
Building glib-0.12.2...
Preprocessing library glib-0.12.2...
System/Glib/hsgclosure.c: In function 'gtk2hs_closure_marshal':
System/Glib/hsgclosure.c:110:0:
warning: passing argument 1 of 'rts_evalIO' from incompatible pointer type
System/Glib/hsgclosure.c:110:0:
error: void value not ignored as it ought to be
Run Code Online (Sandbox Code Playgroud)
相关文件的相关行是
cap=rts_evalIO(CAP rts_apply(CAP (HaskellObj)runIO_closure, …Run Code Online (Sandbox Code Playgroud) 我想在我的c程序中使用哈希表.
我代码:
...
#include <glib.h>
void main(int argc, char **argv)
{
GHashTable *g_hash_table;
...
g_hash_table = g_hash_table_new(g_int_hash, g_int_equal);
...
}
Run Code Online (Sandbox Code Playgroud)
然后我编译:
$ gcc -I/usr/include/glib-2.0
-I/usr/lib/i386-linux-gnu/glib-2.0/include
-lglib-2.0 -o test test.c
Run Code Online (Sandbox Code Playgroud)
或相同的命令:
$ gcc `pkg-config --cflags --libs glib-2.0` -o test test.c
Run Code Online (Sandbox Code Playgroud)
但无论如何,结果是:
test.c: underfined reference to `g_int_equal`
test.c: underfined reference to `g_int_hash`
test.c: underfined reference to `g_hash_table_new`
collect2: ld returned 1 exit status
Run Code Online (Sandbox Code Playgroud)
为什么我不能编译我的程序?我做错了包含glib库吗?
我可以在没有实现D-Bus方法的应用程序启动并运行的情况下调用dbus_g_proxy_new_for_name吗?我不确定这样做是否是好的做法,或者是否通常.
我遇到过他们需要无限循环的代码,他们使用了
GMainLoop *mainloop = NULL;
mainloop = g_main_loop_new (NULL, FALSE);
g_main_loop_run (mainloop);
Run Code Online (Sandbox Code Playgroud)
我怀疑他们为什么不while(true);用于同一目的.GMainLoop内部做了什么,是不是内部相同.如何GMainLoop 提高性能.
我正在从Glib移植到Qt的应用程序.
我确实有很多特定于Gtk的API,但很难在Qt中找到等效的变体.我正在寻找替代方案:
任何的想法?
我想知道如何使用post-messages = TRUE属性从gstreamer multifilesink元素获取回调?在下面的代码中,永不调用my_bus_callback函数。
Multifilesink文档说:如果“ post-messages”属性为TRUE,则在写入每个缓冲区后会发送名为“ GstMultiFileSink”的应用程序消息。
#include <gst/gst.h>
#include <stdio.h>
#include <unistd.h>
// Compilation:
// gcc gst_messages.c -g3 -o gst_messages `pkg-config --cflags --libs gstreamer-0.10`
static GMainLoop *loop;
static gboolean
my_bus_callback (GstBus *bus,
GstMessage *message,
gpointer data)
{
printf("Got %s message\n", GST_MESSAGE_TYPE_NAME (message));
/* we want to be notified again the next time there is a message
* on the bus, so returning TRUE (FALSE means we want to stop watching
* for messages on the bus and our …Run Code Online (Sandbox Code Playgroud) static void ddict_debug(const char* fmt, ...) G_GNUC_PRINTF(1, 2);
Run Code Online (Sandbox Code Playgroud)
我在.c文件中发现了这个,我不明白这一行:是否只有一个函数声明或两个?
这段代码是什么意思?