2

I have currently a synchronous UDP application receiving messages.

The code :

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

#include <glib.h>
#include <gio/gio.h>


int main(argc,argv)
int argc;
char ** argv;{


char buf[256], *ptr, sep[] = "| ";
GError * error = NULL;


  GSocket * socket;
  GSocketAddress *gsockAddr, *gfromAddr;
  guint16 udp_port = 1500;
  //Creates socket udp ipv4
  socket = g_socket_new(G_SOCKET_FAMILY_IPV4,
                    G_SOCKET_TYPE_DATAGRAM,
                    G_SOCKET_PROTOCOL_UDP,
                    &error);
  g_assert(error == NULL);

  if (socket == NULL) {
    g_print("ERROR");
    exit(1);
  }
  //sockaddr struct like
  gsockAddr = G_SOCKET_ADDRESS(g_inet_socket_address_new(g_inet_address_new_any(G_SOCKET_FAMILY_IPV4), udp_port));

  if(gsockAddr == NULL){
    g_error("Error socket\n");
    exit(1);
  }
//
if (g_socket_bind (socket, gsockAddr, TRUE, NULL) == FALSE){

  g_print("Error bind\n");
  exit(1);

}

int bytes = g_socket_receive_from (socket,
                       &gfromAddr,
                       buf,
                       255,
                       NULL,
                       &error);

if (bytes == -1) {
  g_warning ("Failed to receive from socket: %s", error->message);
  g_error_free (error);
  return TRUE;
}

g_message("Server receive: %s", buf);


guint16 port = g_inet_socket_address_get_port(G_INET_SOCKET_ADDRESS(gfromAddr));

g_print("...from    %s(%d)\n",g_inet_address_to_string(g_inet_socket_address_get_address(G_INET_SO    CKET_ADDRESS(gfromAddr))), (int) port);

exit(0);

}

So, I want to make the receive operation, non-blocking instead of blocking. I want to make it either ansynchronous, or/and threaded so that, meanwhile, I could do other operations related to the application I want to develop.

But I did not suceed to make it like I want. I tried to use GLib IO Channels, but I can not make it works. The processus is waiting, but only because of the Main Loop (I can not telnet the application).

The code :

#include <gio/gio.h>
#include <glib.h>

#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

#define BLOCK_SIZE 1024

static gboolean
gio_read_socket (GIOChannel     *channel,
                 GIOCondition    condition,
                 gpointer        data)
{

    char buf[1024];
    gsize bytes_read;
    GError *error = NULL;

    if (condition & G_IO_HUP) return FALSE; /* this channel is done */

    g_io_channel_read_chars (channel, buf, sizeof (buf), &bytes_read,
            &error);
    g_assert (error == NULL);

    buf[bytes_read] = '\0';

    g_print ("%s", buf);

    return TRUE;
}

int
main (int argc, char **argv)
{

  GSocket * s_udp;
  GError *err = NULL;
  guint16 udp_port = 5556;

  s_udp = g_socket_new(G_SOCKET_FAMILY_IPV4,
                    G_SOCKET_TYPE_DATAGRAM,
                    G_SOCKET_PROTOCOL_UDP,
                    &err);
  g_assert(err == NULL);

  if (s_udp == NULL) {
    g_print("ERROR");
    exit(1);
  }

  g_socket_bind(s_udp,
               G_SOCKET_ADDRESS(g_inet_socket_address_new(g_inet_address_new_any(G_SOCKET_FAMILY_IPV4), udp_port)),
              TRUE,
              &err);

  g_assert(err == NULL);

  int fd = g_socket_get_fd(s_udp);
  GIOChannel* channel = g_io_channel_unix_new(fd);
  guint source = g_io_add_watch(channel, G_IO_IN,
                              (GIOFunc) gio_read_socket, NULL);


  g_io_channel_unref(channel);

  GMainLoop *loop = g_main_loop_new(NULL, FALSE);

  g_main_loop_run(loop);
  g_main_loop_unref(loop);

}

I am quite a beginner with GLib/Gio, and I think I am doing wrong with the IO Channels. I would like to add it to the main loop as an event, so that I could use my callback function. Maybe there is a simpler way to do that.

Besides, I have a TCP asynchronous and threaded server that is working, but I did not find how to do the same with UDP (using a GThreadedSocketService and creating a socket listener, then adding the service to the main loop. Easy as pie with TCP).

Do you have any idea how to proceed ? If you know how to do but only with the basic API socket, I still take it ! Thanks.

Acrasidae
  • 71
  • 1
  • 7
  • There are some extra spaces in your code eg `G_SOCKET_FAM ILY_IPV4` – davmac Mar 12 '15 at 14:16
  • Yes, indeed, thanks, I just failed my Copy/Paste, but the first code block totally works. I "just" want to make it non-blocking. – Acrasidae Mar 12 '15 at 14:48
  • I don't know a lot about glib, but I suspect you need to set your channel to be unbuffered (https://developer.gnome.org/glib/stable/glib-IO-Channels.html#g-io-channel-set-buffered). You almost certainly have to put the file descriptor into non-blocking mode (`fcntl(soc, F_SETFL, O_NONBLOCK)`, though technically you should get the flags, set the O_NONBLOCK flag, and set the new flags). – davmac Mar 12 '15 at 15:19
  • See also http://stackoverflow.com/questions/9513327/gio-socket-server-client-example – davmac Mar 12 '15 at 15:22
  • Thanks for the answer, if I used the basic API socket and iocntl function, I would probably have done that, but with Glib/Gio, I am suddenly confused. Anyway I did find how to make it works, see below. Thanks again for your concern. – Acrasidae Mar 12 '15 at 16:11

1 Answers1

5

I figure it out.

I am indeed quite a beginner. Because, when I wanted to test my udp application (the second code block), I used telnet to connect to it and try to send messages. However, we can not telnet udp applications of course...

So I tried with a simple udp sender (I used Glib/Gio for it by the way) instead of telnet and it worked, perfectly non-blocking and reusable. I did make some changes but basically, it is the same. I put an idle function to show you how non-blocking it is, whether this can help someone one day.

My simple Glib/Gio UDP app, non blocking :

#include <gio/gio.h>
#include <glib.h>

#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

#define BLOCK_SIZE 1024

static gboolean
gio_read_socket (GIOChannel     *channel,
                 GIOCondition    condition,
                 gpointer        data)
{

    char buf[1024];
    gsize bytes_read;
    GError *error = NULL;

    if (condition & G_IO_HUP) return FALSE; /* this channel is done */

    g_io_channel_read_chars (channel, buf, sizeof (buf), &bytes_read,
            &error);
    g_assert (error == NULL);

    buf[bytes_read] = '\0';

    g_print ("%s", buf);

    int *a = data;
    *a = *a + 1;

    return TRUE;
}

gboolean
idleCpt (gpointer user_data){

   int *a = user_data;
   g_print("%d\n", *a);
   sleep(1);

   return TRUE;
}
int
main (int argc, char **argv)
{

  GSocket * s_udp;
  GError *err = NULL;
  int idIdle = -1, dataI = 0;
  guint16 udp_port = 1505;
  GSocketAddress * gsockAddr = G_SOCKET_ADDRESS(g_inet_socket_address_new(g_inet_address_new_any(G_SOCKET_FAMILY_IPV4), udp_port));
  s_udp = g_socket_new(G_SOCKET_FAMILY_IPV4,
                    G_SOCKET_TYPE_DATAGRAM,
                    G_SOCKET_PROTOCOL_UDP,
                    &err);
  g_assert(err == NULL);

  if (s_udp == NULL) {
    g_print("ERREUR");
    exit(1);
  }


  if (g_socket_bind (s_udp, gsockAddr, TRUE, NULL) == FALSE){

   g_print("Erreur bind\n");
   exit(1);

  }

  g_assert(err == NULL);

  int fd = g_socket_get_fd(s_udp);

  GIOChannel* channel = g_io_channel_unix_new(fd);
  guint source = g_io_add_watch(channel, G_IO_IN,
                              (GIOFunc) gio_read_socket, &dataI);

  g_io_channel_unref(channel);


   GMainLoop *loop = g_main_loop_new(NULL, FALSE);
   idIdle = g_idle_add(idleCpt, &dataI);
   g_main_loop_run(loop);
}

The code is not perfect, there is a lot of optimisations to make, but we can do nice things from that I think. If you want to see my udp sender, just ask.

Acrasidae
  • 71
  • 1
  • 7