Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

In perl socket programming how to send a data from client and receive it from server and how to get number of clients processes and client ID?

by programmingzeal (Beadle)
on May 01, 2019 at 06:17 UTC ( [id://1233218]=perlquestion: print w/replies, xml ) Need Help??

programmingzeal has asked for the wisdom of the Perl Monks concerning the following question:

#!/usr/bin/perl #server use warnings; use strict; use IO::Socket; use threads; use threads::shared; $|++; print "$$ Server started\n";; # do a "top -p -H $$" to monitor server + threads our @clients : shared; @clients = (); my $server = new IO::Socket::INET( Timeout => 7200, Proto => "tcp", LocalPort => 9000, Reuse => 1, Listen => 3 ); my $num_of_client = -1; while (1) { my $client; do { $client = $server->accept; } until ( defined($client) ); my $peerhost = $client->peerhost(); print "accepted a client $client, $peerhost, id = ", ++$num_of_cli +ent, "\n"; my $fileno = fileno $client; push (@clients, $fileno); #spawn a thread here for each client my $thr = threads->new( \&processit, $client, $fileno, $peerhost ) +->detach(); } # end of main thread sub processit { my ($lclient,$lfileno,$lpeer) = @_; #local client if($lclient->connected){ # Here you can do your stuff # I use have the server talk to the client # via print $client and while(<$lclient>) print $lclient "$lpeer->Welcome to server\n"; while(<$lclient>){ # print $lclient "$lpeer->$_\n"; print "clients-> @clients\n"; foreach my $fn (@clients) { open my $fh, ">&=$fn" or warn $! and die; print $fh "$_" } } } #close filehandle before detached thread dies out close( $lclient); #remove multi-echo-clients from echo list @clients = grep {$_ !~ $lfileno} @clients; } __END__
This is server.pl My question is : How can I get the number of clients connected to the server? If say I have opened 4 terminals and ran client.pl 4 times on localhost for testing purposes, how can I get the client ID and client count at server script? I am using Ubuntu on VirtualBox. I am in multi-threaded environment.
  • Comment on In perl socket programming how to send a data from client and receive it from server and how to get number of clients processes and client ID?
  • Download Code

Replies are listed 'Best First'.
Re: In perl socket programming how to send a data from client and receive it from server and how to get number of clients processes and client ID?
by roboticus (Chancellor) on May 01, 2019 at 18:07 UTC

    programmingzeal:

    You can certainly track open connections as hippo mentions. But since as choroba mentions, it's possible for the connection to close at any time without notifying you, so at best it's an optimistic estimate.

    With respect to the code you've shown, I can see the confusion: When I run it, the thread reports "clients-> 5" when I connect to it a single time. I found that changing the statement reporting the number of clients from:

    print "clients-> @clients\n";

    to the following, it reports the correct number of clients:

    print "clients->", scalar(@clients), "\n";

    I've whacked on your code a little to:

    • Add a bit more information about each client to the @clients array. Please note that this requires a little bit of work in the sharing operation!
    • Don't echo a message to the person who sent it.
    • Tell each client who sent the message.

    I thought about doing other stuff, like:

    • Add a SIGALRM handler so the main server can show current count of clients.
    • Announce when new clients connect.
    • Announce when we detect that clients have disconnected.
    • Show the chat dialog on the main server window for logging.

    But I refrained, as I have other stuff to do and mostly to not deprive you of the fun of doing it yourself.

    ...roboticus

    When your only tool is a hammer, all problems look like your thumb.

Re: In perl socket programming how to send a data from client and receive it from server and how to get number of clients processes and client ID?
by hippo (Bishop) on May 01, 2019 at 07:44 UTC
    how can I get the client ID and client count at server script?

    You already have the client count because you are storing a running count of the file descriptors in @clients and therefore the client count is scalar @clients (or $#clients + 1 if you prefer). As for the client ID it depends entirely what you mean by that.

    open my $fh, ">&=$fn" or warn $! and die;

    Can you explain why you warn $! and die here rather than just die $!?

      Maybe the problem is the number of clients doesn't decrease when a client disconnects? It's possible to send a "goodbye" message to the server, but sometimes the connection gets lost without a chance to say anything, in which case a timeout (and maybe a keep-alive message) might help. Also, why is the initial number of clients set to -1?

      map{substr$_->[0],$_->[1]||0,1}[\*||{},3],[[]],[ref qr-1,-,-1],[{}],[sub{}^*ARGV,3]

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1233218]
Approved by haukex
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others chanting in the Monastery: (1)
As of 2024-04-24 14:47 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found