Echo server: Difference between revisions

Content added Content deleted
(Refine the task with the (obvious to me) requirement to be able to deal with more than a single line per connection)
(added perl implementation)
Line 536: Line 536:
forkIO (echo acc)</lang>
forkIO (echo acc)</lang>


=={{header|Perl}}==
This server will run indefinitely listening in the port 12321 and [http://perldoc.perl.org/functions/fork.html forking] every time a client connects, the childs listen to the client and write back.
<lang perl>use IO::Socket;
my $sock = new IO::Socket::INET (
LocalHost => '127.0.0.1',
LocalPort => '12321',
Proto => 'tcp',
Listen => 1, # maximum queued connections
Reuse => 1,
);
die "Could not create socket: $!\n" unless $sock;

print "server is waiting clients..\n";
my $con;
while(1)
{
$con = $sock->accept();
last if !fork; # create a child to listen to the new client
# and keep waiting more clients
}

print "child listening..\n";

print $con $_ while(<$con>);

print "child dead\n";
close($sock);</lang>
=={{header|Python}}==
=={{header|Python}}==
{{works with|Python|2.3 or above}}
{{works with|Python|2.3 or above}}