http://qs1969.pair.com?node_id=702469


in reply to A Simple Socket Server Using 'inetd'

Based on the sample provided in this page, I have written a server handler and a client - the purpose is which is that when the client opens a socket connection on a specific port and sends data, the server receives it, parses it and logs it in a log file. The inetd and the services files have been configured and I know that it is done correctly because when my server program has compilation errors, the corresponding error message is displayed on the console where I run my client program. But when all the errors have been fixed, the job that the server program is supposed to be doing is not getting done - log file is not getting updated and the response it spits out is not getting communicated to the client. Can someone please review the code and let me know what is it that I am doing wrong? Here are the codes I have for the server program and the client program:
The Server Program:
#!/usr/local/bin/perl5.00506 -Tw # add the -Tw for testing use strict; # use Time::Local; use CGI; use CGI::Carp qw(fatalsToBrowser); # use DB_File; # clean up path for security $ENV{PATH} = "/usr/local/bin"; $ENV{ENV} = ""; use Time::Local; my($day, $month, $year) = (localtime)[3,4,5]; my ($val, $send_ph_num, $mail, $var, $val_untaint, $tmp, $pin, $pagese +rvice, $backupmail); my %db; my (@output_msg_array, $msgto, $msg, $phonenumber, $sendphone, $sendba +ckup, $pager_email); my ($input_data); my $old_fh = select(STDOUT); $| = 1; select($old_fh); $input_data = <STDIN>; chomp $input_data; # remove the first character from the input string $input_data = substr($input_data, 2, (length($input_data)-1)); # Parse the input data my @values = split(' ',$input_data,5); my $cdsid = $values[0]; $msgto = $values[1]; $sendbackup = $values[2]; $msg = substr($values[4],1,(length($values[4])-1)); $phonenumber = ""; $sendphone = "N"; #print "Parsed Text\n"; #print "Parsed Text as Sender $cdsid To $msgto Message $msg\n"; open (LOGFILE, ">> log/batch_call.log"); printf LOGFILE ("%04d-%02d-%02d -- %s, %s, %s\n", $year+1900, $month+1 +, $day, $cdsid, $msgto, $msg); close(LOGFILE); print "Logged message successfully\n"; exit;
The Client program:
#!/usr/local/bin/perl/perl -w use IO::Socket; my ($remote_host, $remote_port, $socket, $answer); $remote_host = "x.y.com"; $remote_port = "5060"; print "Connecting to remote host....\n"; $socket = IO::Socket::INET->new(PeerAddr => $remote_host, PeerPort => $remote_po +rt, Proto => "tcp", Type => SOCK_STREAM) or die "Couldn't connect to $remote_host at $remote_po +rt: $@\n"; $socket->autoflush(1); print $socket "\x01jchakrab jchakrab N N Testing Text Pager thru Socke +t...\x10"; $answer = <$socket>; print ("$answer\n"); close($socket); exit;