You make a few big errors in your client. 1. a different socket number from server. 2. No gtk2 main loop in client so it exits immediately. 3. No callback for the Gtk2->Helper to watch the socket. 4. Need to return 1 from Helper to keep it in play.

Try this, it works on linux.

#server #!/usr/bin/perl use warnings; use strict; use IO::Socket; use Gtk2 -init; use Glib qw/TRUE FALSE/; my $main_window = Gtk2::Window->new("toplevel"); $main_window->signal_connect(delete_event => sub{Gtk2->main_quit}); my $buffer = Gtk2::TextBuffer->new; my $table = Gtk2::Table->new(3, 2, FALSE); my $label = Gtk2::Label->new("Chat Client Test"); my $textview = Gtk2::TextView->new_with_buffer($buffer); my $swindow = Gtk2::ScrolledWindow->new( undef, undef); $swindow->set_size_request (100, 150); my $button = Gtk2::Button->new("Send"); my $entry = Gtk2::Entry->new(); $textview->set_cursor_visible (FALSE); $textview->set_editable(FALSE); $buffer->create_mark ('end', $buffer->get_end_iter, FALSE); $buffer->signal_connect (insert_text => sub {$textview->scroll_to_mark + ($buffer->get_mark ('end'),0.0, TRUE, 0, 0.5)}); $swindow->set_policy( 'automatic', 'automatic'); $swindow->set_shadow_type( 'etched-out'); $swindow->add( $textview); $table->attach_defaults($label, 0, 1, 0, 1); $table->attach_defaults($swindow, 0, 2, 1, 2); $table->attach_defaults($entry, 0, 1, 2, 3); $table->attach_defaults($button, 1, 2, 2, 3); $main_window->add($table); $main_window->show_all(); $button->signal_connect("clicked" =>sub { #get the contents of the entry my $msg_send = $entry->get_text; #clear the entry $entry->set_text(""); #grab focus again for the next round of talks $entry->grab_focus; #if there was bogus input, ignore it! if ($msg_send !~ m/^\s*$/){ #open up a UDP client connection my $SEND_PORT = 5152; my $server_host = "localhost"; my $sock = IO::Socket::INET->new(Proto => 'udp', PeerPort => +$SEND_PORT, PeerAddr => $server_host) or die "Creating socket: $!\n" +; #send the message out on the socket $sock->send($msg_send."\n") or die "send: $!"; #update the screen locally #&update_buffer($buffer,$msg_send,TRUE); } }); Gtk2->main;
and the client
#!/usr/bin/perl -w use strict; use IO::Socket; use Gtk2 -init; use Glib qw/TRUE FALSE/; use Gtk2::Helper; my($sock,$MAXLEN, $LISTEN_PORT, $SEND_PORT, $tag, $tag_send, $tag_receive,$img_big,$img_send,$img_rec); $MAXLEN = 1024; $LISTEN_PORT = 5152; my $tview; #set up a udp server waiting for incomming messages $sock = IO::Socket::INET->new(LocalPort => $LISTEN_PORT, Proto => 'ud +p') or die "socket: $@"; #add a Gtk2::Helper watch on any incomming connections print "Awaiting UDP messages on port $LISTEN_PORT\n"; $tag = Gtk2::Helper->add_watch ( fileno($sock), 'in', sub { if (eof($sock)) { Gtk2::Helper->remove_watch ($tag); close($sock); } else { my $line = <$sock>; #$buffer->insert($buffer->get_end_iter,$line); print "$line"; } return 1; }); Gtk2->main;

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

In reply to Re: Building a chat server and client by zentara
in thread Building a 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.