![]() |
![]() |
10 entries, 1 page |
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#!/usr/bin/perl
# usage: client.pl [host] [port]
use strict;
use IO::Socket;
use IO::Poll 0.04 qw(POLLIN POLLOUT POLLERR POLLHUP);
use Errno qw(EWOULDBLOCK);
use constant MAXBUF => 8192;
$SIG{PIPE} = 'IGNORE';
my ($to_stdout,$to_socket,$stdin_done,$sock_done);
my $host = shift or die "Usage: client.pl host [port]\n";
my $port = shift || 'echo';
my $socket = IO::Socket::INET->new("$host:$port") or die $@;
my $poll = IO::Poll->new() or die "Can't create IO::Poll object";
$poll->mask(\*STDIN => POLLIN);
$poll->mask($socket => POLLIN);
$socket->blocking(0); # turn off blocking on the socket
STDOUT->blocking(0); # and on STDOUT
while ($poll->handles) {
$poll->poll;
#handle readers
for my $handle ($poll->handles(POLLIN|POLLHUP|POLLERR)) {
if ($handle eq \*STDIN) {
$stdin_done++ unless sysread(STDIN,$to_socket,2048,length $to_socket);
}
elsif ($handle eq $socket) {
$sock_done++ unless sysread($socket,$to_stdout,2048,length $to_stdout);
}
}
# handle writers
for my $handle ($poll->handles(POLLOUT|POLLERR)) {
if ($handle eq \*STDOUT) {
my $bytes = syswrite(STDOUT,$to_stdout);
unless ($bytes) {
next if $! == EWOULDBLOCK;
die "write to stdout failed: $!";
}
substr($to_stdout,0,$bytes) = '';
}
elsif ($handle eq $socket) {
my $bytes = syswrite($socket,$to_socket);
unless ($bytes) {
next if $! == EWOULDBLOCK;
die "write to socket failed: $!";
}
substr($to_socket,0,$bytes) = '';
}
}
} continue {
my ($outmask,$inmask,$sockmask) = (0,0,0);
$outmask = POLLOUT if length $to_stdout > 0;
$inmask = POLLIN unless length $to_socket >= MAXBUF
or ($sock_done || $stdin_done);
$sockmask = POLLOUT unless length $to_socket == 0 or $sock_done;
$sockmask |= POLLIN unless length $to_stdout >= MAXBUF or $sock_done;
$poll->mask(\*STDIN => $inmask);
$poll->mask(\*STDOUT => $outmask);
$poll->mask($socket => $sockmask);
$socket->shutdown(1) if $stdin_done and !length($to_socket);
}
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
43
44
45
46
47
48
49
#!/usr/bin/perl -w
use strict;
use IO::Socket;
use IO::File;
use Switch;
use Data::Dumper;
my $peerAddr; # that's the hosts ip-adress
my $peerPort; # that's the hosts port
# the usage is: command ip-addr port
if ($ARGV[0] && $ARGV[1])
{
$peerAddr = $ARGV[0];
$peerPort = $ARGV[1];
}
else
{
die "Usage: $0 target-address portnumber\n";
}
# CREATING THE SOCKET
my $server = new IO::Socket::INET (
PeerAddr => $peerAddr,
PeerPort => $peerPort,
Proto => "tcp",
Type => SOCK_STREAM
) or die "*** Can't connect to server. ($!) ***\n";
my $maxBuffer = 1024;
my $myName;
# CREATING THE FORK
my $pid = fork(); # the fork lets us create two parallel processes
if ($pid == 0) { # when our process is active -> do something
main("start","");
} else { # otherwise -> read out the server's messages
while(1) {
my $nval = $server->recv(my $data, $maxBuffer, 0);
if (defined($nval) && length($data))
{
XMLparse($data);
}
}
}
$server->close();
exit;
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
43
44
45
my $socket = new IO::Socket::INET (
LocalHost => $hostAddr,
LocalPort => $hostPort,
Proto => 'tcp',
Type => SOCK_STREAM,
Listen => SOMAXCONN,
Reuse => 1
) or die "*** Can't create server socket. ($!) ***\n" unless $socket;
print "*** Server listening on $hostAddr:$hostPort ... :-) ***\n";
my $allClients = new IO::Select($socket);
my @activeClients;
while (@activeClients = $allClients->can_read())
{
foreach my $client (@activeClients)
{
if ($client eq $socket)
{
my $newClient = $socket->accept(); # accept the connection
$allClients->add($newClient); # add it to the handler
print "*** ".$newClient->peerhost.":".$newClient->peerport." has just connected. ***\n";
}
else
{
my $nval = $client->recv(my $request, $maxBuffer, 0);
if (!defined($nval) || !length($request))
{
print "*** Connection has been closed. ***\n";
my $h = 0;
while ($activeUsers[$h])
{
if ($activeUsers[$h]->{'handle'} == $client)
{
splice(@activeUsers,$h,1);
}
$h++;
}
$allClients->remove($client);
$client->close();
next;
}
}
}
}
$socket->blocking(0);
![]() |
![]() |
10 entries, 1 page |