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); } }

In reply to Re: IO::Socket simple server by tybalt89
in thread IO::Socket simple server by packetstormer

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.