Advertisement
Guest User

Untitled

a guest
Jun 16th, 2019
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.63 KB | None | 0 0
  1. $sock = new IO::Socket::INET(
  2. PeerAddr => $_[0],
  3. PeerPort => $_[1],
  4. Proto => 'tcp',
  5. Timeout => 2
  6. );
  7.  
  8. my $timeout = ${*$sock}{'io_socket_timeout'};
  9. # my $before = time() if $timeout;
  10.  
  11. undef $@;
  12. if ($sock->connect(pack_sockaddr_in($rport, $raddr))) {
  13. # ${*$sock}{'io_socket_timeout'} = $timeout;
  14. return $sock;
  15. }
  16.  
  17. return _error($sock, $!, $@ || "Timeout")
  18. unless @raddr;
  19.  
  20. # if ($timeout) {
  21. # my $new_timeout = $timeout - (time() - $before);
  22. # return _error($sock,
  23. # (exists(&Errno::ETIMEDOUT) ? Errno::ETIMEDOUT() : $EINVAL),
  24. # "Timeout") if $new_timeout <= 0;
  25. # ${*$sock}{'io_socket_timeout'} = $new_timeout;
  26. # }
  27.  
  28. eval {
  29. local $SIG{ALRM} = sub { die 'Timed Out'; };
  30. alarm 3;
  31. my $sock = IO::Socket::INET->new(
  32. PeerAddr => inet_ntoa( gethostbyname($host) ),
  33. PeerPort => 'whois',
  34. Proto => 'tcp',
  35. ## timeout => ,
  36. );
  37. $sock->autoflush;
  38. print $sock "$qry1512";
  39. undef $/; $data = <$sock>; $/ = "n";
  40. alarm 0;
  41. };
  42. alarm 0; # race condition protection
  43. return "Error: timeout." if ( $@ && $@ =~ /Timed Out/ );
  44. return "Error: Eval corrupted: $@" if $@;
  45.  
  46. # Impatient Client
  47. use IO::Socket::INET;
  48.  
  49. $sock = new IO::Socket::INET(
  50. PeerAddr => "localhost",
  51. PeerPort => "10007",
  52. Proto => 'tcp',
  53. Timeout => 2,
  54. );
  55.  
  56. print <$sock>;
  57.  
  58. close($sock);
  59.  
  60.  
  61. # SlowServer
  62. use IO::Socket::INET;
  63.  
  64. $sock = new IO::Socket::INET(
  65. LocalAddr => "localhost",
  66. LocalPort => "10007",
  67. Proto => 'tcp',
  68. Listen => 1,
  69. Reuse => 1,
  70. );
  71.  
  72. $newsock = $sock->accept();
  73. sleep 5;
  74.  
  75. #while (<$newsock>) {
  76. # print $_;
  77. #}
  78. print $newsock "Some Stuff";
  79. close($newsock);
  80. close($sock);
  81.  
  82. pti@pti-laptop:~/playpen$ perl server.pl&
  83. [1] 9130
  84. pti@pti-laptop:~/playpen$ time perl test.pl
  85. Some Stuff[1]+ Done perl server.pl
  86.  
  87. real 0m5.039s
  88. user 0m0.050s
  89. sys 0m0.030s
  90.  
  91. use IO::Socket::INET;
  92. eval {
  93. local $SIG{ALRM} = sub { die 'Timed Out'; };
  94. alarm 2;
  95. $sock = new IO::Socket::INET(
  96. PeerAddr => "localhost",
  97. PeerPort => "10007",
  98. Proto => 'tcp',
  99. Timeout => 2,
  100. );
  101.  
  102. print <$sock>;
  103.  
  104. close($sock);
  105. alarm 0;
  106. };
  107. alarm 0; # race condition protection
  108. print "Error: timeout." if ( $@ && $@ =~ /Timed Out/ );
  109. print "Error: Eval corrupted: $@" if $@;
  110.  
  111. pti@pti-laptop:~/playpen$ perl server.pl&
  112. [1] 9175
  113. pti@pti-laptop:~/playpen$ time perl test2.pl
  114. Error: timeout.Error: Eval corrupted: Timed Out at test2.pl line 3.
  115.  
  116. real 0m2.040s
  117. user 0m0.020s
  118. sys 0m0.010s
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement