Created
November 1, 2014 17:30
-
-
Save psyark/a83daccb125814dd9226 to your computer and use it in GitHub Desktop.
This file contains 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
#!/usr/local/bin/perl | |
use strict; | |
use IO::Socket; | |
use IO::Select; | |
my $julius_socket = IO::Socket::INET->new( | |
Proto => 'tcp', | |
PeerAddr => 'localhost', | |
PeerPort => 10500, | |
) or die "connect failed: $!\n"; | |
my $listening_socket = IO::Socket::INET->new( | |
Proto => 'tcp', | |
LocalPort => 10501, | |
Listen => SOMAXCONN, | |
Reuse => 1, | |
) or die "listen failed: $!\n"; | |
my $selecter = IO::Select->new; | |
$selecter->add($julius_socket); | |
$selecter->add($listening_socket); | |
while (1) { | |
my $julius_output = ''; | |
# Socketの接続受付と受信 | |
for my $sock ($selecter->can_read(1)) { | |
if ($sock == $listening_socket) { | |
my $new_sock = $listening_socket->accept; | |
print "[event socket] connect from ", $new_sock->peerhost, "\n"; | |
$selecter->add($new_sock); | |
} elsif ($sock == $julius_socket) { | |
$julius_output = <$sock>; | |
print $julius_output; | |
} else { | |
unless ($sock->eof) { | |
my $buf = <$sock>; # 捨てる | |
} else { | |
warn "[socket] disconnect\n"; | |
$selecter->remove($sock); | |
$sock->close; | |
} | |
} | |
} | |
# Socketへの送信 | |
for my $sock ($selecter->can_write(1)) { | |
if ($sock == $julius_socket) { | |
# Juliusに言いたいことがあればここで | |
} elsif ($julius_output) { | |
print $sock $julius_output; | |
$sock->flush; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment