first off, i apologize if i'm not describing this as succinctly as possible.

i'm working on some client/server stuff using sockets, and i grabbed
some code from the net that i don't totally understand.

i'm passing packed messages back and forth over a socket from the
client/server.  Right now i have 2 way communication, the server sends
a intro message,
the client prints the message,
then the client waits for input from the user, sends this info to the server,
and then the server acts accordingly, based on what it recieved from
the client.  Thats all well and good, however:

I can't figure out how to get anything to print to the clients
stdout/monitor on the line accepting input.

while the client is waiting for input from the user, i would like it
to display a prompt, like:

input:+space+what the user types goes here

here is the client being run, i am unsuccessfully trying to print
"input>" at the beginning of the 10 and 13 lines, which were user
input:

[EMAIL PROTECTED]:~/l$ ./int_cli_w_IOSOCK.pl x.x.edu 12000
[Connected to x.x.edu:12215]
Welcome to ./serv_p_forking.pl; type help for command list.
10
 input> score: 100
13
score: 169

here's the client code, the server code follows, though i can't see
how the problem would be in the server:
#
#
#
#
#
#!/usr/bin/perl -w

    use strict;
    use IO::Socket;

    my ($host, $port, $kidpid, $handle, $line);
    my $input = "intput> ";

    unless (@ARGV == 2) { die "usage: $0 host port" }
    ($host, $port) = @ARGV;

    # create a tcp connection to the specified host and port
    #$handle = IO::Socket::INET->new(Proto     => "udp",
    $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";

    # split the program into two processes, identical twins
    die "can't fork: $!" unless defined($kidpid = fork());

    # the if{} block runs only in the parent process
    if ($kidpid) {
        # copy the socket to standard output
        #while (defined ($line = <$handle>)) {
        #    print STDOUT $line;
        #}

    my $byte;
    my $byte2;
    my $byte3;
    my $bytecount = 0;


    #get initial message from server
    #which is 59 bytes
    sysread($handle, $byte, 59);

    $byte3 = unpack ("a*", $byte);

    print $byte3."\n input> ";



#    do{

        #sysread($handle, $byte, 1);
        #$byte2 .= $byte;
        #chomp $byte;
        #print $byte;
    #}while($byte ne "\n");


    while(sysread($handle, $byte, 4)) {

        if($byte2 = unpack ("i",$byte)){
            print "score: ".$byte2."\n";
        }
    }





        kill("TERM", $kidpid);                  # send SIGTERM to child
    }
    # the else{} block runs only in the child process
    else {
        # copy standard input to the socket
        while (defined ($line = <STDIN>)) {     
            print $handle $line;
        
        }
    }

#
#
#
#
#
#

here's the server code:

#
#
#
#
#
#
#

#!/usr/bin/perl

use Chatbot::Eliza;
use IO::Socket;
use Net::hostent;              # for OO version of gethostbyaddr
use POSIX 'WNOHANG';
use constant PORT => 12000;
use strict;

$|=1;

my $hostinfo;
my $client;
my $quit = 0;
#signal handler for child die events
$SIG{CHLD} = sub { while ( waitpid(-1,WNOHANG)>0 ) { } };

#signal handler for interrupt key and TERM signal
$SIG{INT} = sub { $quit++ };

my $listen_socket = IO::Socket::INET->new(LocalPort => PORT,
                                          Listen    => 20,
                                          Proto     => 'tcp',
                                          Reuse     => 1,
                                          Timeout   => 60*60,
                                           );
die "Can't create a listening socket: $@" unless $listen_socket;
    warn "Server ready.  Waiting for connections. . .\n";

while (!$quit) {
        
    next unless $client = $listen_socket->accept;
        
    $client->autoflush(1);

    defined (my $child = fork()) or die "Can't fork $!";
    if ($child == 0) {
        $listen_socket->close;
        interact($client);
        exit 0;
        }

    $client->close;
        
}

#}

sub interact {

   my $welcome = "Welcome to $0; type help for command list.";
   my $pkd_welcome = pack("a*", "Welcome to $0; type help for command list.");

   my $lgth = length($pkd_welcome);


   print "\nl - $lgth\n\n";

   $client->send($pkd_welcome);



   $hostinfo = gethostbyaddr($client->peeraddr);
   printf "[Connect from %s]\n", $hostinfo->name || $client->peerhost;
   #$client->send("Command?");

   #$client->send(pack("i",42*42)) ;

   while ( <$client>) {
     next unless /\S/;       # blank line
     if (/10/i){
         $client->send(pack("i",10*10)) ;
         print "recv'd -> 10\n";
     }
     elsif (/13/i){
         $client->send(pack("i",13*13)) ;
         print "recv'd -> 13\n";
     }
     elsif (/29/i ){
         $client->send(pack("i",29*29)) ;
         print "recv'd -> 29\n";
     }
     elsif (/42/i ){
         $client->send(pack("i",42*42)) ;
         print "recv'd -> 42\n";
     }
     elsif (/65/i ){
         $client->send(pack("i",65*65)) ;
         print "recv'd -> 65\n";
     }
     else {
       print $client "Game: 10, 13, 29, 42, 65?\n";
     }
   } continue {

   }
   close $client;

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


Reply via email to