#----------------------------------------------------------
# Process command line options.
#----------------------------------------------------------
GetOptions(
"userid=s" => \$userid,
"passwd=s" => \$passwd,
"tr_file|file|qt_file=s" => \$in_file,
"port=s" => \$port,
"log|log_fl:s" => \$log_file,
"verbosity|verbose:s" => \$verbosity,
"help|usage" => \$usage,
);
#----------------------------------------------------------
# Validate command line arguments.
#----------------------------------------------------------
validate_arguments()
or pod2usage(
-message => 'Incorrect usage!!',
-exitval => 2,
-verbose => 1,
-output => \*STDOUT,
-noperldoc => 0
);
#----------------------------------------------------------
# Open up a TCP socket and start waiting for the connection request.
#----------------------------------------------------------
# Create our socket.
$logger->debug("Making the socket.");
socket( SERVER, PF_INET, SOCK_STREAM, getprotobyname('tcp') );
$logger->debug("Socket made.");
# Set socket properties.
$logger->debug("Setting socket options: SO_REUSEADDR.");
setsockopt( SERVER, SOL_SOCKET, SO_REUSEADDR, 1 );
$logger->debug("Socket options set.");
# Bind the socket to our port and all ip's.
$logger->debug( "Binding the socket to the specified port $port "
. 'and all available IP addresses on the machine.' );
$my_addr = sockaddr_in( $port, INADDR_ANY );
unless ( bind( SERVER, $my_addr ) ) {
$logger->fatal("Couldn't bind to $port on this machine. $!");
$logger->fatal('Exiting with an exit code of 1.');
exit(1);
}
$logger->debug("Binding to port $port successful.");
# Initialize our listen queue before getting ready for passive open.
$logger->debug('Initialize our listen queue. Setting queue depth to SO
+MAXCONN');
unless ( listen( SERVER, SOMAXCONN ) ) {
$logger->fatal("Could not initialize listen'er queue for our socke
+t: $!");
$logger->fatal('Will exit out with an exit code of 1.');
exit(1);
}
#----------------------------------------------------------
# Now wait for client requests.
#----------------------------------------------------------
# Cool now lets wait for the SYN from client. (We do a passive open).
REQUEST:
while ( accept( CLIENT, SERVER ) ) {
# We got a request.
my ( $client_port, $client_ip_address );
my $client_sockaddr = getpeername(CLIENT);
( $client_port, $client_ip_address ) = sockaddr_in($client_sockadd
+r);
$client_ip_address = inet_ntoa($client_ip_address);
$logger->info(
'Got a connection from ' . $client_ip_address . ':' . $client_
+port );
# Let's clone off a copy of ourselves and get ready to accept()
# another client request.
if ( $child_pid = fork() ) {
# You are in the parent here.
close CLIENT;
next REQUEST;
}
# Check if the fork succeeded.
unless ( defined($child_pid) ) {
# Well it did not!!!
$logger->fatal("Could not fork () a child. $!");
$logger->fatal('Exiting with a status of 1');
# What do we do with children who are already fork()'d?
exit(1);
}
# Fork'd ok!! We are in the child SoupTCP server.
$logger->debug(
'Child PID:' . $$ . ' - Closing off unwanted filehandle SERVER
+.' );
close(SERVER);
# Keep our pipes piping hot.
$logger->debug( 'Child PID:' . $$
. ' - Flushing filehandle CLIENT after every write.' );
CLIENT->autoflush(1);
#------------------------------------------
# This info gets to the client!!
#------------------------------------------
print CLIENT 'Before READING DATA' . "\n";
# The reading should stop once we hit a line-feed.
local $/ = '\x0A';
# The first packet we expect from the client is a Login Request pa
+cket.
$client_data = <CLIENT>;
#------------------------------------------
# This info never gets to the client. (Buffered) OR
# The process never reaches this point and blocks on <CLIENT>.
# How do I debug it? Should I use sysread (and syswrite)? But then
+ it reads
# fixed length packets and I am getting variable length data packe
+ts!!
#------------------------------------------
print CLIENT 'After READING DATA' . "\n";
|