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

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Intermediate:
Sockets / Threads

 



ReapZor
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:

Code
use threads; 
use IO::SOCKET;
#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 {
sleep(5);
while(1) {
print $sock "test\n";
timer();
sleep 1;
}
}

Code
--- 
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.


davorg
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
http://www.dave.org.uk/
Get more help at Perl Monks


thegiova
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?


KevinR
Veteran


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.
-------------------------------------------------


lod
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