[gnet] New to gnet, simple program returns G_IO_STATUS_ERROR



Hi!

I wrote a simple (48 line) program.  Please find it attached.

I don't understand why I get G_IO_STATUS_ERROR when I try to perform
g_io_channel_read_line on the GIOChannel returned from
gnet_tcp_socket_get_io_channel.

>From a previous email I seem to gather that gnet IO channels and glib IO
channels are not entirely unified.  Is this the case ?

The story:

I currently use Unix file descriptors wrapped up in FILE * structures
(using fdopen), but I would like to migrate the whole shebang to
GTcpSocket/GIOChannel, so I may use the same code on both Windoze and
Linux.  I want to continue to be able to treat both sockets and files
using the same functions.

TIA
#include <gnet.h>
#include <stdio.h>

int main (int argc, char *argv[])
  {
  GTcpSocket *sock = NULL, *sock_listen = NULL ;
  GIOChannel *chan = NULL ;
  char *pszLine = NULL ;
  int cb = 0, idxDelim = -1 ;
  GIOStatus status ;

  if (argc < 2)
    {
    fprintf (stderr, "Usage: %s listen|connect\n", argv[0]) ;
    exit (1) ;
    }

  if (!strcmp (argv[1], "listen"))
    {
    sock_listen = gnet_tcp_socket_server_new_with_port (10123) ;
    sock = gnet_tcp_socket_server_accept (sock_listen) ;
    chan = gnet_tcp_socket_get_io_channel (sock) ;
    status = g_io_channel_read_line (chan, &pszLine, &cb, &idxDelim, NULL) ;
    fprintf (stderr, "status:%s\n%s", 
      G_IO_STATUS_NORMAL == status ? "G_IO_STATUS_NORMAL" :
      G_IO_STATUS_ERROR == status ? "G_IO_STATUS_ERROR" :
      G_IO_STATUS_AGAIN == status ? "G_IO_STATUS_AGAIN" : "G_IO_STATUS_EOF", 
      G_IO_STATUS_NORMAL == status ? pszLine : "<nothing>\n") ;
    gnet_tcp_socket_delete (sock_listen) ;
    }
  else
  if (!strcmp (argv[1], "connect"))
    {
    pszLine = "Abracadabra\n" ;
    sock = gnet_tcp_socket_connect ("localhost", 10123) ;
    chan = gnet_tcp_socket_get_io_channel (sock) ;
    g_io_channel_write_chars (chan, pszLine, strlen (pszLine), &cb, NULL) ;
    }
  else
    {
    fprintf (stderr, "Usage: %s listen|connect\n", argv[0]) ;
    exit (1) ;
    }

  gnet_tcp_socket_delete (sock) ;

  return 0 ;
  }


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]