recently i'm doing project on network connections. i'm making server side program on perl handle request client, processing, request data other server. multiuser handling purpose i've use multitasking. , not leaking resource, each thread/connection client have limited time out (5 seconds)
here codes:
while(1) { # waiting new client connection. $client_socket = $socket->accept(); threads->new(\&gotrequest, $client_socket); #gotrequest($client_socket); }
this catching connection client
sub gotrequest { $client_socket=$_[0]; # $peer_address = $client_socket->peeraddr(); $th1=threads->new(\&responce, $client_socket); sleep(5); if (!($th1->is_running())) {print "connection terminated\n";} else { print "operation time out, killing process , terminating connection\n"; print $client_socket "quit\n"; close $client_socket; print "closing...\n"; #$thr->set_thread_exit_only(); $th1->detach(); $th1->exit(); #this thing causing thread's death print "hello i'm still here!"; } }
this thread manage processing thread quit on time otherwise server cant new connection
sub responce { $client_socket=$_[0]; $peer_address = $client_socket->peeraddr(); $peer_port = $client_socket->peerport(); sleep (10); print "i'm still alive"; print "accepted new client connection : $peeraddress, $peerport\n";#dont know why printed null 2 null string :( $client_socket->recv($data,1024000); $data_decode = decode("utf-16", $data); print "received client : $data_decode\n"; #custom code added here $data = encode("utf-16","data server"); print $client_socket "$data\n"; #close($sock); }
i got error:
thread 1 terminated abnormally: usage: threads->exit(status) @ server-cotton.pl line 61 thread 1
when
$th1->exit();
executing.
and 1 more thing, can't not disconnect connection client.
as message says, it's static method call
threads->exit(1); # ok
not instance method call
$th1->exit(1); # not ok unless $th1 contains string "threads"
Comments
Post a Comment