#!/usr/bin/perl use IO::Socket; use Sys::Hostname; use POSIX qw(:sys_wait_h); #testserver $version=".00"; $debug=0; $myip='192.168.0.2'; $true=1; #---------------------------------- BetQ.d Main ---------------------- +-------------------- &do_socket; #---------------------------------- BetQ.d Main ---------------------- +-------------------- #---------------------------------- Subroutines ---------------------- +-------------------- sub S_print { print $new_sock (@_); } sub do_socket { $SIG{CHLD} = \&REAP; $sock = new IO::Socket::INET( LocalHost => $myip, LocalPort => 8721, Proto => 'tcp', Listen => SOMAXCONN, Reuse => 1); $sock or die "no socket :$!"; STDOUT->autoflush(1); while (($new_sock, $c_addr) = $sock->accept()) { ($client_port, $c_ip) = sockaddr_in($c_addr); $client_ipnum = inet_ntoa($c_ip); $client_host = gethostbyaddr($c_ip, AF_INET); # execute a fork, if this is # the parent, its work is done, # go straight to continue $i=0; $done=0; $betsend=0; print "[$client_ipnum] $client_host connected\n"; next if $kid = fork; die "fork: $!" unless defined $kid; # child now... # close the server - not needed #print "closing sock\n"; close $sock; #print "Forked child printing CMDSTART to client\n"; $date=`date`; print $new_sock "[$client_ipnum] $client_host $date co +nnection attempt logged\n"; print $new_sock "This is a private server.\n"; print $new_sock "username:\n"; if (defined($lineread = <$new_sock>)) { print "un received: $lineread"; print $new_sock "$lineread"; print $new_sock "password:\n"; } if (defined($lineread = <$new_sock>)) { print "pw received: $lineread"; print $new_sock "$lineread"; } if ($lineread eq "start\n"){ print $new_sock "Accepted\n"; $first=1; &Send_Bets; }else{ sleep(5); print $new_sock "Denied and logged\n"; } print "lineread is -->$lineread<--\n"; print "Breaking connection to [$client_ipnum] $client_ +host\n"; exit; } continue { # parent closes the client since # it is not needed print "closing new sock\n"; close $new_sock; } print "IN 7\n"; } sub REAP { print "reaping\n"; 1 until (-1 == waitpid(-1, WNOHANG)); $SIG{CHLD} = \&REAP; } sub Send_Bets { $done=0; while(!$done){ if ($first || defined($lineread = <$new_sock>)) { print "startcmd is $lineread"; $first=0; for ($f=0;$f<=10;$f++) { print "sending: $f"; &S_print (" sent $f\n"); } print "sending: EOF\n"; &S_print ("EOF\n"); } if ($lineread ne "start\n"){ $done=$true; } } }
edited: Mon Nov 18 15:30:18 2002 by jeffa - s/pre/code/g
In reply to Socket question Help! by drake50
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |