Contributed by jettero
on Jan 05, 2006 at 14:45 UTC
Q&A
> network programming
Description: It seems like this should be a relatively simple thing to do. If feels like it should be 30 lines. However, all my attempts to get this to work have made me feel like a real perl newbie. To tell you the truth, I can't even get a simple two threaded socket to work.
More than once I considered giving up and just using C... Please tell me that's not the best option! Answer: 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;
}
|
Please (register and) log in if you wish to add an answer
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
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, 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, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
Outside of code tags, you may need to use entities for some characters:
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.
|
|