in reply to Re: How to serve multiple socket clients Perl
in thread How to serve multiple socket clients Perl

I cannot figure out how to switch between clients. I mean that I need to accept all possible connections, but I need to communicate only with one client at once, so other can send information, or if it possible to notificate them that now server is not instresting in your information, so you have to wait until it call you. I have only one , but stupid idea, so I am asking here for some better approach. As for my idea, clients are sending data to server for instance each second, server accpets this data and place it in array and when server switches to another client data it will read info from array assigned to this client. But here memory synchronization comes into play, so it is bad idea. It would be great to make it possible for server to switch beetween clients , other should sleep at this time, or something other not interrupting server , or to do this in way that doesn't affect on connection betw een server and focused client. I have no building block to do this taks, so I asked this question to get some advices or directions.
  • Comment on Re^2: How to serve multiple socket clients Perl

Replies are listed 'Best First'.
Re^3: How to serve multiple socket clients Perl
by Anonymous Monk on Mar 23, 2015 at 21:01 UTC

    Some examples of async servers in:

    https://blog.afoolishmanifesto.com/posts/concurrency-and-async-in-perl +/
Re^3: How to serve multiple socket clients Perl
by Anonymous Monk on Mar 25, 2015 at 16:28 UTC

    Here's some building blocks for you. It's just an extension of the example at the end of the IO::Select perldoc page with some features from your stated problem added.

    #!/usr/bin/perl # http://perlmonks.org/?node_id=1120916 use IO::Socket; use IO::Select; use strict; my ($port, $current, $cycle) = (shift // 6667); my $listen = IO::Socket::INET->new(LocalPort => $port, Listen => 9, Re +use => 1) or die "$@ opening socket on port $port"; my $sel = IO::Select->new($listen, 0); while(1) { for my $h ($sel->can_read) { if($h == $listen) { $sel->add(my $new = $h->accept); $current //= $new; print "accepted from: ", $new->peerhost, ":", $new->peerport, "\ +n"; } elsif($h == 0) { sysread STDIN, my $in, 1024; # ignore $current = (grep $_ > 0 && $_ != $listen, $sel->handles) [++$cycle % ($sel->count - 2 || 1)]; print $current ? "switch to " . $current->peerhost . ':' . $current->peerpor +t . "\n" : "no clients\n"; } elsif(sysread $h, my $in, 1024) { $h == $current and print $in; } else { $sel->remove($h); $h == $current and undef $current, print "client exited\n"; } } }

    As others (including me) have said, this kind of problem should be done in one of the async frameworks, but sometimes a plain example can make for a decent tutorial.

    Or not.

Re^3: How to serve multiple socket clients Perl
by Anonymous Monk on Mar 23, 2015 at 22:08 UTC

    You don't "switch between clients". Your callback gets data from all clients, and you simply ignore data from any client except the "current client" (keep a $currentclient :).

    Have a callback from STDIN to switch which client is the "current client".

    I hope this helps.