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

  Main Index MAIN
Search Posts SEARCH
Who's Online WHO'S
Log in LOG

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


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:

#!/usr/bin/perl -w
# Author: Gerhard Oosthuizen <>
# 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 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;

# 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.



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

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