Re: Networking Application

On Mon, 6 Nov 2000, Joe Shaw wrote:

> On Mon, 6 Nov 2000, Chris Nystrom x8269 wrote:
> > It looks like I need to use a GLib IO Channel and add it to the main event 
> > loop. Are there any examples of how to use one? Can someone tell me of an
> > application that uses one?
> It's pretty simple. First, create a GIOChannel:

Thanks. I almost got it working. I have a little program kicked off by
inetd that simply prints "hello networking!" that this program connects
to. What is happening is that it keeps repeating over and over instead of
printing just once. It looks like I need a function to flush the IO
channel to tell it I got the data and wait for some new data, but I looked
in the online docs and I did not see one.

Or (more likely) did I do something wrong?



#include <stdio.h>
#include <strings.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <gnome.h>

#define TCP_PORT        32662
#define HOST_ADDR       ""
static gboolean read_data(GIOChannel *channel, GIOCondition cond,
  gpointer data);

main(gint argc, gchar *argv[])
  int                sockfd;
  struct sockaddr_in serv_addr;
  GIOChannel *channel;

  gnome_init("ntest","0.0", argc, argv);

  bzero((char *) &serv_addr, sizeof(serv_addr));
  serv_addr.sin_family	    = AF_INET;
  serv_addr.sin_addr.s_addr = inet_addr(HOST_ADDR);
  serv_addr.sin_port        = htons(TCP_PORT);

  if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0)
    fprintf(stderr, "client: can't open stream socket");

  if (connect(sockfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) < 0)
    fprintf(stderr, "client: can not connect to server");

  channel = g_io_channel_unix_new(sockfd);

  g_io_add_watch(channel, G_IO_IN, read_data, NULL);


  return 0;

static gboolean
read_data(GIOChannel *channel, GIOCondition cond, gpointer data)
  GIOError err;
  char buffer[4096];
  int bytes;

  err = g_io_channel_read(channel, buffer, 4096, &bytes);

  return TRUE;

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