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