tucano has asked for the wisdom of the Perl Monks concerning the following question:
#!/usr/bin/perl # #################################### # # JDKCHAT WRITER SERVER # # (Studing sockets) #################################### use strict; use warnings; use IO::Socket; use Net::Telnet; ##################################### #telnet connection my $telnet = new Net::Telnet ( Timeout=>10, Errmode=>'die', Port=>2002, Prompt => '/bash\$ $/' ); #socket my $sock = new IO::Socket::INET (LocalHost => 'oni-nux', LocalPort => 8000, Proto => 'tcp', Listen => 5, Reuse => 1, ); die "could not connect: $!" unless $sock; die "can't connect to the chat server: $!" unless $telnet; $telnet->open('localhost'); my @welcome = $telnet->waitfor('/Your Name:$/i'); my $new_sock; my $buf; my $name = 'recipientbot'; chomp $name; $telnet->print($name); my @buffer_in; $telnet->waitfor('/>$/i'); while ($new_sock = $sock->accept()) { while (defined ($buf = <$new_sock>)) { $telnet->print($buf); @buffer_in = $telnet->waitfor('/ >$/i'); print @buffer_in,"\n"; } } close ($sock);
#!/usr/bin/perl # # SOCKET JDKCLIENT SEND MSG TO THE SERVER use strict; use warnings; use IO::Socket; my $chat_path='/usr/local/src/jdkchat-1.5/'; my $log_file='log_file'; my $mode; my $opt; my $msg; if (defined $ARGV[0]) {$mode = $ARGV[0]} else {$mode=0;} if (defined $ARGV[1]) {$opt = $ARGV[1]} else {$opt='tail';} my $sock = new IO::Socket::INET (PeerAddr => 'oni-nux', PeerPort => 8000, Proto => 'tcp', ); die "Socket could not be created. Reason is $!" unless $sock; if ($mode eq 'who') { print $sock "/who"; } if ($mode eq 'priv_msg') { #print $sock "/who"; print "send a message to ... [connection number]"; print "connection number:\n>"; my $user = <STDIN>; chomp ($user); print "msg?"; $msg = <STDIN>; chomp ($msg); print $sock '/',$user,"$msg","\n>"; } if ($mode eq 'read') { _read_log($opt, $chat_path, $log_file); } if ($mode eq 'msg') { $msg=<STDIN>; print $sock "$msg"; } close ($sock); ####################################################### # SUB ####################################################### sub _read_log { my $opt=$_[0]; my $chat_path=$_[1]; my $log_file=$_[2]; if ($opt eq "tail") { _tail ($chat_path,$log_file); } elsif ($opt eq "all") { _all ($chat_path,$log_file); } else { die "unrecognized command: $!" } }; sub _tail { my $line; my $log_file = $_[0].$_[1]; print $log_file,"\n"; system ("tail $log_file > tail.log"); open (TAIL, 'tail.log'); while ( $line = <TAIL> ) { if ($line=~/JDKCHAT:/) { next; } else { print $line; } } close (TAIL); }; sub _all { my $line; my $log_file = $_[0].$_[1]; open (LOGFILE, "$log_file"); while ($line = <LOGFILE>) { if ($line =~ /JDKCHAT:/) { next; } else { print $line; } } close (LOGFILE); };
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Perl Telnet Client
by Joost (Canon) on Jun 01, 2004 at 19:15 UTC | |
by tucano (Scribe) on Jun 02, 2004 at 10:48 UTC |