我试图将哈希值共享给线程函数。哈希值包含soket连接,但我没有将哈希变量共享给线程函数。我收到Invalid value for shared scalar
错误。但是当我将正常文本或整数值分配给散列时,那个散列变量能够共享到线程函数。任何人都可以解决问题吗?我在代码下面尝试了
use strict;
use Data::Dumper;
use IO::Socket;
use IO::Select;
use JSON ;
use threads;
use threads::shared;
my $i=5 ;
my ($Main_Socket_Hash,$Read_Client );
$|=1;
print "Calling" ;
my ($Chat_Details ) ;
($main::Server,$main::Port)=("192.168.12.37","6666");
$main::Socket= IO::Socket::INET->new(
'LocalAddr' => $main::Server,
'LocalPort' => $main::Port,
'Proto' => 'tcp',
'Listen' => '120' ,
'Reuse' => 1
) ;
Handle_Client() ;
sub Handle_Client {
my ( $New_Socket,$New_Client,@Ready_Clints,$Thread_Id ) ;
share($i) ;
shared($Main_Socket_Hash) ;
$Thread_Id=threads->create(\&Read_Outgoing_Message);
$Read_Client = new IO::Select() ;
$Read_Client->add($main::Socket) ;
while ( 1 ) {
print "Main loop \n" ;
my ($Read_Sockets) = IO::Select->select($Read_Client, undef, undef, 0);
foreach $New_Socket (@$Read_Sockets) {
Process_Request($New_Socket) ;
}
sleep(1);
$i=$i+5 ;
}
}
sub Process_Request {
my ( $New_Socket,$New_Client,$Request_Api ) =@_ ;
if ( $New_Socket eq $main::Socket ) {
$New_Client=$New_Socket->accept();
$Read_Client->add($New_Client ) ;
}
else {
$Request_Api=<$New_Socket> ;
unless ( $Request_Api ) {
$Read_Client->remove($New_Socket) ;
close($New_Socket)
}
else {
chomp($Request_Api );
Validation_Request_Api($New_Socket,$Request_Api) ;
}
}
}
sub Validation_Request_Api {
my ( $New_Socket, $Request_Api ,$Request_Data ) = @_ ;
$Request_Data=decode_json($Request_Api) ;
unless ( $Request_Data )
{
return "FAILURE" ;
}
unless ( $Main_Socket_Hash->{$Request_Data->{'chatId'}} )
{
$Main_Socket_Hash->{$Request_Data->{'chatId'}}=$New_Socket;
#$Main_Socket_Hash->{$Request_Data->{'chatId'}}=$Request_Data->{'chatId'};
}
return "SUCCESS" ;
}
sub Read_Outgoing_Message {
my ( $Status,$Outgoing_Message ) ;
while ( 1 ) {
print "%%%%%%%%%%%%%%%%%%%===> $i <=====%%%%%%%%%%%%%\n" ;
print "%%%%%%%%%%%%%%%%%%%===> " . Dumper ( $Main_Socket_Hash) ." <=====%%%%%%%%%%%%%\n" ;
sleep ( 1) ;
}
}
注意:
当我将套接字连接分配给哈希值时出现错误
$Main_Socket_Hash->{$Request_Data->{'chatId'}}=$New_Socket;