in reply to Building a chat server and client
Try this, it works on linux.
and the client#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;
#!/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;
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Building a chat server and client
by deadpickle (Pilgrim) on Jan 09, 2008 at 21:52 UTC | |
by zentara (Cardinal) on Jan 10, 2008 at 15:56 UTC | |
by deadpickle (Pilgrim) on Jan 14, 2008 at 22:42 UTC | |
by zentara (Cardinal) on Jan 15, 2008 at 00:27 UTC | |
by deadpickle (Pilgrim) on Jan 16, 2008 at 06:53 UTC |