I am new to this list so if this is the wrong place to ask please let me
know.  I am attempting to build a client/server application using the
IO::Socket package.  I am having a hard time implimenting a simple
server which can send/receive messages in response to a client that can
also send/recieve messages.

As of yet I have only been able to get the server to listen, accept, and
return a response to the client send, the client at that point blocks
and cannot retrieve the servers response to its initial send.  Below is
the code I am working with, any help is appreciated;

[server.pl]
sub socket
{
    print "Binding: $host on port $port, please wait...\n";
    my $sock = new IO::Socket::INET( LocalHost => $host,
                                     LocalPort => $port,
                                     Proto     => 'tcp',
                                     Listen    => $max_clients,
                                     Resuse    => 1 );
    die "Error: Could not create socket - $!\n" unless $sock;
    print "Daemon bound at $host on $port\n";
    print "Awaiting connections...\n";
    for( ;; ) {
       while( my $new = $sock->accept ) {
           print "Recieved connection from " . $new->peerhost . " : " .
$new->peerport . "\n";
           $sock->autoflush( 1 );
           $sock->blocking( 0 );
           while( defined( my $data = <$new> ) ) {
               if( $data =~ /^status$/ ) {
                   $buffer = &GetStatus;
                   print "Command STATUS recieved, sending data...\n";
                   print $new $buffer;
                   print "Sent to client:\n$buffer\n";
                   close( $new );
               } elsif( $data =~ /^stats$/ ) {
                   @data = &GetStats;
                   print "Command STATS recieved, sending data...\n";
                   foreach $buffer( @data ) {
                       print $new $buffer;
                       print "$buffer\n";
                       close( $new );
                   }
               } else {
                   $new->send( "Error: Command not
recognized.\nAvailable Commands: stats | status\n" );
                   print "Error: Command not recognized from client -
$data\n";
                   print $new "Error: Command not recognized.\nAvailable
Commands: stats | status\n";
                   close( $new );
               }
           }
       }
    }
}

[client.pl]
sub Connect
{
    print "Connecting to $host on $port...\n";
    my $sock = new IO::Socket::INET( PeerAddr => $host,
                                     PeerPort => $port,
                                     Proto    => 'tcp' );
    die "Error: Could not create socket - $!\n" unless defined( $sock );
    $sock->autoflush( 1 );
    die "Error: Could not fork process - $!\n" unless defined( my $pid =
fork() );
    if( $pid ) {
        while( my $buffer = <$sock> ) {
            print "Server data:\n";;
            print "$buffer\n";
        }
        close( $sock );
        kill( "TERM", $pid );
    } else {
        print "Sending $command command to $host on $port...\n";
        $sock->send( $command );
    }
}

Thanks in advance.

-- 
Jason Gerfen

"And remember... If the ladies
 don't find you handsome, they
 should at least find you handy..."
             ~The Red Green show

_______________________________________________
ActivePerl mailing list
[email protected]
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs

Reply via email to