|< 1 2 >| | 16 Einträge, 2 Seiten |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
use strict;
use warnings;
use IO::Socket::INET;
use Fcntl;
my $pipe_name = './myFIFO';
my $host = '127.0.0.1';
my $port = 43600;
my @open_ports;
my $listener = IO::Socket::INET->new( LocalHost => $host
, LocalPort => $port
, Type => SOCK_STREAM
, Proto => 'tcp'
, Listen => 10
, Reuse => 1
) or die "can't open socket over port $port";
while (my $client = $listener->accept()) {
chomp (my $request = <$client>);
# wenn @open_ports leer ist, werden die naechsten Ports
# aus der Pipe gelesen...
unless (@open_ports) {
die "pipe $pipe_name is not open" unless -e $pipe_name;
sysopen my $PIPE,$pipe_name,O_RDONLY or die $!;
@open_ports = <$PIPE>;
close $PIPE;
}
# an den Client wird der naechste freie Port gesendet und das
# Array gleichermassen verkuertzt
print $client shift @open_ports;
close $client;
}
close($listener);
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
use strict;
use warnings;
use IO::Socket::INET;
use Fcntl;
sub i_am_free {
my $pipe = shift;
my $port = shift;
unless (-p $pipe) {
require POSIX;
POSIX::mkfifo($pipe,0600) or die $!;
}
sysopen my $PIPE,$pipe,O_WRONLY or die $!;
print $PIPE "$port\n";
close $PIPE;
}
my $pipe_name = './myFIFO';
my $host = '127.0.0.1';
my $port = shift @ARGV;
die "Usage: $0 <port>\n" unless $port =~ /^\d+$/ && $port > 0 && $port < 65657;
my $server = IO::Socket::INET->new( LocalHost => $host
, LocalPort => $port
, Type => SOCK_STREAM
, Proto => 'tcp'
, Listen => 10
, Reuse => 1
) or die "can't open socket over port $host:$port";
i_am_free($pipe_name,$port);
while (my $client = $server->accept()) {
my $request = <$client>;
if ($request) {
# processing the request
}
close $client;
i_am_free($pipe_name,$port);
}
close($server);
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
use strict;
use warnings;
use IO::Socket::INET;
my $addr = '127.0.0.1';
my $port = 43600;
my $data = {};
while (1) {
my $port_request = IO::Socket::INET->new( PeerAddr => $addr
, PeerPort => $port
, Proto => 'tcp'
, Type => SOCK_STREAM
) or die "can't connect to $addr:$port";
print $port_request "1\n";
my $new_port = <$port_request>;
close $port_request;
print $new_port; # just a info for tests
chomp $new_port;
my $new_client = IO::Socket::INET->new( PeerAddr => $addr
, PeerPort => $new_port
, Proto => "tcp"
, Type => SOCK_STREAM
) or die "can't connect to $addr:$port";
print $new_client "1\n";
close $new_client;
sleep(1);
}
|< 1 2 >| | 16 Einträge, 2 Seiten |