使用 GSocket 接收简单的 UDP 消息
我正在开发一个非常简单的应用程序,需要在指定端口上侦听传入的 UDP 消息,然后将它们显示在 GTK Entry 字段中。我正在尝试根据 GSocket 文档进行工作,但运气不佳。
以下是我的相关函数(省略了错误检查)。我从 main 调用 open_listen_socket ,然后启动一个超时,每隔一秒左右调用 get_incoming_messages 。第一次调用 get_incoming_messages 时,应用程序会因分段错误而崩溃。
我不知道我是否错过了一些愚蠢的东西,或者我使用 GSocket 进行监听的方式是否完全错误,但任何指导将不胜感激。我的截止日期很短,过去几天你们一直在拯救我的屁股!
static void open_listen_socket()
{
GInetAddress *localAddress;
GSocketAddress *localSocketAddress;
localAddress = g_inet_address_new_from_string("127.0.0.1");
guint16 listenPort = atoi(gtk_entry_get_text (GTK_ENTRY (listenPortField)));
localSocketAddress = g_inet_socket_address_new(localAddress, listenPort);
listenSocket = g_socket_new(G_SOCKET_FAMILY_IPV4, G_SOCKET_TYPE_DATAGRAM, 17, NULL);
g_socket_bind (listenSocket, localSocketAddress, FALSE, NULL);
}
static void get_incoming_message()
{
gchar *buffer=NULL;
gsize size=100;
g_socket_receive(listenSocket, buffer, size, NULL, NULL);
if (strlen(buffer) > 0)
{
gtk_entry_set_text (GTK_ENTRY (current_status_message_box), buffer);
}
}
I am working on a very simple app that needs to listen on a designated port for incoming UDP messages, and then display them in a GTK Entry field. I am trying to work from the GSocket documentation, but not having much luck.
Below are my related functions (error checking left out). I call open_listen_socket from main, and then launch a timeout which calls get_incoming_messages every second or so. The first time get_incoming_messages is called, the app crashes with a Segmentation fault.
I don't know if I'm just missing something dumb, or if I'm on completely the wrong track with my usage of the GSocket for listening, but any guidance will be greatly appreciated. I've been on a short deadline and you guys have been saving my butt the past few days!!
static void open_listen_socket()
{
GInetAddress *localAddress;
GSocketAddress *localSocketAddress;
localAddress = g_inet_address_new_from_string("127.0.0.1");
guint16 listenPort = atoi(gtk_entry_get_text (GTK_ENTRY (listenPortField)));
localSocketAddress = g_inet_socket_address_new(localAddress, listenPort);
listenSocket = g_socket_new(G_SOCKET_FAMILY_IPV4, G_SOCKET_TYPE_DATAGRAM, 17, NULL);
g_socket_bind (listenSocket, localSocketAddress, FALSE, NULL);
}
static void get_incoming_message()
{
gchar *buffer=NULL;
gsize size=100;
g_socket_receive(listenSocket, buffer, size, NULL, NULL);
if (strlen(buffer) > 0)
{
gtk_entry_set_text (GTK_ENTRY (current_status_message_box), buffer);
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果不查看文档,对 g_socket_receive 的调用看起来是假的。缓冲区 arg 为 null - 该函数的用途是什么(这可能意味着它将分配自己的缓冲区)。你确定这不是 &buffer 的意思吗?
<编辑>
不,它是一个 char* ,应该至少分配 size 字节。你的是空的。就可以了。
< /编辑>
Without looking at the docs, the call to g_socket_receive looks bogus. The buffer arg is null - what is the function meant to do with that (it could mean it will allocate its own buffer). Are you sure it isn't mean to be &buffer?
< edit>
Nope, it's a char* that is supposed to be allocated with at least size bytes. Yours is null. that will do it.
< /edit >