0

I have got below sample from stackoverflow itself.

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

gchar *buffer;

gboolean
network_write(GIOChannel *source,
            GIOCondition cond,
            gpointer data)
{
    return TRUE;
}
gboolean
network_read(GIOChannel *source,
            GIOCondition cond,
            gpointer data)
{
    GString *s = g_string_new(NULL);
    GError *error;
    g_print("Inside network_read function\n");
    GIOStatus ret = g_io_channel_read_line_string(source, s, NULL, &error);
    if (ret == G_IO_STATUS_ERROR)
        g_error ("Error reading: %s\n", error->message);
    else
        g_print("Got: %s\n", s->str);

    return TRUE;
}

gboolean
new_connection(GSocketService *service,
              GSocketConnection *connection,
              GObject *source_object,
              gpointer user_data)
{
  GSocketAddress *sockaddr = g_socket_connection_get_remote_address(connection, NULL);
  GInetAddress *addr = g_inet_socket_address_get_address(G_INET_SOCKET_ADDRESS(sockaddr));
  guint16 port = g_inet_socket_address_get_port(G_INET_SOCKET_ADDRESS(sockaddr));

  g_print("New Connection from %s:%d\n", g_inet_address_to_string(addr), port);

  GSocket *socket = g_socket_connection_get_socket(connection);

  gint fd = g_socket_get_fd(socket);
g_print("Naseeb fd: %d\n", fd);
  GIOChannel *channel = g_io_channel_unix_new(fd);
  if(!g_io_add_watch(channel, G_IO_IN, (GIOFunc) network_read, NULL))
  {
      g_print("Got Error while adding network_read\n");
  }
//  g_io_add_watch(channel, G_IO_OUT, (GIOFunc) network_write, NULL);
  return TRUE;
}

int main(int argc, char **argv) {
    g_type_init();
    GSocketService *service = g_socket_service_new();
    GInetAddress *address = g_inet_address_new_from_string("0.0.0.0");
    GSocketAddress *socket_address = g_inet_socket_address_new(address, 3001);
    g_socket_listener_add_address(G_SOCKET_LISTENER(service), socket_address, G_SOCKET_TYPE_STREAM,
            G_SOCKET_PROTOCOL_TCP, NULL, NULL, NULL);

    g_object_unref(socket_address);
    g_object_unref(address);
    g_socket_service_start(service);

    g_signal_connect(service, "incoming", G_CALLBACK(new_connection), NULL);
    g_socket_service_start(service);

    GMainLoop *loop = g_main_loop_new(NULL, FALSE);
    g_main_loop_run(loop);
    return 0;
}

Using this code, i am able to get the client connected to server. I confirm the same using message printed in the function new_connection

But when i send data from client, callback network_read never gets called at server. Although client side, send() API return value shows total bytes sent.

1) Is there any api missing at server side. 2) What is proper way to invoke network_write ?

1 Answers1

0

You shouldn't be using GIOChannel, new_connection() already has a connection and you can just use stream = g_io_stream_get_input_stream (G_IO_STREAM (connection)); to get a GInputStream to read from.

TingPing
  • 2,129
  • 1
  • 12
  • 15
  • Thanks for the reply. I tried using API mentioned but still no data received. `GInputStream *stream = g_io_stream_get_input_stream(G_IO_STREAM(connection)); gsize read_size; gboolean ret = g_input_stream_read_all(stream, recv_buf, 13, &read_size, &cancel, &err); g_print("Naseeb g_input_stream_read_all return value: %d:%d\n", ret, read_size);` –  Jul 21 '17 at 06:43
  • Also, i want to make my application as tcp server which `accept()` connections from client and read data from sockets whenever data is written on socket in async way. –  Jul 21 '17 at 06:48