You are viewing a plain text version of this content. The canonical link for it is here.
Posted to modperl@perl.apache.org by "Purcell, Scott" <sp...@ltcgroup.com> on 2001/06/19 19:17:43 UTC

Web Site Question

Hello,

First: I am running apache/mod-perl on a NT box as a web server. Sorry it is
not unix, it is the only tool I can have.

Kind of a forward question, but I am curious about something. My large web
site talks to a back-end product that uses an API and I use the IO::Socket
to talk to it. It is a pure socket application. It takes the place of a
database back-end. 

So my question is, should I create a global socket handle, and then use it
all the time, or is it best to just call for the handle each time I need
request some data?

The reason I ask is as follows: I have the code below which works, but I
cannot seem to ask two socket calls back to back? I don't know why. Maybe it
is a learning curve I have to perfect.

Anyway, I figured I would ask people who would know for their input. So if
anyone has any opinions, could you fill me in?

Thanks for your time,
Scott Purcell

#### code that works, called from another file with a &ask routine.
use strict;
use vars qw($socket);

    $socket = IO::Socket::INET->new(Proto => 'tcp',PeerAddr =>
'208.238.162.204',PeerPort => '8000') or die ("Socket issue @!");
    $socket->autoflush(1);


sub ask {
    ### perl apache notes for my sanity during testing
  my $counter = 0;
  for (1..5) {
    increment_counter();
  }
  
  sub increment_counter{
    $counter++;
    print "Counter is equal to $counter !\r\n";
  }
   #### end counter eg for sanity
    use IO::Socket;
    use CGI;
    use CGI::Carp qw(fatalsToBrowser);
    # 
    my $q = CGI->new;
    print $q->header;
    print $q->start_html;

    my $querystring = "\' \'\$USER_NAME";
    my $fieldlist   = "USER_NAME\nPASSWORD\n";
    my $db = "/DISK2/VBank/bin/sysdbase/dbuser.dbf";
    
    my $message  =
"DBASEQUERY\nSYSTEM\n$querystring\nLIST\tFIELDS\n$fieldlist\nENDLIST\nLIST\t
DBASES\n$db\nENDLIST\n";
    
    my $msgid = int(rand(1000));
    $message = $msgid . "\t" . $message;
    my $msgsize = length($message);
    print "$message IS OUR MESSAGE<BR>\n";
    print $socket "\%BEGIN\t$msgsize\n$message";


    my $err = 0;
    my $header;
    my $response;
    my $cc;

    while ($err < 50) {
        if (read($socket, $cc, 1, 0) == 1) {
            $header .= $cc;
            print "$header is header<br>\n";
            if ($header =~ /^\%BEGIN\s+(\d+)\r?\n/) {
                my $rspsize = $1;
                read($socket, $response, $rspsize, 0);
                print "$response is response<br>\n";
                exit;
            }
        } else {
            print "NO data<br>\n";
            exit;
        }
        $err++;
    }

    
}