Glib / Gio异步或线程UDP服务器

时间:2015-03-12 14:10:54

标签: c multithreading glib asyncsocket gio

我目前有一个同步UDP应用程序接收消息。

代码:

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

}

所以,我想进行接收操作,非阻塞而不是阻塞。我想让它成为ansynchronous,或者/和线程,这样,同时,我可以做与我想要开发的应用程序相关的其他操作。

但我并没有像我想的那样成功。我尝试使用GLib IO通道,但我无法使其工作。进程正在等待,但只是因为主循环(我不能telnet应用程序)。

代码:

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

}

我是GLib / Gio的初学者,我认为我对IO频道做错了。我想将它作为一个事件添加到主循环中,以便我可以使用我的回调函数。也许有一种更简单的方法可以做到这一点。

此外,我有一个TCP异步和线程服务器正在工作,但我没有找到如何对UDP做同样的事情(使用GThreadedSocketService并创建一个套接字监听器,然后将服务添加到主循环。馅饼与TCP)。

你知道怎么办吗?如果你知道如何做,但只有基本的API套接字,我仍然接受它!感谢。

1 个答案:

答案 0 :(得分:4)

我明白了。

确实非常初学者。因为,当我想测试我的udp应用程序(第二个代码块)时,我使用telnet连接到它并尝试发送消息。但是,我们当然不能telnet udp应用程序......

所以我尝试使用一个简单的udp发送器(顺便使用Glib / Gio)而不是telnet,它工作正常,非阻塞和可重用。我做了一些改变,但基本上,它是一样的。我放了一个空闲函数来向你展示它是如何非阻塞的,这是否有一天可以帮助某人。

我的简单Glib / Gio UDP应用程序,非阻止:

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

代码并不完美,需要做很多优化,但我们可以从中做到很好的事情。如果你想看我的udp发件人,请问。