i have a script that listens on a port for simple text messages and then
executes sql to a remote mssql database . The script is started via crontab .
There are 2 problems :
1 . The line to the remote database appears to go to sleep in the sense that the script receives a null return from the database instead of a message containing the number of rows affected by the sql . The sql in such a case does not get executed against the database, i.e. no updates are done.
2. The line sometimes fails with 'connection refused' or 'Error: Global alarm in 5'
2 code excerpts that may be relevant :
The script TESTS the port at start and forks if the port is not accepting TEST messages :
Expand|Select|Wrap|Line Numbers
- $socket = IO::Socket::INET->new(
- PeerAddr => '10.100.1.232',
- PeerPort => 41004,
- Type => SOCK_STREAM,
- Proto => "tcp",
- );
- if ($socket) {
- if ($#ARGV >= 0) {
- $socket->send($ARGV[0]);
- } else {
- $socket->send('TEST');
- $mess = 'TEST send';
- wlog($mess);
- $socket->recv($instr,20);
- }
- $socket->close;
- wlog('Server running and using port 41004');
- exit;
- }
- #--- Fork to free child and close parent
- $pid = fork;
- if ($pid) {
- wlog('Closing parent process');
- close(LOG);
- exit;
- }
Expand|Select|Wrap|Line Numbers
- $server = IO::Socket::INET->new(
- LocalPort => 41004,
- Type => SOCK_STREAM,
- Reuse => 1,
- Listen => 10,
- ) or die wlog("Cannot bind to port ");
- while (($client,$peeradd) = $server->accept()) {
- ($clientport,$clientadd) = unpack_sockaddr_in($peeradd);
- $mess = "Server loop" . " " . inet_ntoa($clientadd) . " - " . $clientport;
- wlog($mess);
- #new
- $cadd = inet_ntoa($clientadd);
- print $cadd . "\n";
- #new
- if ($cadd eq '10.') {
- $mess = 'Bad store detected - abort';
- next;
- }
- $rc = $client->timeout(10);
- $client->recv($instr,200);
- # if message = TEST etc etc
- if (substr($instr,0,4) eq 'TEST') {
- $mess = 'TEST received';
- wlog($mess);
- $client->send('OK');
- next;
- }
- }
much appreciated if you could offer any advice,
regards,
Clem