Hi all, I am trying to write a TCP server in perl, and currently have one working except for the fact that it only seems to be able to handle one connection at a time... can anyone point me in the right direction if it needs to deal with more than one connection at a time?? Code below:

#!/usr/bin/perl -wT use strict; use IO::Socket; use POSIX qw(setsid); use vars qw( $pid $server $client $read_tmp @numbers $number $msg $dat +e $cnt ); umask 0177; $| = 1; # Do not allow this server to be run as root. if( $< != 0 ) { die "You must run this as root.\n"; } $pid = fork(); if( $pid ) { exit; } else { setsid or die "Could not start new session.\n"; $server = IO::Socket::INET->new( Type => SOCK_STREAM, LocalPort => "5001", Proto => "tcp", Reuse => "1", Listen => "20", Blocking => "0" ) or die "Could not bind to port 5001.\n"; #print "Daemon now running in the background using PID [" . $$ + . "].\n"; write_pid(); $cnt = 0; } while( $client = $server->accept() ) { $client->autoflush(1); while( defined( $client->recv( $read_tmp, 1024 ) ) ) { chomp $read_tmp; # Check the data we're being given for malicious char +acters. if( $read_tmp =~ /quit/ ) { server_exit( "501 Exiting normally." ); } if( $read_tmp =~ /^send ([0-9,]+) (.+)$/i ) { @numbers = split( ",", $1 ); $msg = $2; foreach $number ( @numbers ) { # Get the epoch... $date = time; # Increment the counter... $cnt++; open( SMS, "> /var/spool/sms/OTHER/sms +.$date.$cnt" ) || die "Couldn't open SMS file: $!\n"; print SMS << "end"; To: $number $msg end close( SMS ); } print $client "Message sent.\n"; } else { server_exit( "504 Invalid characters used in s +erver query." ); } } } sub server_exit { my( $errmsg ) = @_; if( defined( $msg ) && defined( $client ) ) { print $client "$errmsg\n"; } close( $client ); } sub write_pid { my( $pid ) = $$; open( PID, "> /var/run/sms_server.pid" ) || die "Could not ope +n PIDFILE.\n"; print PID $$ . "\n"; close( PID ); }

Any help would be appreciated. Thanks, Marc

edited by ybiC: Balanced <readmore> tags around code for frontpaged node


In reply to IO:Socket question... sort of :) by marcs

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.