Solved

NEED HELP! - streaming text for cgi web chat

Posted on 1997-07-17
2
2,878 Views
Last Modified: 2008-03-17
Ok, PERL gurus! I am working on a project to allow streaming(scrolling) text for a cgi chat server in a
multi-framed HTML document. I have seen several client / server models and sample code, but cannot seem to get it straight. The problem I see with many of the
sample daemons is that they create way too much server
load. ex. The have to spawn a child process to
communicate with a newly opened socket. I need a
server to accept incoming sockets and to print messages
recieved from a socket to all other open connections
from ONE process(I guess this would be similar to the
innd process). I will be INCREDIBLY grateful for some
advice as this is an extremely important feature for me to
add. Thank you very much.


------ Text Omitted .... Code needing fixed is listed below


I feel the code is almost there, but it has bugs in it. I've set the server to listen on a particular port. Rather then writing the client, I've just telneted in to test the
response. Once the socket is opened, I type in data(then of course the steam will be ended via a \n) and the data is sent back to my socket and it is then closed. For some
reason the text is not being broadcasted to other open connections. I'd really appreciate further help if it is possible. Also, once completed this version should be
able to support up to and even beyond 50 - 100 simultaneous users. Is this feasable with this implementation. Here is my code so far. Also The cgi web-based interface will be in a
3 frame window.  The upper left frame will recieve all new
text written by other chatters.  The lower frame will allow the user to add new text, which will in turn be sent to the upper right frame.  The third frame will be used for customizable options.
Thank you greatly! - Eric

               #!/usr/bin/perl
                 
               ($port) = @ARGV;
               $port = 2345 unless $port;
                 
               $AF_INET = 2;
               $SOCK_STREAM = 1;
                 
               $sockaddr = 'S n a4 x8';
                 
               ($name, $aliases, $proto) = getprotobyname('tcp');
               if ($port !~ /^\d+$/) {
                   ($name, $aliases, $port) = getservbyport($port, 'tcp');
               }
                 
               print "Port = $port\n";
               $this = pack($sockaddr, $AF_INET, $port, "\0\0\0\0");
                 
               socket(LISTEN, $AF_INET, $SOCK_STREAM, $proto) || die "socket: $!";
               bind(LISTEN,$this) || die "bind: $!";
               listen(LISTEN,5) || die "connect: $!";
                 
               select(LISTEN); $| = 1; select(stdout);
                 
               print "Listening for connections....\n";
                 
               while (1) {
                   # each time, create the select bit array
                   $bits = 0;
                   for (@CLIENTS) {
                   vec ($bits, fileno ($_), 1) = 1;
                   }
                   vec ($bits, fileno (LISTEN), 1) = 1;
                 
                   #OK, wait until something comes in.
                   $nfound = select($rbits=$bits,undef,$ebits=$bits,undef);
                 
                   #OK, something woke us up.
                   # see if it is a new connection...
                   if (vec($rbits, fileno(LISTEN), 1)) {
                   # new connection
                   accept (NS, LISTEN);
                   push (@CLIENTS, NS); }
                 
                   #no, try the clients
                   $incoming = undef;
                   for (@CLIENTS) {
                   if (vec ($rbits, fileno ($_), 1)) {
                   #OK, this client's got something, read a line...
                   $incoming .= <$_>; } }
                 
                   # next, send $incoming to all clients...
                   if ($incoming) {
                   for (@CLIENTS) {
                   print $_ "$incoming\n"; } }
                   }
0
Comment
Question by:knight76
  • 2
2 Comments
 
LVL 84

Expert Comment

by:ozo
ID: 1204925
Using the -w flag says:
Unquoted string "stdout" may clash with future reserved word at line 24.
Name "main::ebits" used only once: possible typo at line 37.
Name "main::NS" used only once: possible typo at line 43.
Name "main::nfound" used only once: possible typo at line 37.

 
0
 
LVL 84

Accepted Solution

by:
ozo earned 90 total points
ID: 1204926
Perhaps more tellingly,
use strict;
gives the error
Bareword "NS" not allowed while "strict subs" in use at line 45.

Could it be that
      push (@CLIENTS, *NS);
was what you meant?

0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
perl script 4 97
pattern matching in perl 2 101
Perl 101 11 72
Union rows in array that have common elements (Part 2) 4 70
On Microsoft Windows, if  when you click or type the name of a .pl file, you get an error "is not recognized as an internal or external command, operable program or batch file", then this means you do not have the .pl file extension associated with …
Many time we need to work with multiple files all together. If its windows system then we can use some GUI based editor to accomplish our task. But what if you are on putty or have only CLI(Command Line Interface) as an option to  edit your files. I…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …

867 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now