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

Re: Client-Server app

by NetWallah (Canon)
on Sep 27, 2018 at 17:17 UTC ( [id://1223170]=note: print w/replies, xml ) Need Help??


in reply to Client-Server app

Thanks for posting the code, but there is nothing by way of diagnostic information on the actual problem.

Could you quantify "A lot of data" ?

The only suspicious thing I see in the code is :

$client_socket = $socket->accept(); # Push new client connection to it's own thread push (@clients, threads->create(\&clientHandler, $client_socke +t));
Where you use a global (to the while loop) for $client_socket.

I would rather see that as:

my $client_socket = $socket->accept();
and delete the global declaration.

My thinking (Probably pretty low probability, unless a lot of clients attempt simultaneous connect) is that this may prevent a potential race condition where the content of $client_socket changes before the invoked thread has had a chance to copy it.

                Memory fault   --   brain fried

Replies are listed 'Best First'.
Re^2: Client-Server app
by radu (Initiate) on Sep 28, 2018 at 06:08 UTC

    Hello NetWallah,

    First of all, thanks for your answer.

    And about the "lot of data" I'm doing test with files of more or less 100k lines of log file.

    Usually the server will handle maxium 3-4 lines per connection and then parse it and update a table in mysql and log it to a csv file but I want it to be stable and dont break if any server do a full update of 10k-20k lines of logs

    I tried to delete the global declaration and declare it in the while(1) loop, but it still breaks with only 1 connection. If I try it with

    The problem I think is related to the writing on the log file, because if I comment the line that open the report file in append mode and write the buffer it breaks.

    Now I'm doing tests with 3 simultaneous clients sending 250k lines in loop to the server and it handles it perfectly if I dont write it to the file, only print the ouptut to the console

    while(my $buffer = <$client_socket>){ .... open(my $fh, '>>', $output) or die "Could not open file '$outp +ut' $!"; print $fh $buffer; }
    And the full code with the changes your recommendations and with the log output commented
    #!/usr/bin/perl use strict; use warnings; use threads; use IO::Socket; my $host = '127.0.0.1'; my $port = '1337'; my $proto = 'tcp'; my $debug = 1; my $output = 'report.txt'; my @allowed_ips = ('127.0.0.1'); sub Main { # flush after every write $| = 1; # Bind to listening address and port my $socket = new IO::Socket::INET( LocalHost => $host, LocalPort => $port, Proto => $proto, Listen => 5, Reuse => 1 ) or die "ERROR > Could not open socket: ".$!."\n"; print "INFO > Waiting for client connections on tcp:[$host]:$port +...\n"; my @clients = (); while(1){ # Waiting for new client connection my $client_socket = $socket->accept(); # Push new client connection to it's own thread push (@clients, threads->create(\&clientHandler, $client_s +ocket)); foreach(@clients){ if($_->is_joinable()) { $_->join(); } } } $socket->close(); return 1; } sub clientHandler { # Socket is passed to thread as first (and only) argument. my ($client_socket) = @_; # Create hash for user connection/session information and set init +ial connection information. my %user = (); $user{peer_address} = $client_socket->peerhost(); $user{peer_port} = $client_socket->peerport(); unless (client_allowed($user{peer_address})){ print $client_socket "Server > Connection denied.\n"; print "WARN > Connection from $user{peer_address}:$user{peer_ +port} denied by IP policy.\n"; $client_socket->shutdown(2); $client_socket->close(); threads->exit(); } print "INFO > Client ".$user{peer_address}.":".$user{peer_port}." + has been conected.\n"; # Let client know that server is ready for commands. print $client_socket "Server > Welcome to raClus-Server $user{peer +_address}\n$user{peer_address}> "; # Listen for commands while client socket remains open while(my $buffer = <$client_socket>){ # Accept the command `PING` from client with optional argument +s if($buffer =~ /^PING(\s|$)/i) { print $client_socket "Server > Pong!\n"; } # Accept the command `HELLO` from client with optional argumen +ts if($buffer =~ /^HELLO(\s|$)/i){ print $client_socket "Server > Hello!\n"; print $client_socket "Server > Your IP:\t".$user{peer_addr +ess}."\n"; print $client_socket "Server > Your Port:\t".$user{peer_po +rt}."\n"; } # This will terminate the client connection to the server if($buffer =~ /^QUIT(\s|$)/i){ # Print to client, and print to STDOUT then exit client co +nnection & thread print $client_socket "Server > GOODBYE\n"; print "INFO > Client ".$user{peer_address}.":".$user{peer +_port}." has been disconnected.\n"; $client_socket->shutdown(2); threads->exit(); } print "DEBUG > $buffer" if $debug; # Escribimos en fichero de texto #open(my $fh, '>>', $output) or die "Could not open file '$out +put' $!"; #print $fh $buffer; print $client_socket "$user{peer_address} > "; } print "INFO > Client ".$user{peer_address}.":".$user{peer_port}." + has been disconnected.\n"; # Client has exited so thread should exit too threads->exit(); } sub client_allowed { my $client_ip = shift; return grep { $_ eq $client_ip || $_ eq '0/0' || $_ eq '0.0.0.0/0' + } @allowed_ips; } # Start the Main loop Main();

    Then, the problem is with the output to the log file, can I make it asyncronous or save it in cache and write when it can?

      Just checking if you meant Un-Commenting the open/print to append to $output would cause the program to break.

      I suspect that multiple threads appending to a file may be giving you trouble.

      I suggest starting a separate thread to collect the output, and append it to a file in a single thread.
      You can use Thread::Queue to pass data to the collector thread.

                      Memory fault   --   brain fried

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others having an uproarious good time at the Monastery: (9)
As of 2024-03-28 14:46 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found