From j.l.shipman at cox.net Wed Jan 24 21:32:49 2007 From: j.l.shipman at cox.net (Jeffery Shipman) Date: Wed Jan 24 22:25:19 2007 Subject: [HRPM] webserver and client Message-ID: Hi, I am not sure if this is the right forum. I am having difficult getting my server to read the data from my client. The client is pretty short using Socket library. The server receives the connection, then sends some data. I would like to send data from the client first. How do you send information over it from the client side. client #!/usr/bin/perl -w use strict; use Socket; my ($remote,$port, $iaddr, $paddr, $proto, $line); $remote = shift || 'localhost'; $port = shift || 2345; # random port if ($port =~ /\D/) { $port = getservbyname($port, 'tcp') } die "No port" unless $port; $iaddr = inet_aton($remote) || die "no host: $remote"; $paddr = sockaddr_in($port, $iaddr); $proto = getprotobyname('tcp'); socket(SOCK, PF_INET, SOCK_STREAM, $proto) || die "socket: $!"; connect(SOCK, $paddr) || die "connect: $!"; while (defined($line = )) { #print response back from the server. print $line; } close (SOCK) || die "close: $!"; exit; The server is much longer. #!/usr/bin/perl -w use strict; BEGIN { $ENV{PATH} = '/usr/ucb:/bin' } use Socket; use Carp; my $EOL = "\015\012"; my $response; my @method; my $record; my @mimes; my @codes; my $file; my $line; #my $JStemp; sub spawn; # forward declaration sub logmsg { print "$0 $$: @_ at ", scalar localtime, "\n" } my $port = shift || 1234 ; my $proto = getprotobyname('tcp'); ($port) = $port =~ /^(\d+)$/ or die "invalid port"; socket(JServer, PF_INET, SOCK_STREAM, $proto) || die "socket: $!"; setsockopt(JServer, SOL_SOCKET, SO_REUSEADDR, pack("l", 1)) || die "setsockopt: $!"; bind(JServer, sockaddr_in($port, INADDR_ANY)) || die "bind: $!"; listen(JServer,SOMAXCONN) || die "listen: $!"; logmsg "JServer started on port $port"; #*******************Load config files******************** #open up methods file if (! open METHOD, ") { push @method, $record; } close(METHOD); #open up mimes file if (! open MIMES, ") { push @mimes, $record; } close(MIMES); #open up codes file if (! open CODES, ") { push @codes, $record; } close(CODES); #open up html file used for producing status codes sub htmlResponse { if (! open HTML, ") { $fileString .= $_; } close(HTML); $fileString =~ s/##TITLE##/$title/g; $fileString =~ s/##BODY##/$body/g; # if (! open RESPOND, ">config/respond") # { # die "Can't open respond file: $!"; # } # # select RESPOND; # print $fileString; # select STDOUT; # close(RESPOND); return $fileString; } sub dirOutput { my $dirname = $_[0]; opendir(DIR, $dirname) or die "can't opendir $dirname: $!"; my $fileString = ""; while (defined($file = readdir(DIR))) { #prepend with correct ~address, link info and
$fileString .= $file . "\n"; } closedir(DIR); return $fileString; } #print &dirOutput("a1-test"); my $waitedpid = 0; my $paddr; use POSIX ":sys_wait_h"; use Errno; sub REAPER { local $!; # don't let waitpid() overwrite current error while ((my $pid = waitpid(-1,WNOHANG)) > 0 && WIFEXITED($?)) { logmsg "reaped $waitedpid" . ($? ? " with exit $?" : ''); } $SIG{CHLD} = \&REAPER; # loathe sysV } $SIG{CHLD} = \&REAPER; while(1) { $paddr = accept(Client, JServer) || do { # try again if accept() returned because a signal was received next if $!{EINTR}; die "accept: $!"; }; my ($port, $iaddr) = sockaddr_in($paddr); my $name = gethostbyaddr($iaddr, AF_INET); my ($data_read, $maxlen, $flags); $maxlen = 200; $flags = 0; logmsg "connection from $name [", inet_ntoa($iaddr), "] at port $port"; #get input before fork ??????? spawn sub { $|=1; #this is were the code is sent to the client #$response = &htmlResponse("Hello there", "testing testing"); #print $response; #print $EOL; #signifies in of transmission #can't get data from client #print $EOL; #signifies in of transmission print "Hello there, $name, it's now ", scalar localtime, $EOL; }; close Client; } 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; # I'm the parent } # else I'm the child -- go spawn open(STDIN, "<&Client") || die "can't dup client to stdin"; #my $test = ; #print $test; open(STDOUT, ">&Client") || die "can't dup client to stdout"; ## open(STDERR, ">&STDOUT") || die "can't dup stdout to stderr"; exit &$coderef(); } Thanks Jeffery Shipman j.l.shipman@cox.net -------------- next part -------------- An HTML attachment was scrubbed... URL: http://lists.fini.net/pipermail/hrpm/attachments/20070124/dc380a06/attachment-0001.html