j0nny has asked for the wisdom of the Perl Monks concerning the following question:

I wrote an IRC bot and to do various functions it creates threads. One of the functions connects to a web page and parses the content, and before the thread function returns it does this:
print $sock "PRIVMSG $channel :Thread Finished\r\n";
For some reason it won't send it. I have to message something to the bot, then it sends it. SO heres what happens when it doesn't send.
<j0n> !DoFunction <b0t> OK, starting
Here's what happens when I make it send.
<j0n> !DoFunction <b0t> OK, starting <j0n> dssdsdds <b0t> Thread Finished
What?

Replies are listed 'Best First'.
Re: Thread weirdness
by Corion (Patriarch) on Sep 24, 2008 at 13:08 UTC

    Most likely, IRC in itself is a line based protocol and hence you need to append a newline to your string. Possibly you are Suffering From Buffering, and appending the newline would also help this. Also consider using one of the IRC modules for parsing and possibly even wrapping all your communications with the IRC server.

      Whoops, I updated my original post from
      print $sock "PRIVMSG $channel :Thread Finished";
      to
      print $sock "PRIVMSG $channel :Thread Finished\r\n";
      i don't think that's the problem.
Re: Thread weirdness
by zentara (Cardinal) on Sep 24, 2008 at 17:38 UTC
    It almost sounds like your client is waiting for something to be sent from the other end, and the other end dosn't know if it's in send or recv mode, until it receives something. Try experimenting/forcing it with something that forces you into recv mode. One of the problems with using sockets to access a webserver is getting the send/recv right. Maybe the webserver is waiting for you to close your send connection before it returns the page. In typical sockets, you would want to have a forked client that has one branch always in recv mode listening, while the other branch is for sending. Try something like this to tell the web server you are done sending.
    $client->send("\r\n"); while (1) { my $msg; if($client->connected){ $client->recv( $msg, 1024 ); print "$msg\n" } # do closing up here, a webserver won't keep an open socket for you # each connection is new }

    I'm not really a human, but I play one on earth Remember How Lucky You Are
      It's an IRC server not a web server. The thread function is not doing any recving.
        t's an IRC server not a web server.

        In your original node you identified the problem as: One of the functions connects to a web page and parses the content

        So it dosn't matter if you are an IRC bot, it is still trying to access a web server thru a socket, so you need to deal with the http protocols. This is the most basic ....notice you need to send a Get, or at least a "\n\n", before the web server will respond.

        #!/usr/bin/perl -w # Usage: this_script 192.168.0.1 /~zentara/index.html use IO::Socket; unless (@ARGV > 1) { die "usage: $0 host document ..." } $host = shift(@ARGV); foreach $document ( @ARGV ) { $remote = IO::Socket::INET->new( Proto => "tcp", PeerAddr => $host, PeerPort => "http(80)", ); unless ($remote) { die "cannot connect to http daemon on $host" } $remote->autoflush(1); print $remote "GET $document HTTP/1.0\n\n"; while ( <$remote> ) { print } close $remote; }

        I'm not really a human, but I play one on earth Remember How Lucky You Are
Re: Thread weirdness
by BrowserUk (Patriarch) on Sep 24, 2008 at 13:15 UTC
      It worked fine not in a thread.