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

Hi Monks, I am trying to write a script that will listen on a certain port "7777" for connections, the connection will be persistant and will send data strings to that port / server i would like to know if someone has some code lying around on how to achieve this and simpley log all the data to a txt file , is this even possible?? thanks kenny

Replies are listed 'Best First'.
Re: IO::Sockets server
by Rhandom (Curate) on May 17, 2007 at 14:44 UTC
    I'd use Net::Server.
    package Foo; use base qw(Net::Server); open(my $log, '>>', 'log.txt') || die "Couldn't open log.txt: $!"; Foo->run(port => 7777); sub process_request { while (<STDIN>) { print "You said: $_"; print $log "They said: $_"; } }


    There are many many options available including Fork, PreFork, Multiplex, and others. See the perldoc for Net::Server.

    my @a=qw(random brilliant braindead); print $a[rand(@a)];
      Thanks guys, I have the following and its working great:
      #!/usr/bin/perl -w my $socket; my $port = 7777; my $host; use IO::Socket; use Sys::Hostname; $SIG{CHLD} = sub {wait ()}; $host = hostname(); # START LOOP HERE my $main_sock = new IO::Socket::INET (LocalHost => $host, LocalPort => $port, Listen => 5, Proto => 'tcp', Reuse => 1, ); if (!$main_sock) { die "Socket could not be created. Reason: $!\n"; } my $buf; my $new_sock; my $pid; while ($new_sock = $main_sock->accept()) { $pid = fork(); if ($pid == 0) { # Child process while (defined ($buf = <$new_sock>)) { # do something with $buf .... my @lines = split(/\|/,$buf); exit(0); # Child process exits when it is done. } } # else 'tis the parent process, which goes back to accept() } close ($main_sock); exit; However how do i save the data recieved to a log file? say /usr/home/l +og.txt
Re: IO::Sockets server
by syphilis (Archbishop) on May 17, 2007 at 13:53 UTC
    Hi kenny,
    Something like (untested):
    use warnings; no warnings 'once'; # suppress warning re $client_address use IO::Socket; $server_port = '7777'; open ($rd, '>>', 'log.txt') or die "Couldn't open log.txt for appendin +g: $!"; $server = IO::Socket::INET->new( LocalPort => $server_port, Type => SOCK_STREAM, Listen => 1 ) or die "Couldn't be a tcp server on port $server_port : $@\n$!\n"; while(1) { print "Server waiting\n"; ($client, $client_address) = $server->accept(); $data_read = <$client>; print $rd $data_read; close($client); }
    (That's adapted from an actual script I use on Windoze ... ymmv.)

    Cheers,
    Rob
Re: IO::Sockets server
by shmem (Chancellor) on May 17, 2007 at 13:35 UTC
    See perlipc, section Sockets: Client/Server Communication for a start. Then, read (and use) IO::Socket.

    --shmem

    _($_=" "x(1<<5)."?\n".q·/)Oo.  G°\        /
                                  /\_¯/(q    /
    ----------------------------  \__(m.====·.(_("always off the crowd"))."·
    ");sub _{s./.($e="'Itrs `mnsgdq Gdbj O`qkdq")=~y/"-y/#-z/;$e.e && print}