Last change
on this file since 24671 was 24671, checked in by jmt12, 13 years ago |
Simple server and client objects that are based around a socket that accepts requests then spawns off threads to action them. I didn't write these - they were downloaded from github - and I'm unsure of the licensing (but assume GPL)
|
File size:
930 bytes
|
Line | |
---|
1 | # @author Donnie Cameron (macnod)
|
---|
2 | # @url https://github.com/macnod/DcServer
|
---|
3 | # @readme http://donnieknows.com/blog/perl-sockets-swimming-thread-pool
|
---|
4 |
|
---|
5 | package SocketsSwimmingThreadPoolClient;
|
---|
6 |
|
---|
7 | use IO::Socket;
|
---|
8 | use Carp;
|
---|
9 | use strict;
|
---|
10 | use warnings;
|
---|
11 |
|
---|
12 | sub new
|
---|
13 | {
|
---|
14 | # Params: host, port
|
---|
15 | my ($proto, %param)= @_;
|
---|
16 | my $class= ref($proto) || $proto;
|
---|
17 | bless +{%param} => $class;
|
---|
18 | }
|
---|
19 |
|
---|
20 | sub stop_server
|
---|
21 | {
|
---|
22 | shift->query('$self->stop')
|
---|
23 | }
|
---|
24 |
|
---|
25 | sub query
|
---|
26 | {
|
---|
27 | my ($self, $query)= @_;
|
---|
28 | my $socket= new IO::Socket::INET(PeerAddr => $self->{host} || 'localhost',
|
---|
29 | PeerPort => $self->{port} || 8191,
|
---|
30 | Proto => 'tcp');
|
---|
31 | croak "$!. Is the server running?\n" unless $socket;
|
---|
32 | print $socket $query . "\n.\n";
|
---|
33 | my $buffer = '';
|
---|
34 | my $reply = '';
|
---|
35 | while($buffer = <$socket>)
|
---|
36 | {
|
---|
37 | $reply.= $buffer;
|
---|
38 | last if $reply =~ s/\n\.\n$//;
|
---|
39 | }
|
---|
40 | $socket->shutdown(2);
|
---|
41 | return $reply;
|
---|
42 | }
|
---|
43 |
|
---|
44 | 1;
|
---|
Note:
See
TracBrowser
for help on using the repository browser.