Skip to content

Instantly share code, notes, and snippets.

@Logioniz
Created September 12, 2015 09:57
Show Gist options
  • Save Logioniz/9356bd8a3bb04386e8a0 to your computer and use it in GitHub Desktop.
Save Logioniz/9356bd8a3bb04386e8a0 to your computer and use it in GitHub Desktop.
Non-blocking client
#!/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