William Ampeh wrote:
 
> Hello David,
> 
> 
> How would you convert your code to allow a bidirectional communication
> between the clients and the server without the use of files or named
> pipes?
> 
> That is:
> Client send a request to server, waits for server to respond,
> Server processes client's request, and send response back to client,
> Client views server responds and sends another request to server.
> Server responds,

keep the request alive so the client and server can talk to each other:

#!/usr/bin/perl -w
use strict;

use IO::Socket::INET;

#--
#-- server.pl
#--
my $server = IO::Socket::INET->new(Listen    => 5,
                                   LocalAddr => 'localhost',
                                   LocalPort => 5050,
                                   Proto     => 'tcp') || die $!;

while($| = my $client = $server->accept){

        while(sysread($client,$_,1024)){

                next unless(/^time: (.+)/i);

                my $token = $1;
                if($token eq 'whole'){
                        print $client scalar localtime;
                }elsif($token eq 'year'){
                        print $client (localtime)[5]+1900;
                }elsif($token eq 'month'){
                        print $client sprintf("%02d",(localtime)[4]+1);
                }elsif($token eq 'day'){
                        print $client sprintf("%02d",(localtime)[6]);
                }elsif($token eq 'done'){
                        #--
                        #-- at this point, the client wants to stop talking
                        #--
                        print $client -1;
                        close($client);
                        $client = undef;
                        last;
                }else{
                        print $client "?token: $token";
                }
        }

        close($client) if($client);
}

__END__

#!/usr/bin/perl -w
use strict;

use IO::Socket::INET;

#--
#-- client.pl
#--
my $server = IO::Socket::INET->new(PeerAddr => 'localhost',
                                   PeerPort => 5050,
                                   Proto    => 'tcp') || die $!;

$| = 1;

while(<STDIN>){
        chomp;
        last if(/quit/i);
        print $server $_;
        if(sysread($server,my $answer,1024)){
                if($answer =~ /^\?(.+)/){
                        print STDERR "server response with unknown $1\n";
                }elsif($answer =~ /^-1$/){
                        close($server);
                        $server = undef;
                        last;
                }else{
                        print STDERR "Server: $answer\n";
                }
        }
}

close($server) if($server);

__END__

transaction:

[panda]# perl server.pl &
[panda]# perl client.pl
time: whole
Server: Thu Jan  8 14:50:46 2004
time: day
Server: 04
time: year
Server: 2004
time: month
Server: 01
time: what
server response with unknown token: what
quit
[panda]#

the server keeps respond to client request until the client:

1. close the connection on its end
2. send a done command

i notice that you might have sent another reply to one of my previous 
message with some of your code. i haven't get a chance to look at them yet. 
pretty busy nowadays, i will take a look at your code when i have more 
time.

david
-- 
sub'_{print"@_ ";* \ = * __ ,\ & \}
sub'__{print"@_ ";* \ = * ___ ,\ & \}
sub'___{print"@_ ";* \ = * ____ ,\ & \}
sub'____{print"@_,\n"}&{_+Just}(another)->(Perl)->(Hacker)

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>


Reply via email to