in reply to IO::Socket simple server
The questions I have are: After a client disconnects, do you want to do another accept? (seems like yes),
and Do you want to have simultaneous conversations with multiple clients? (seems like no).
Otherwise, just some simple tweaks...
#!/usr/bin/perl use strict; # https://perlmonks.org/?node_id=11152468 use warnings; $SIG{__WARN__} = sub { die $@ }; use feature 'say'; use IO::Socket qw(AF_INET AF_UNIX SOCK_STREAM SHUT_WR); use Data::Dumper; my $server = IO::Socket::INET->new( LocalPort => 3333, ReusePort => 1, Listen => 5, ) or die "Can't open socket: $IO::Socket::errstr"; while (1) { say "Waiting on 3333"; my $client = $server->accept() or die "accept failed on $!"; my $client_address = $client->peerhost(); my $client_port = $client->peerport(); say "Connection from $client_address:$client_port"; while( defined $client->recv(my $data, 1024) ) { say "received data: $data"; length $data or last; if ($data =~ m/99/) { $data = "We've received a string starting with 99\n"; } if ($data =~ m/63/) { $data = "We've received a string starting with 63\n"; } $client->send($data); } }
|
|---|