jmusbach has asked for the wisdom of the Perl Monks concerning the following question:
(the socket is defined earlier...) my $server_sock=IO::Socket::INET->new(LocalPort=>2012,LocalAddr=>'localhost',Proto=>'udp',Blocking=>0); And when a new client is initiated it does:while (1) { if ($server_sock->recv($rcvd_data,1024)) { print "Got a client, address ".$server_sock->peerhost().", por +t ".$server_sock->peerport().".\n"; my $new_sock=IO::Socket::INET->new(LocalPort=>$new_socknum,Loc +alAddr=>'localhost',Proto=>'udp',Blocking=>0); $server_sock->send("$new_socknum\n"); print "Sent client their new port of $new_socknum!\n"; threads->create('handleAClient',$new_sock->peeraddr(),$new_soc +knum); $new_sock->close(); $new_socknum+=1; } }
Where decodeUDP is:$socket->send("Go!\n"); $socket->recv($data,1024); $port=decodeUDP($data,1024); print "Got port num: $port!\n"; $socket->close(); sleep(3); $socket=IO::Socket::INET->new(PeerHost=>$ip,PeerPort=>$port,Proto=>'ud +p',LocalAddr=>$own_ip); if ($socket) { print "Reconnected to dedicated port $port ok!\n"; } else { die "Error switching to dedicated port $port!\n"; }
Finally, back in the server, the handleAClient thread sub is:sub decodeUDP { my $packet=shift; my $size=shift; return unpack("A$size",$packet); }
The error occurs after $cli_sock is performed in the while loop where I try to read from it to see if the client has sent anything, here perl errors out saying $cli_sock is undefined. I've read that this can happen if the instantiator fails for some reason but I'm unclear as to why it'd fail, can there only be one socket at a time on a host? If so, is there a way around that? I tried the blocking clause to see if that would solve this issue but alas nothing changed. Thanks for any wisdom offered. :)sub handleAClient { my $my_addr=shift; $clients[$#clients+1]=$my_addr; my $my_port=shift; $port_nums[$#port_nums+1]=$my_port; my $udp_data; my $cli_sock=IO::Socket::INET->new(LocalPort=>$my_port,LocalAddr=> +'localhost',Proto=>'udp',Blocking=>0) or die "Could not create socket + with port $my_port!\n"; while (1) { if($cli_sock->recv($udp_data,1024)) { my $counter=0; foreach my $cli (@clients) { #print "cli val: ".$cli."\n"; #print "Cli pn ".$port_nums[$counter]." my pn ".$local +_client->peerport()."\n"; if ($port_nums[$counter]!=$my_port) { my $sock=IO::Socket::INET->new(PeerPort=>$port_num +s[$counter],PeerAddr=>$cli,Proto=>'udp',Blocking=>0); $sock->send("<".$my_addr.":".$my_port."> ".$udp_da +ta."\n"); $sock->close(); } $counter+=1; } } } }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Thread failed to start: cannot do recv
by BrowserUk (Patriarch) on Jan 30, 2012 at 17:20 UTC |