Thread Simpler Threaded Server: ...und ich find den fehler nicht... (5 answers)
Opened by Gast at 2007-05-18 11:56

Gast Gast
 2007-05-18 11:56
#37557 #37557
Hallo zusammen, ich bin relativ neu dabei was perl angeht und habe zur übung mit einem kleinen script angefangen was in eine datei schreibt, das wurde dann durch einen simplen server erweitert, und nun bin ich (mit hilfe eines beispiel-scripts) daran threads mit einzubauen...

ich bekomme beim ausführen des scripts diese Fehler:

$ perl server.pl
syntax error at server.pl line 100, near "'CODE' {"
syntax error at server.pl line 116, near "}"
Execution of server.pl aborted due compilation errors.

ich sitz jetzt schon 2 stunden dran, hab einiges hin und herprobiert aber ich packs nich... Vieleicht schaut einer von euch ma drüber und sieht auf den ersten blick meinen fehler :)
(die hoffnung stirbt zuletzt *g*)

server.pl
Code: (dl )
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
#!/usr/bin/perl

#Simples Server konstrukt by FlorianL;)

use strict;
require 5.002;
use warnings;
use IO::Socket;
use Carp;

my $cfgfile = "serverconfig.cfg";
my $keyfile = ".key.file";

my ($port, $username, $choosed, $coderef);
sub logmsg {print "$0 $$: @_ at ", scalar localtime, "\n" }
sub spawn;

sub genpass {
print ("\nGenerating KeyFile\nEnter your Password: ");
chomp (my $password = <STDIN>);
my @chars = ("A" .. "Z");
my $salt = join("", @chars[ map {rand @chars } (1 .. 2) ]);
our $crypted = crypt("$password", "$salt");
open(KEYFILE,">$keyfile") or die "Error: Cant write the Keyfile";
print KEYFILE chomp($crypted);
close (KEYFILE);
print ("\nHash = $crypted\n");
system("chmod 666 $keyfile");
print ("Keyfile saved to $keyfile!\nPermissions set to 666\nUpload it to your Clients now!\n");
}

sub readconfig() {
open(CONFIG,$cfgfile) or die "Error: Cant open $cfgfile";
my @config=<CONFIG>;
close(CONFIG);
chomp($port = $config[0]);
if ($port eq "") { die ("Error: No Port specified"); }
chomp($username = $config[1]);
       if ($username eq "") { die ("Error: No Username specified"); }
}

sub writeconfig() {
print ("Config\n------\n");
print ("\nPort: ");
my $port = <STDIN>;
if ($port =~ m/[a-z]+/) { die ("Port must be a number!"); }
       print ("\nUsername: ");
my $username = <STDIN>;
print ("Config written to $cfgfile\n");
my @config = ($port, $username,);
open(CONFIG,">$cfgfile");
print CONFIG (@config);
close (CONFIG);
if (-z "$keyfile") {
print ("No KeyFile present, well we generate one now...\n");
genpass();
} else {
print ("KeyFile allready present!\n");
}
}

sub server() {
readconfig();
my $proto = getprotobyname('tcp');
socket(SERVER, PF_INET, SOCK_STREAM, $proto) || die "FAILED: socket: $!";
setsockopt(SERVER, SOL_SOCKET, SO_REUSEADDR, pack ("l", 1)) || die "FAILED: setsockopt: $!";
bind(SERVER, sockaddr_in($port, INADDR_ANY)) || die "FAILED: bind: $!";
listen(SERVER, SOMAXCONN) || die "FAILED: listen: $!";

logmsg "Server started on Port $port";

my $waitedpid = 0;
my $paddr;

sub KILLER {
        our $waitedpid = wait;
        $SIG{CHLD} = \&KILLER;
        logmsg "Killed $waitedpid" . ($? ? " with exit $?" : '');
}
$SIG{CHLD} = \&KILLER;
for ($waitedpid = 0;
($paddr = accept(CLIENT,SERVER)) || $waitedpid;
$waitedpid = 0, close CLIENT)
{
next if $waitedpid and not $paddr;
my ($port,$iaddr) = sockaddr_in($paddr);
my $name = gethostbyaddr($iaddr,AF_INET);

logmsg "Connection from $name [",
inet_ntoa($iaddr), "]
at port $port";

spawn sub {
print "Hello there, $name\n";
};
}

sub spawn {
my $coderef = shift;
unless (@_ == 0 && $coderef && ref($coderef) eq 'CODE' {
confess "usage: spawn CODEREF";
}
my $pid;
if (!defined($pid = fork)) {
logmsg "cannot fork: $!";
return;
} elsif ($pid) {
logmsg "begat $pid";
return;
}
open (STDIN, "<&CLIENT") || die "cant dup client to stdin";
open (STDOUT, ">&CLIENT") || die "cant dup client ti stdout";
exit &$coderef();
}
}

sub help() {
print ("Valid commandline Options are:\n");
print ("-config -Initiates Configuration\n");
print ("-printcfg -Print Config\n");
print ("-server -Starts the Server\n");
print ("-genpass -Generates a new keyfile (1st-timers: Use -config instead!\n");
}

main {
$choosed = $ARGV[0];
if ($choosed eq '-config') {
       writeconfig();
       exit 0;
} elsif ($choosed eq '-printcfg') {
       readconfig();
print ("Port: $port");
print ("Username: $username");
       exit 0;
} elsif ($choosed eq '-genpass') {
       genpass();
       exit 0;
} elsif ($choosed eq '-server') {
       server();
       exit 0;
} else {
help();
       exit 0;
}
}

View full thread Simpler Threaded Server: ...und ich find den fehler nicht...