Well you have other good answers by my 2 cents is this: You are reinventing he wheel making a socket server, and there is a lot of complication because you have to handle different client instances, and handle the various situations to manage the sockets/sessions/clients. Then you also have the problem of framing/structuring the info over the tcp stream itself. You are much better just jumping straight to something already event driven and using a higher level protocol so sockets, connections, tcp, are all abstracted away.

An example is a simple HTTP server:

use AnyEvent; use AnyEvent::HTTPD; # Create server my $server = AnyEvent::HTTPD->new(host => '127.0.0.1',port => 3333); # Setup response for requests $server->reg_cb ('/test' => sub { my ($obj, $req) = @_; print "Got message '".$req->content."' from client ".$req->client_ +host.":".$req->client_port."\n"; $req->respond([200, 'ok', { 'Content-Type' => 'text/plain' }, 'Hel +lo' ]); }); AnyEvent->condvar->recv; #wait forever

For the client use LWP::UserAgent, or even AnyEvent::HTTP client:

use AnyEvent; use AnyEvent::HTTP; my $cv = AnyEvent->condvar; http_post "http://127.0.0.1:3333/test", "99", persistent=>1, sub { my ($data, $headers) = @_; print "Got back message '$data' from server\n"; $cv->send; # stop waiting }; $cv->recv; # Wait for response

In reply to Re: IO::Socket simple server by Anonymous Monk
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.