in reply to thread:shared

Well I downloaded your code and tried to run it. When the server starts it gives an errror:
Useless use of a variable in void context at ./threaded-chat-server li +ne 120. Useless use of a variable in void context at ./threaded-chat-server li +ne 120. Which is: %E = ( map { $1 => $2; /(.+):(.+)/ } ( split /;/, $Elm ) );

I see there are alot of warnings when trying to print to clients, about unitialized values. I tried connecting 3 clients. The server reported the 3 connections, but when I sent something from the clients, it was not printed unless I hit enter twice, but that's minor. More serious: there was no chat-echo back to any client, but the line feeds were echoed if I hit enter repeatedly. So there is a connection and this is promising. So this thing is far from ready to go. If I manage to get it working over the holidays, I'll post it as a snippet. I did observe the problem you are talking about. I start 3 clients and send some messages, Then I kill all clients and then restart them. If I send from client 2 or 3, all goes well; but as soon as I try to send from client1, the whole thing just crashes taking all clients with it. I tried one of these threaded servers before, and didn't get as far as you did, so it looks hopeful. Thanks for posting your code.

Replies are listed 'Best First'.
Re: Re: thread:shared
by Anonymous Monk on Dec 17, 2003 at 10:05 UTC
    zentara,

    as I want to exchange results of a program running on several computer
    I don't want that the client gets an echo of his own lines. Therefore the ID,
    which is added by the Reader
    push @chat, "$self->{ID}\t$1";
    and removed by the Writer and send if $ID ne $1:
    $chat[$i] =~ /(.+?)\s(.+)[\n\r]*/; print $socket $2,$EOL if ( $1 ne $ID && $2 ne $kill);
    So the Writer skips the lines with its ID, ok? But this is easy to change!!

    The occurence of the warning of the line 120:

    %E = (map { $1 => $2; /(.+):(.+)/; } (split /;/, $Elm));
    is something that I don't understand!!
    This initial warning disappeares if you either remove
    /(.+):(.+)/;
    or
    $1 => $2;
    or if you have a seperate function for those two:
    sub _split { /(.+):(.+)/; return ($1 => $2) if ($1); }
    so that the line 120 looks like
    %E = (map { _split($_) } (split /;/, $Elm));


    Finally I have to tell you that there is a really, really stupid bug, after the loop of the Writer. This code

    $Elm =''; # to rewrite $E foreach ( split /;/, $Elm ) { $Elm .= "$_;" unless ( $_ =~ /^$ID\:/); } cond_broadcast($Elm);
    has to be changed into:
    my $tmp = ''; foreach ( split /;/, $Elm ) { $tmp .= "$_;" if ( $_ !~ /^$ID\:/ && $_ =~/:/); } $Elm = $tmp; cond_broadcast($Elm);
    I think, now everything is ok,
    'it can be braodcasted'
    Carl
      Yeah, I figured out that "no-echo-to-self" part myself, but even with your bug fix, there is no echo, at least on my machine. Linux with Perl5.8.0. Are you using a later Perl version, I know some of Liz's modules require 5.81. I still only get newlines echoed around. As a first guess, I'm thinking it has something to do with your extensive use of $1 and $2, and maybe they are "going out of scope"? I feel confident that I can narrow it down, since I can trace the newlines.

      The bigger problem, which I see, is the way the server crashes when you close all clients, then restart them, and try to print something from client 1. It only affects client1.

      I have found a fix for this, but it was just by my "intuitive guessing", so I can't say what overall effect it will have. But.......if you don't detach the reader and writer threads, the crash problem goes away.(as far as my limited testing has shown). So comment out:

      # $r->detach(); # $w->detach();

      Maybe that will help your problem. Thanks again for the code. This is the first threaded-chat code I could find.