Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much

Thread failed to start: cannot do recv

by jmusbach (Initiate)
on Jan 30, 2012 at 17:11 UTC ( #950816=perlquestion: print w/replies, xml ) Need Help??
jmusbach has asked for the wisdom of the Perl Monks concerning the following question:

Hello, I'm working on creating a UDP client/server chat program but have run into some trouble that I'm hoping someone can help me out with. On my server it listens for new connections with:
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; } }
(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:
$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"; }
Where decodeUDP is:
sub decodeUDP { my $packet=shift; my $size=shift; return unpack("A$size",$packet); }
Finally, back in the server, the handleAClient thread sub is:
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; } } } }
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. :)

Replies are listed 'Best First'.
Re: Thread failed to start: cannot do recv
by BrowserUk (Pope) on Jan 30, 2012 at 17:20 UTC

    It sounds like you are trying to create multiple, concurrent local sockets connected to the same port. That doesn't work.

    Of course, if you'd posted complete, runnable code, then we might be able to see what you are actually doing, or even try it out, rather than guessing.

    With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.

    The start of some sanity?

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://950816]
Approved by ww
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (6)
As of 2018-05-23 11:47 GMT
Find Nodes?
    Voting Booth?