Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Ok, I have this server script that I am currently writing for use with a gui front end. I am trying to do some simple auth with a user id and password, and for the most part it works just fine. Though I have come across a strang thing when sending a response back to the client. I am calling a sub that checks the id and password and returns the userid on good and undef on bad. The problem is that when I try to set the return to something the response never gets sent to the client. If I just call the sub by it's self everything works great .... any ideas Thanx
sub Sserv { my ($sever, $log, $logfile) = @_; $server = IO::Socket::INET->new( LocalPort => 3300, Type => SOCK_STREAM, Listen => 0, Reuse => 1, ) or die $!; open($log, ">> $logfile"); select((select($log), $| = 1)[0]); while (my $conn = $server->accept) { my $t = threads->create("gClnt", $conn, $log, $server) or die +$!; $t->detach; } } sub gClnt { my ($conn, $log, $server) = @_; my $user; while (defined(my $ans = <$conn>)) { if (my $mess = chkAns($ans)) { if ($mess =~ /^LOGIN_REQ:.*/) { if ($user = userLogin($mess)) { print $conn "GOOD"; } else { print $conn "BAD"; } } if ($user) { print "$user:$mess\n"; KILLER($conn, $log, $server) if $mess =~ /^END_PROC_RE +Q:.*/; last if $mess =~ /^LOGOUT_REQ:.*/; } else { last; } } } } sub userLogin { my ($mess) = @_; if ($mess =~ m/^LOGIN_REQ:([\d \w]+):([\d \w]+)/) { tie(my %db, 'SDBM_File', $userDB, O_RDWR|O_CREAT, 0666) or die + "Problem accessing user DB\n"; if (my $pass = $db{$1}) { my $pwd = PwdCrypt->new($2); if ($pwd->check($pass)) { return $1; } } untie %db; } return undef; }
oh ya the chkAns sub is just a check sum for the request....

Replies are listed 'Best First'.
Re: Socket problems?????
by Thelonius (Priest) on Apr 29, 2003 at 20:09 UTC
    It's not exactly clear from your description what the problem is. You also didn't include the client code, which makes debugging harder. If the client is using readline (i.e. <sockethandle>), then the client is going to be waiting a while since you didn't include a newline with the "GOOD" or "BAD" response.
      That was the problem, as soon as I posted this question it became apparent to me. What still puzzles me is the fact that is would work with out the newline when I only called the "userLogin()" sub with out catching the return value. Thanx for you help