CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
socket problem

 



aaaone
New User

Jan 8, 2008, 12:38 PM

Post #1 of 2 (728 views)
socket problem Can't Post

Hi all!

I was studying Perlipc and tried sources from there.
For strange reasons, they do not work properly!

System: Xp SP2, ActiveState Perl 5.8

Codes:

Client:

use IO::Socket;
my ($host, $port, $kidpid, $handle, $line);

($host, $port) = ("192.168.0.29",10000);

$handle = IO::Socket::INET->new(Proto => "tcp",
PeerAddr => $host,
PeerPort => $port)
or die "can't connect to port $port on $host: $!";

$handle->autoflush(1); # so output gets there right away
print STDERR "[Connected to $host:$port]\n";

die "can't fork: $!" unless defined($kidpid = fork());

if ($kidpid)
{ print "parent!";
my $byte;
while (sysread($handle, $byte, 1) == 1)
{
print STDOUT $byte;
}
#while (defined ($line = <$handle>))
#{
# print STDOUT $line;
#}
kill("TERM", $kidpid); # send SIGTERM to child
}
# the else{} block runs only in the child process
else
{
print "child!";
while (defined ($line = <STDIN>))
{
print $handle $line;
}
}
print "end?\n";

--------------

Server:

use IO::Socket;
use Net::hostent; # for OO version of gethostbyaddr

$PORT = 10000; # pick something not in use
$server = IO::Socket::INET->new( Proto => 'tcp',
LocalPort => $PORT,
Listen => SOMAXCONN,
Reuse => 1);

die "can't setup server" unless $server;
print "[Server $0 accepting clients]\n";

while ($client = $server->accept()) {
$client->autoflush(1);
print $client "Welcome to $0; type help for command list.\n";
#printf "[Connect from %s]\n", $hostinfo ? $hostinfo->name : $client->peerhost;
print $client "Command? "; # HERE IT HANGS!!!
print "THIS LINE WILL NOT BE PRINTED!";
while ( <$client>) {
print "!\n";
next unless /\S/; # blank line
if (/quit|exit/i) { last; }
elsif (/date|time/i) { printf $client "%s\n", scalar localtime; }
elsif (/who/i ) { print $client `who 2>&1`; }
elsif (/cookie/i ) { print $client `/usr/games/fortune 2>&1`; }
elsif (/motd/i ) { print $client `cat /etc/motd 2>&1`; }
else {
print $client "Commands: quit date who cookie motd\n";
}
} continue {
print $client "Command? ";
}
close $client;
}

--------------

Please, help me with such stupid problem. Thanks! :)


Kanji
User / Moderator

Jan 11, 2008, 9:30 PM

Post #2 of 2 (719 views)
Re: [aaaone] socket problem [In reply to] Can't Post

http://www.perlmonks.org/?node_id=646108 has a pretty good explanation of the issue you're running into, and a fix/workaround.
--k.

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives