Hi,
I'm trying to create a network game where the server part is a cgi script and the client is a flash embedded into an html page. Data exchange is performed via sockets.
Now I'm using the following scheme:
a user starts the game by running "start.cgi" in his browser. Firstly,this program returns a location to the flash client. Secondly, it defines whether the main script "server.cgi" is already started by checking a "pid" file and run "server.cgi" it if necessary.
The server part "server.cgi" writes its pid into "pid" file when it starts, does all the job and writes '0' into "pid" at exit.
Also "server.cgi" exits if there is no socket connection in 15 seconds.
The problem is that sometimes it works and sometimes it doesn't. And I don't know why. I suppose that maybe something is wrong with starting "server.cgi" from within "start.cgi" though I'm not sure.
Any ideas about what can be wrong here or any better ways of making the whole server part will be very appreciated.
start.cgi code
--------------
#!/usr/bin/perl -w
open(PID, "< pid") || die "Can't open PID file: $!";
$pid = <PID>;
close PID;
if(defined $pid){
print "Location: \n\n";
if($pid==0){
open(TST, "> tst") || die "Can't open PID file: $!";
print TST "start";
close TST;
system('/var/ &');
}
}
server.cgi code
--------------
#!/usr/bin/perl -w
...
open(PID, "> pid") || die "Can't open PID file: $!";
print PID $$;
close PID;
...
eval {
local $SIG{ALRM} = sub {
print "alarmed\n";
die "no sockets allowed";
};
alarm 15;
... main part of code here
};
if ($@ =~ /no sockets allowed/) {
open(PID, "> pid") || die "Can't open PID file: $!";
print PID '0';
close PID;
print "alarm dying...\n";
exit 0;
}
...
I'm trying to create a network game where the server part is a cgi script and the client is a flash embedded into an html page. Data exchange is performed via sockets.
Now I'm using the following scheme:
a user starts the game by running "start.cgi" in his browser. Firstly,this program returns a location to the flash client. Secondly, it defines whether the main script "server.cgi" is already started by checking a "pid" file and run "server.cgi" it if necessary.
The server part "server.cgi" writes its pid into "pid" file when it starts, does all the job and writes '0' into "pid" at exit.
Also "server.cgi" exits if there is no socket connection in 15 seconds.
The problem is that sometimes it works and sometimes it doesn't. And I don't know why. I suppose that maybe something is wrong with starting "server.cgi" from within "start.cgi" though I'm not sure.
Any ideas about what can be wrong here or any better ways of making the whole server part will be very appreciated.
start.cgi code
--------------
#!/usr/bin/perl -w
open(PID, "< pid") || die "Can't open PID file: $!";
$pid = <PID>;
close PID;
if(defined $pid){
print "Location: \n\n";
if($pid==0){
open(TST, "> tst") || die "Can't open PID file: $!";
print TST "start";
close TST;
system('/var/ &');
}
}
server.cgi code
--------------
#!/usr/bin/perl -w
...
open(PID, "> pid") || die "Can't open PID file: $!";
print PID $$;
close PID;
...
eval {
local $SIG{ALRM} = sub {
print "alarmed\n";
die "no sockets allowed";
};
alarm 15;
... main part of code here
};
if ($@ =~ /no sockets allowed/) {
open(PID, "> pid") || die "Can't open PID file: $!";
print PID '0';
close PID;
print "alarm dying...\n";
exit 0;
}
...