Re: gtk_input_add - code



In app_run() - initialization routine generated by Glade:
# Open some socket (or pipe with some command like `tail -f interesting_file`)
   $interesting_socket = IO::Socket::INET->new(
      Proto => 'tcp',
      PeerAddr => $some_address,
      PeerPort => $some_permitted_port         );
   if(!$interesting_socket){
      carp "ERROR *** Could not open $some_address:$some_permitted_port!\n";
#      return(-1);
   }
   else{
      # Look, I can write to interesting_socket.
      $interesting_socket->syswrite("0 I have connected.\n");
   }

# Tell input_add to watch this file handle
      $retval = Gtk::Gdk->input_add($interesting_socket->fileno, ['read'],
       \&socket_handler, $interesting_socket);


Sub to call when there is something to read from handle:
sub socket_handler{
   my ($fd_ptr, $fd, $widget) = @_; 

   # Read at most 1k bytes from socket, the rest stays there till our next 
   # read.
   $length = $fd_ptr->sysread($in_str, 1024);    
   if(!$len){
      carp "Remote end appears to have disconnected!\n";
#      return(-2);
   } 

    # Do something with the message - split at newlines, etc.
   
   return 0;
}
    




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