Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

Re^2: Client-Server app

by radu (Initiate)
on Sep 28, 2018 at 06:08 UTC ( #1223199=note: print w/replies, xml ) Need Help??


in reply to Re: Client-Server app
in thread Client-Server app

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?

Replies are listed 'Best First'.
Re^3: Client-Server app
by NetWallah (Canon) on Sep 28, 2018 at 16:40 UTC
    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
Node Status?
node history
Node Type: note [id://1223199]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (3)
As of 2020-10-22 03:22 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    My favourite web site is:












    Results (225 votes). Check out past polls.

    Notices?