See : shrek-donkey chat and it's Gtk2 tutorial

Here is the only example of a client server I have, where the client uses Gtk2. I didn't write this, but it's very good and basic. As far as a chat server goes, programming Gtk2 is similar to Tk, except that instead of using a fileevent to watch the socket, you use a Gtk2::Helper ( read "perldoc Gtk2::Helper") Also, ask on the Perl/Gtk2 maillist where more Perl/Gtk2 programmers read daily.

#!/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; }
#!/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 ); Gtk2->init(); 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 ); $button_poll->signal_connect("clicked", \&poll_host ); $button_exit->signal_connect("clicked", \&exit_app ); #$button->signal_connect( "clicked", sub { Gtk->exit( 0 ); } ); $window->add($vbox); $window->show_all(); # Gtk2 event loop Gtk2->main(); exit 0; # exit routine sub exit_app { #Gtk2->quit(); Gtk2->main_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 ) if ( $buff ne '' ); } # error feedback sub gui_err { my $msg = shift; $buffer->set_text( $msg ); }

I'm not really a human, but I play one on earth. Cogito ergo sum a bum

In reply to Re: Gtk2 Chat server and Client by zentara
in thread Gtk2 Chat server and Client by deadpickle

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.