I’m fairly new to C programming and having some difficulties wrapping my brain around the usage of GSocket to send UDP messages.
The application I’m working on is extremely simple. I have GTK_ENTRY fields where the user can enter an IP address, port, and a message, and when they click a button it should send the message via UDP to the address and port specified.
This is my function so far:
static void send_message()
{
GInetAddress *udpAddress;
GSocketAddress *udpSocketAddress;
GSocket *udpSocket;
udpAddress = g_inet_address_new_from_string(gtk_entry_get_text (GTK_ENTRY (ipField)));
guint16 udpPort = atoi(gtk_entry_get_text (GTK_ENTRY (portField)));
udpSocketAddress = g_inet_socket_address_new(udpAddress, udpPort);
udpSocket = g_socket_new(G_SOCKET_FAMILY_IPV4, G_SOCKET_TYPE_DATAGRAM, 17, NULL);
const gchar *myMessage = gtk_entry_get_text (GTK_ENTRY (mainShowCommandField));
g_socket_send (udpSocket, myMessage, sizeof(myMessage), NULL, NULL);
}
The program compiles without errors, but when I trigger the function, the message is not being sent.
I’m sure I’m missing something stupid, but any help would be greatly appreciated!!
You forgot to connect the socket. Do
Before calling
g_socket_send. You also might want to add error checking to your code.