Oct 10, 2008, 5:45 AM
Post #1 of 1
Socket server exits after client disconnects
I want my socket server to keep listening for client connections after a client disconnects. any ideas
Heres my code:
# Author: Gerhard Oosthuizen <firstname.lastname@example.org>
# This script recieves ipx srings fron remote reader
# management systems and enters it into a predefined
# MySQL database.
#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;
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();
my @strd = ipxParse($_);
$sth->execute(@strd) or die "Couldn't execute statement: " . $sth->errstr;
# Close the socket
# 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.