Re: Sockets



On Mon, Aug 13, 2001 at 05:29:46PM +0200 Johan wrote:

Hello,
How is the best way to handle sockets in a gtk application ?
Is there any signals I can use call a function when a socket
receives data or gets an connection ?


There is a gdk way for doing that:

  socket->input =
      gdk_input_add(socket->fd, GDK_INPUT_READ,
                    GTK_SIGNAL_FUNC(read_function), socket);

socket is a struct defined by myself.

The read_function might look like this:

gint read_function(gpointer data, gint source,
                   GdkInputCondition condition)
{
  socket_t *socket;
  unsigned char buffer[1024];
  int res;
  int cnt;
  int i1;
  unsigned long size;

  socket = data;

  if (condition != GDK_INPUT_READ) {
    /* internal function that closes the socket and removes the gdk_input */
    socket_end(socket);
    return 1;
  }

  switch ((res = recv(source, buffer, 1024-1, 0))) {  /* source == socket->fd */
  case -1:  /* error */
    socket_end(socket);
    return 1;
  case 0:  /* closed by peer */
    socket_end(socket);
    return 1;
  default:
    break;
  }
}

The read_function is called by gtk if there is data available, or there is
an error, or the connection was closed by the peer.

Markus.




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