Hi dan -

> -----Original Message-----
> From: dan [mailto:[EMAIL PROTECTED]]
> Sent: Saturday, February 01, 2003 3:47 PM
> To: [EMAIL PROTECTED]
> Subject: Re: Threads
>
>
> I tried that, but it's still doing as before. I'll give you a more fuller
> outline of what there is, and what I want it to do, maybe it may become
> clearer...
> I'm using a socket, and it loops in a sub.
> while (<SOCK>) {
>     # etc..
> }
> once the code has finished connecting to the server, it runs another sub,
> &ping,
> sub ping {
>     # blah blah
>     if ($initial) {
>         $thr = threads->new(\&akilltimeout);
>         threads->yield;
>         $thr->detach;
>     }
> }
> as you see, this executes the sub &akilltimeout, which needs to
> run all the
> time, which is,
> sub akilltimeout {
>     while (1) {
>         # checks run here
>         sleep 1;
>     }
> }
> Now, when the code receives data from the server, the akilltimeout sub
> executes, it sleeps for a second, then doesn't do anything else. When it
> receives some more data, it executes again, etc. Basically, that sub needs
> to be running, and the checks need to be made once every second, without
> affecting the listening on the socket, since I know sleep() makes
> the entire
> program wait. So i use threads to run this portion of code. But it isn't
> running every second. Is this more clearer? Is this actually possible? heh
>
> Many thanks once again for your help.
>
> Dan
> -snipped-

I'm not sure if I am on track with the following - but
this works (try it please):

#!/usr/bin/perl
use strict;
use warnings;
use threads;
use Socket qw(:all);

    threads->new (\&tick)->detach;

    socket (SERVER, AF_INET, SOCK_STREAM, getprotobyname ('tcp'));
    setsockopt (SERVER, SOL_SOCKET, SO_REUSEADDR, 1);
    my $my_addr = sockaddr_in (8223, INADDR_ANY);
    bind (SERVER, $my_addr) or die "bind\n";
    listen (SERVER, SOMAXCONN) or die "listen\n";
    while (accept (CLIENT, SERVER)) {
        while (<CLIENT>) {
            print $_;
            s/($CR|$LF)$//g;
            last unless $_;
            }
        my $content = <<"*EOF*";
<html><body>
<h1>Hello World</h1>
</body></html>
*EOF*
        my $clen = length $content;
        $content = <<"*EOF*" . $content;
HTTP/1.1 200 OK
Content-Type: text/html
Content-Length: $clen

*EOF*
        print CLIENT $content;
        close CLIENT;
        }

sub tick
{
    while (1) {
        sleep 1;
        print scalar localtime()."\n";
        }
}

I have tested this on ActivePerl 5.8.0 (804) on Win2K and
Perl 5.8.0 on SuSE Linux 8.1. Now I realize that the
main wait in this simple server is on an 'accept'
(not a 'recv'), but ...

To test, just start this script and point your browser to
'http://127.0.0.1:8223/ . The 'tick' _should_ keep ticking.

Please try this on your system, and let me know if it works.
I have been very pleased with 5.8 threads and really want
to know what the problem is. Also - can you let me know what
your configuration is (OS and Perl).

Aloha => Beau;

PS: If you are using the thread as a timeouter - you may want
to use 'select' instead. But let's get your thread going anyway!



-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to