ghenry has asked for the wisdom of the Perl Monks concerning the following question:

Hi Monks,

I am just messing with Gtk2 and I was trying to get running the examples on builder.com from Build complex GUI applications with Gtk2-Perl

The error is:

Usage: init(class=NULL) at ./client.pl line 35.

Which is:

Gtk2->init(\@ARGV);

What does it mean?

The server.pl is:

#!/usr/bin/perl # server1.pl - a simple server use strict; use Socket; # use port 7890 as default my $port = shift || 7890; my $proto = getprotobyname('tcp'); # create a socket, make it reusable socket(SERVER, PF_INET, SOCK_STREAM, $proto) or die "socket: $!"; setsockopt(SERVER, SOL_SOCKET, SO_REUSEADDR, 1) or die "setsock: $!"; # grab a port on this machine my $paddr = sockaddr_in($port, INADDR_ANY); # bind to a port, then listen bind(SERVER, $paddr) or die "bind: $!"; listen(SERVER, SOMAXCONN) or die "listen: $!"; print "SERVER started on port $port\n"; $| = 1; # for each connection... my $client_addr; while ($client_addr = accept(CLIENT, SERVER)) { # find out who connected my ($client_port, $client_ip) = sockaddr_in($client_addr); my $client_ipnum = inet_ntoa($client_ip); my $client_host = gethostbyaddr($client_ip, AF_INET); # tell who connected print "got a connection from: $client_host", " [$client_ipnum]\n"; # send them a message, close connection my $w = `w`; # log the access event print CLIENT "$w\n "; close CLIENT; }

The client, which has the problem, is:

#! /usr/bin/perl # client-gui.pl - a simple socket client GUI use strict; use Socket; use Gtk2; # initialize some variables my $port = 7890; # hard coded port the server runs on # we could have made this an argument instead # used by the hbox, vbox and widget packing my $homogenous = 0; # a "1" here would make all # widgets the same height or width my $spacing = 0; # spacing between widgets in pixels my $expand = 1; # widgets expand to fit available space my $fill = 0; # extra space used by widgets or empty my $padding = 0; # no padding # build the GUI Gtk2->init(\@ARGV); my $window = Gtk2::Window->new('toplevel'); $window->set_title("Network 'w' Monitor GUI"); $window->set_border_width(6); $window->set_size_request(400, 180); my $vbox = Gtk2::VBox->new($homogenous, $spacing); my $hbox = Gtk2::HBox->new($homogenous, $spacing); my $label_host = Gtk2::Label->new("Hostname to poll:"); my $entry_host = Gtk2::Entry->new; my $text_result = Gtk2::TextView->new; my $scrolled_window = Gtk2::ScrolledWindow->new(undef, undef); $scrolled_window->set_policy('automatic', 'automatic'); $scrolled_window->add($text_result); my $buffer = $text_result->get_buffer; $text_result->set_editable(0); my $button_poll = Gtk2::Button->new_with_label("Poll"); my $button_exit = Gtk2::Button->new_with_label("Exit"); $hbox->pack_start($label_host, $expand, $fill, $padding); $hbox->pack_start($entry_host, $expand, $fill, $padding); $vbox->pack_start($hbox, $expand, $fill, $padding); $vbox->pack_start($scrolled_window, $expand, $fill, $padding); $vbox->pack_start($button_poll, $expand, $fill, $padding); $vbox->pack_start($button_exit, $expand, $fill, $padding); Gtk2::GSignal->connect($button_poll,"clicked", \&poll_host); Gtk2::GSignal->connect($button_exit,"clicked", \&exit_app); $window->add($vbox); $window->show_all(); # Gtk2 event loop Gtk2->main(); exit 0; # exit routine sub exit_app { Gtk2->quit(); return 0; } # poll routine sub poll_host { my $host = $entry_host->get_text; if ($host eq '') { gui_err("Empty Host Name!"); return; } my $buff = ''; my $proto = getprotobyname('tcp'); # get the port address my $iaddr = inet_aton($host); my $paddr = sockaddr_in($port, $iaddr); # create the socket, connect to the port socket(SOCKET, PF_INET, SOCK_STREAM, $proto) or gui_err("socket: $ +!"); connect(SOCKET, $paddr) or gui_err("connect: $!"); while (<SOCKET>) { $buff .= $_; } close SOCKET or gui_err("close: $!"); $buffer->set_text($buff, -1) if ($buff ne ''); } # error feedback sub gui_err { my $msg = shift; $buffer->set_text($msg, -1); }

I haven't changed a thing from the main article.

Thanks.

Walking the road to enlightenment... I found a penguin and a camel on the way.....
Fancy a yourname@perl.me.uk? Just ask!!!

Replies are listed 'Best First'.
Re: Messing with Gtk2
by zentara (Cardinal) on Apr 08, 2005 at 13:05 UTC
    The problem with
    Gtk2->init( \@ARGV );
    is that it expects you to have something in @ARGV, i.e. command line options. Just change it to
    Gtk2->init();

    But that is just the start of your problems. This is old Gtk2 code from 2003, and Gtk2 has changed ALOT in the last few years. So, assuming you have the latest version, here are a couple more "fixes" needed to get it to run.

    $there is no longer a GSignal !! #Gtk2::GSignal->connect( $button_poll, "clicked", \&poll_host ); #Gtk2::GSignal->connect( $button_exit, "clicked", \&exit_app ); #SHOULD BE $button_poll->signal_connect("clicked", \&poll_host ); $button_exit->signal_connect("clicked", \&exit_app );

    and

    sub exit_app { #Gtk2->quit(); Gtk2->main_quit(); return 0; }

    and the set_text needs fixing

    #$buffer->set_text( $buff, -1 ) if ( $buff ne '' ); $buffer->set_text( $buff ) if ( $buff ne '' ); .... .... #$buffer->set_text( $msg, -1 ); $buffer->set_text( $msg);

    Those fixes will get you running.


    I'm not really a human, but I play one on earth. flash japh

      That did the job. Thanks a lot.

      I can now start playing and adapting the code!!

      Walking the road to enlightenment... I found a penguin and a camel on the way.....
      Fancy a yourname@perl.me.uk? Just ask!!!