On Jan 14, 9:31 am, [EMAIL PROTECTED] (Zentara) wrote: > On Sun, 13 Jan 2008 11:34:36 -0800 (PST), [EMAIL PROTECTED] > > > > (Turner) wrote: > >I'm currently in the process of writing a chat server in Perl. > >Everything is all hunky-dory--it parses commands as it should, and is, > >of course, quite satisfying. Except for one thing, and that is that it > >cannot handle multiple clients at once, which, needless to say, is > >kind of useful for a chat program, isn't it? So I've been following > >the discussion online of Threads vs. forking vs. non-blocking IO, and > >I've decided to try threads, which is neat because this is the first > >thing I've ever done with threading. However, my excitement has been > >somewhat dampened by the fact that it does not work. It can still > >happily handle a single client--no complaints there. However, it can > >still ONLY handle a single client. There's probably a hole in my > >understanding of threads (e.g., I don't entirely understand what > >join() and detach() DO...). Below is the relevant server code, and I > >was hoping some kind soul could look at it, suppress his laughter at > >my naive code and point me in the right direction. > > This code is the only threaded chat server which seems to work. > It may show you the way. > > Seehttp://perlmonks.org/?node_id=319472 > > It's tricky to use dynamically spawned threads, because each successive > thread gets a copy of the parent. This can cause confusion in the > IO::Select object ( and that is probably why your code handles only 1 > client), but I havn't tested it. > > Also you will need a bidirectional client to work with the above server. > > #!/usr/bin/perl -w > use strict; > use IO::Socket; > # bi-directional client > > my ( $host, $port, $kidpid, $handle, $line ); > > ( $host, $port ) = ('192.168.0.1',3333); > > #my $name = shift || ''; > #if($name eq ''){print "What's your name?\n"} > #chomp ($name = <>); > > # create a tcp connection to the specified host and port > $handle = IO::Socket::INET->new( > Proto => "tcp", > PeerAddr => $host, > PeerPort => $port > ) > or die "can't connect to port $port on $host: $!"; > $handle->autoflush(1); # so output gets there right away > print STDERR "[Connected to $host:$port]\n"; > > # split the program into two processes, identical twins > die "can't fork: $!" unless defined( $kidpid = fork() ); > > # the if{} block runs only in the parent process > if ($kidpid) { > > # copy the socket to standard output > while ( defined( $line = <$handle> ) ) { > print STDOUT $line; > } > kill( "TERM", $kidpid ); # send SIGTERM to child > > } > > # the else{} block runs only in the child process > else { > > # copy standard input to the socket > while ( defined( $line = <STDIN> ) ) { > #print $handle "$name->$line"; > print $handle "$line"; > }} > > __END__ > > zentara > > -- > I'm not really a human, but I play one on earth.http://zentara.net/japh.html
So what was my problem in the above code? Does thread creation block in some way? Why didn't my loop spawn off a separate thread for each incoming connection? If each gets a copy of the parent, as you said, why is it that it only seems to read one socket (the first client)? Thanks, Turner -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/