Created
September 12, 2015 09:57
-
-
Save Logioniz/9356bd8a3bb04386e8a0 to your computer and use it in GitHub Desktop.
Non-blocking client
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/use/bin/perl | |
use Mojo::Base -strict; | |
use Socket qw/:DEFAULT SOCK_NONBLOCK/; | |
use Data::Dumper; | |
use EV; | |
my $host="localhost"; | |
my $port="3000"; | |
socket(my $sock, PF_INET, SOCK_STREAM | SOCK_NONBLOCK, getprotobyname('tcp')); | |
my $iaddr = inet_aton($host); | |
my $paddr = sockaddr_in($port, $iaddr); | |
my $w; | |
if (!connect($sock, $paddr)) { | |
if ($!{EINPROGRESS} || $!{EWOULDBLOCK}) { | |
$w = EV::io $sock, EV::WRITE | EV::READ, sub { | |
my ($w, $revents) = @_; # all callbacks receive the watcher and event mask | |
if (EV::READ & $revents) { | |
my $msg = <$sock>; | |
print $msg; | |
} | |
print $sock "Hi, i am client\n" if EV::WRITE & $revents; | |
}; | |
} | |
} | |
EV::run; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment