-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmothership
executable file
·78 lines (68 loc) · 1.69 KB
/
mothership
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
78
#!/usr/bin/perl
use IO::Socket::INET;
use IO::Select;
# auto-flush on socket
$| = 1;
# creating a listening socket
my $socket = new IO::Socket::INET (
LocalHost => '10.0.0.20',
LocalPort => '7778',
Proto => 'tcp',
Listen => 50,
Reuse => 1
);
my $sel = new IO::Select($socket);
#$sel->add( \*STDIN );
die "cannot create socket $!\n" unless $socket;
print "server waiting for client connection on ".$socket->sockaddr(). " port ".$socket->sockport()."\n";
my %known = (
"18:fe:34:9c:8d:d0"=>"vfdclock-1"
);
my %clients;
while(1) {
print scalar localtime,"\n";
if(@ready = $sel->can_read(1.0)) {
foreach my $fh (@ready) {
if ($fh == $socket) {
print "new connection\n";
$new_sock = $socket->accept();
$sel->add($new_sock);
$clients{$new_sock}={};
} elsif ($fh == \*STDIN) {
print "got stdin:\n";
print "< $_ >" foreach <$fh>;
} else {
read_socket($fh);
}
}
}
}
sub read_socket() {
print "read_socket...\n";
my $fh = shift;
my $c = $clients{$fh};
$buf = <$fh>;
if ($buf) {
if($buf =~ /MAC (.+)$/) {
print "registering connection for <$1> aka $known{$1}\n";
$c->{MAC} = $1;
$c->{id} = $known{$1};
}
print "$c->{id}: $buf\n";
while($buf = <$fh>) {
print "$c->{id}:: $buf\n";
}
#my @sockets = $readable_handles->can_write();
#print $sock "You sent $buf\n";
# foreach my $sck(@sockets){
# print $sck "$buf\n";
# }
} else {
print $clients{$sock}->{MAC}." disconnected?\n";
$readable_handles->remove($sock);
close($sock);
}
}
print "Terminating Server\n";
close $main_sock;
getc();