Создаем приложение для многопользовательской беседы
<FRAME NAME="_display" SCR="chat1.cgi">
<FRAME NAME="_data" SRC="chat2.cgi">
</FRAMESET>
</HTML>
Листинг 21.2. chat1.cgi
#!/usr/lib/perl
use CGI;
use Fcntl;
$co = new CGI;
open (DATA1, "<chat1.dat") or die "Could not open data
file.";
lockfile(DATA1);
$text1 = < DATA1>;
unlockfile(DATA1);
close DATA1;
open (DATA2, "<chat2.dat") or die "Could not open datafile.";
lockfile(DATA2);
$text1 = < DATA2>;
unlockfile(DATA2);
close DATA2;
print
$co->header,
"<meta HTTP-EQUIV=\"refresh\" CONTENT=\"5\">",
$co->start_html(
-title=>'Chat Example',
-author=>'Steve',
-target=>'_display',
Создаем приложение для многопользовательской беседы
-BGCOLOR=>'white',
-LINK=>'red'
),
$co->center($co->h1('Multi-User Chat')),
$co->p,
$co->p,
$co->center($text1),
$co->p,
$co->center($text2),
$co->end_html;
exit;
sub lockfile
{
my $count = 0;
my $handle = shift;
until (flock($handle, 2)) {
sleep .10;
if(++$count > 50) {
print
$co->header,
"<meta HTTP-EQUIV=\"refresh\" CONTENT=\"5\">",
$co->start_html(
-title=>'Chat Example',
-author=>'Steve',
-target=>'_display',
-BGCOLOR=>'white',
-LINK=>'red'
),
$co->center($co->h1('Server too busy')),
$co->end_html;
exit;
}
}
}
sub unlockfile
{
my $handle = shift;
flock($handle, 8);
}
Листинг 21.3. chat2.cgi
#!/usr/lib/perl
use CGI;
use Fcntl;
$co = new CGI;
if($co->param()) {
продолжение И
$name =$co->param('username');
$name =~ s/</</;
$text =$co->param('textarea');
$text =~ s/</</;
if ($text) {
my $oldtext; open (OLDDATA, "<chat2.dat" or die "Could not open
data file."
lockfile(OLDDATA);
$oldtext= <OLDDATA>;
unlockfile(OLDDATA);
close OLDDATA;
open (DATA, "<chat1.dat" or die "Could not open
Содержание Назад Вперед