CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
Socket server exits after client disconnects

 



thejerryman
New User

Oct 10, 2008, 5:45 AM

Post #1 of 1 (312 views)
Socket server exits after client disconnects Can't Post

Hi all,

I want my socket server to keep listening for client connections after a client disconnects. any ideas

Heres my code:


Code
 
#!/usr/bin/perl -w
# Author: Gerhard Oosthuizen <gehard@ipico.co.za>
#
# This script recieves ipx srings fron remote reader
# management systems and enters it into a predefined
# MySQL database.

use strict;
use DBI;
use IO::Socket;
#sub-routines
sub ipxParse;

#Start the MySQL connection
my $dbh = DBI->connect("DBI:mysql:database=testDB;host=localhost","usetrn", "passw")
or die die "Couldn't connect to database: " . DBI->errstr;
#Create the MySQL Expresion.
my $sth = $dbh->prepare("INSERT INTO rawlog (reader, uid , icount, qcount, r_datetime, location) VALUES (?,?,?,?,?,?)")
or die "Couldn't prepare statement: " . $dbh->errstr;
#Create Socket
my $sock = new IO::Socket::INET (
LocalHost => 'localhost',
LocalPort => '11000',
Proto => 'tcp',
Listen => 1,
ReuseAddr => 1
) or die "Could not create socket: $!\n";

my $new_sock = $sock->accept();

while(<$new_sock>) {
print $_;
my @strd = ipxParse($_);
$sth->execute(@strd) or die "Couldn't execute statement: " . $sth->errstr;
$sth->finish;
}

# Close the socket
close($sock);
# Disconenct from the DB
$dbh->disconnect or die "Couldn't disconnect from DB: " . dbh->errstr;


What happens now is that the strings are processed when the first client connects. When the first client disconnects the script exits.

Do I just put the processing part ins while(1) loop waiting for more connections or do I have to use threads of some sort?

This is ment to be a server daemon that processed data and enters it in to a DB.

Thanks!

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives