Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

In the following code, does the program stop running after it reads something from the client? If so, how can I keep it listening 24/7?
use IO::Socket; $server = IO::Socket::INET->new ( LocalPort => 1116, Type => SOCK_STREAM, Reuse => 1, Listen => 5 ) or die "Could not open port.\n"; while ($client = $server->accept()) { $line = <$client>; print = $line; } close($server);

Replies are listed 'Best First'.
Re: New at socket programming
by dws (Chancellor) on Apr 19, 2002 at 03:14 UTC
    Do you really mean to write   print = $line; or is   print $line; be what you intend?

    It's almost always good to start a script with   use strict; That would have made this problem instantly apparent. You probably want to close $client, too.

      Sorry, I meant to write print $line; instead of print = $line; So after this code "hears" something from the client, will it listen again for more data from the client or will it stop running?
        What you've got should work. It's nearly identical to an example on page 192 of Advanced Perl Programming (which happened to be within reach).

        It will fail if a client connects, sends nothing, then closes the socket.

        What do you see happening?

Re: New at socket programming
by jeffenstein (Hermit) on Apr 19, 2002 at 09:47 UTC

    With the listed code, the program will only read one line from the client when it connects, and then it will disconnect from the client.

    You might want to do something like this:

    $server = IO::Socket::INET->new ( LocalPort => 1116, Type => SOCK_STREAM, Reuse => 1, Listen => 5 ) or die "Could not open port.\n"; while ($client = $server->accept()) { # # start a new process for each new connection # my $pid = fork; if( $pid < 0){ die "fork error: $!"; }elsif( $pid == 0){ # # child process # do whatever you want with the connection here # while( my $line = <$client>){ print $line; } close($client); } # # parent falls through to listen for new connections # } # should never reach here #close( $server);

    This lets you do whatever processing you want with the client, while still accepting new client connections.

      Thanks for the advice. I think your recommendation is the way to go.
Re: New at socket programming
by mslattery (Initiate) on Apr 19, 2002 at 12:35 UTC
    I curious about what OS your running on.

    The reason being that I'm new to IO::Socket module
    programing as well and ran into several problems while
    looking at the recommeded sources.

    It took going through several examples and combining what I
    saw as "good" programing practices to come up with a
    version that works on my win32 platform.

    So once you understand the basics of IO::Socket (which your
    on you way with the inital code you've written. Then
    look at these two examples.

    http://pleac.sourceforge.net/pleac_perl/sockets.html

    http://www.perlfect.com/articles/select.shtml

    Also, here was my problem, but it was pretty complex at this point. I'm handing a nonforking server that supports nonblocking IO.

    http://www.perlmonks.com/index.pl?node_id=159578

    Good Luck,
    mslattery

Re: New at socket programming
by TheHobbit (Pilgrim) on Apr 19, 2002 at 10:13 UTC

    This looks suspiciously like homework to me...As a teacher, who gives this sort of homework, I can not approve of your idea to have the monks doing it for you...

    Do a little more work by yourself. As some monks sugested, try at leas putting in use strict and a -w after the shebang.

    Also, read the doc on Socket and IO::Socket::INET. Finaly, you could look at the wonderful Network Programming with Perl by L.D.Stein.

    Cheers
    Leo TheHobbit
    GED/CS d? s-:++ a+ C++ UL+++ P+++>+++++ E+ W++ N+ o K? !w O? M V PS+++
    PE-- Y+ PPG+ t++ 5? X-- R+ tv+ b+++ DI? D G++ e*(++++) h r++ y+++(*)
      Hello. My question is indeed sort of like homework but I'm no longer a student. My company creates wireless applications for GSM telcos. Recently, because of excessively large SMS volumes, we've been requested to directly connect to the SMSC (Short Message Service Center), a machine designed by CMG. I posed my question to solicit additional information regarding the stability of such a connection. Thanks.