CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
Search Posts SEARCH
Who's Online WHO'S
Log in LOG

Home: Perl Programming Help: Intermediate:
Sockets / Threads


New User

Nov 26, 2006, 3:25 PM

Post #1 of 5 (600 views)
Sockets / Threads Can't Post

I'm trying to create an irc bot with its own timer system...
for this ive tried using threads, but the timer thread cannot communicate with the socket in any way shape or form.

Here is the jist of the code:

use threads; 
#start timer thread
$thr = threads->new(\&timer_while);
#join the irc server
$sock = IO::Socket::INET->new(PeerAddr => "$config{server}",
PeerPort => "$config{port}",
Proto => "tcp",
die "Could not create socket: $!\n" unless $sock;
#print any message sent to me
while(<$sock>) { print $_; }

#timer sub
sub timer_while {
while(1) {
print $sock "test\n";
sleep 1;

The timer works fine, but it wont print to $sock giving me an error that it is non-existant: thread failed to start: Can't use an undefined value as a symbol reference

i tried using threads::shared, but it wont allow the socket to be shared...

Is the any way to fix this? Thanks for the help.

Thaumaturge / Moderator

Nov 27, 2006, 5:53 AM

Post #2 of 5 (592 views)
Re: [ReapZor] Sockets / Threads [In reply to] Can't Post

I don't have an answer for your question, but I just wanted to point out that you're wasting a lot of time if you're reimplementing an IRC bot from scratch. You should really look at Bot::BasicBot and Bot::BasicBot::Pluggable.

Dave Cross, Perl Hacker, Trainer and Writer
Get more help at Perl Monks

New User

Nov 21, 2008, 7:06 AM

Post #3 of 5 (465 views)
Re: [ReapZor] Sockets / Threads [In reply to] Can't Post

I have the same problem with socket and Thread...

How did you solve this problem?


Nov 21, 2008, 10:21 AM

Post #4 of 5 (460 views)
Re: [thegiova] Sockets / Threads [In reply to] Can't Post

The thread is 2 years old, it is doubtful anyone that posted in it 2 years ago is going to reply. I personally don't have any answer.

New User

Dec 3, 2008, 11:12 PM

Post #5 of 5 (407 views)
Re: [KevinR] Sockets / Threads [In reply to] Can't Post

Variables are thread local, not shared between threads.

You need to use threads::shared to share variables between threads. If that doesn't work on sockets for whatever reason (and it seems like it would be prone to problems) then you need to collect all your socket manipulation in the one thread.


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives