Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/perl
- use strict;
- use warnings;
- ################################################################################
- # Load hash from CSV at startup
- ################################################################################
- open DATA, "mapping.csv";
- my %hash;
- while( <DATA> ) {
- chomp $_;
- my ($field1,$field2) = split /,/, $_;
- if( $field1 ne '' ) {
- $hash{$field1} = $field2;
- }
- }
- close DATA;
- print "Readyn";
- ################################################################################
- # Answer queries forever
- ################################################################################
- use IO::Socket::INET;
- # auto-flush on socket
- $| = 1;
- my $port=5000;
- # creating a listening socket
- my $socket = new IO::Socket::INET (
- LocalHost => '127.0.0.1',
- LocalPort => $port,
- Proto => 'tcp',
- Listen => 5,
- Reuse => 1
- );
- die "cannot create socket $!n" unless $socket;
- while(1)
- {
- # waiting for a new client connection
- my $client_socket = $socket->accept();
- my $data = "";
- $client_socket->recv($data, 1024);
- my $key=$data;
- chomp $key;
- my $reply = "ERROR: Not found $key";
- if (defined $hash{$key}){
- $reply=$hash{$key};
- }
- print "DEBUG: Received $key: Replying $replyn";
- $client_socket->send($reply);
- # notify client that response has been sent
- shutdown($client_socket, 1);
- }
- chmod +x go.pl
- ./go.pl
- socat - TCP:127.0.0.1:5000 <<< "1350772177"
- 1347092335
- START=$SECONDS; tail -1000 *csv | awk -F, '{print $1}' |
- while read a; do echo $a | socat - TCP:127.0.0.1:5000 ; echo; done; echo $START,$SECONDS
Add Comment
Please, Sign In to add comment