Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

Answer: How can I mix Term::ReadLine::Gnu and IO::Sockets to create a telnet/mud client?

( #521575=categorized answer: print w/ replies, xml ) Need Help??

Q&A > network programming > How can I mix Term::ReadLine::Gnu and IO::Sockets to create a telnet/mud client? contributed by jettero

I just hate answering my own question, because I want to see how someone better at this sort of thing might do it. But I also don't want this question to disappear.

This is a simple IGS client ...

#!/usr/bin/perl use strict; use Term::ReadLine; use IO::Socket; use IO::Select; use threads; use threads::shared; ## This is the IGS go server. Welcome to text mode. ## You must create a username on their website to get this to work my @pair = (qw(igs.joyjoy.net 6969)); my $cmds : shared = ""; my $prompt : shared = "igs> "; my $done : shared = 0; my $term = new Term::ReadLine "igs"; my $listener = new threads( \&listener ); &sender; $listener->join; exit; sub sender { while( defined(my $line = $term->readline($prompt)) ) { lock $cmds; $cmds .= "$line\n"; return if $done; } } sub listener { my $sock = new IO::Socket::INET( PeerAddr => $pair[0], PeerPort => + $pair[1], Proto => "tcp" ) or logger->error("pfft: $!"); my $sele = new IO::Select; my $eb_count = 0; my $last = time+60; $sele->add( $sock ); while( $sock->connected ) { if( $sele->can_read(0.1) ) { my $buf = undef; my $sock_addr = recv( $sock, $buf, 1024, 0 ); if( not defined $sock_addr ) { die "socket error"; } if( not $buf ) { #print "empty \$buf...\n"; if( (++ $eb_count) >= 20 ) { warn "connection terminated\n(return to quit)\n"; $done = 1; return; } } our $ayt; $ayt -- if $ayt and $buf =~ s/(?:\s*|\b)yes\s*(?=\n)//s; $prompt = $1 if $buf =~ s/([^\r\n]+?)$//s; if( $buf ) { print $buf; $term->set_prompt( $prompt ); $term->forced_update_display; $eb_count = 0; } } if( $cmds ) { lock $cmds; print $sock $cmds; $cmds = ""; } if( $last+60 < (my $now = time) ) { our $ayt ++; print $sock "ayt\n"; $last = $now; } } shutdown $sock, 2; }

Comment on Answer: How can I mix Term::ReadLine::Gnu and IO::Sockets to create a telnet/mud client?
Download Code
Re: Answer: How can I mix Term::ReadLine::Gnu and IO::Sockets to create a telnet/mud client?
by zentara (Archbishop) on Jan 06, 2006 at 20:06 UTC
    Here is another threaded chat server, from awhile back. It didn't use Term::ReadLineGnu, but it works. It might give you some ideas. ChatServer

    I'm not really a human, but I play one on earth. flash japh
Log In?
Username:
Password:

What's my password?
Create A New User
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (4)
As of 2015-07-04 04:19 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (57 votes), past polls