Louie Iturzaeta wrote:
> Hello Perl Guru's!
>
> Does anyone know of a way to get the remote clients hostname with
> IO::Socket?
> I am new to this socket programming and I am writing a simple socket
> server that uses telnet as a client.
> I want to have the remote clients hostname appear on the server console
> when they connect.
> Any ideas or pointers would be appreciated!
>
> -Louie
>
> Here is my code:
>
> use strict;
> use IO::Socket;
> use Sys::Hostname;
>
> my $host = hostname;
> my $port = $ARGV[0] || 4242;
> my $pass = $ARGV[1] || "pass";
> my $count;
> my $sock = new IO::Socket::INET(
> LocalHost => $host,
> LocalPort => $port,
> Proto => 'tcp',
> Listen => SOMAXCONN,
> Reuse => 1);
> $sock or die "no socket :$!";
> STDOUT->autoflush(1);
> my($new_sock, $buf);
>
> while ($new_sock = $sock->accept()) {
> pl ("Connected to Console Server on $host TCP Port $port\n");
Try something like:
my $raddr = $accept->peerhost();
my $rport = $accept->peerport();
my $rhost = gethostbyaddr (inet_aton ($raddr), AF_INET) || '<unknown>';
pl ("TCP Console Server $host:$port accepting $rhost ($raddr:$rport)\n");
> $buf = <$new_sock>; my @split = split(/\s/, $buf);
> if ($split[0] ne $pass) { close $new_sock; }
> while (defined($buf = <$new_sock>)) {
> my @split = split(/\s/, $buf);
> my $cmd = shift @split;
> if ($cmd eq 'exit') { close $new_sock; }
> if ($cmd eq 'quit') { exit; }
> if ($cmd eq 'beep') { beep($split[0]); }
> print $new_sock ">"; # print prompt
> }
> close $new_sock;
> }
>
> sub pl {
> print "$_[0]\r";
> print $new_sock "$_[0]\r";
> }
>
> sub beep {
> if ($_[0] eq '') { pl ("usage: beep x - where x is the number of
> times to beep\n"); return; }
> pl ("beeping $_[0] times\n");
> for ($count=0; $count<$_[0]; $count++) {
> pl ("\007");
> }
> }
--
,-/- __ _ _ $Bill Luebkert Mailto:[EMAIL PROTECTED]
(_/ / ) // // DBE Collectibles Mailto:[EMAIL PROTECTED]
/ ) /--< o // // Castle of Medieval Myth & Magic http://www.todbe.com/
-/-' /___/_<_</_</_ http://dbecoll.tripod.com/ (My Perl/Lakers stuff)
_______________________________________________
ActivePerl mailing list
[EMAIL PROTECTED]
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs