Zip has asked for the wisdom of the Perl Monks concerning the following question:
#!/usr/bin/perl my @sock; my $numsocks = 0; my @message; print "Waiting for client to connect...\n"; use Socket; socket(FH, PF_INET, SOCK_STREAM, getprotobyname('tcp')) || die $!; setsockopt(FH, SOL_SOCKET, SO_REUSEADDR, pack("l", 1)); my $sin = sockaddr_in(20000, INADDR_ANY); bind(FH, $sin) || die $!; listen(FH, SOMAXCONN); my $read = ''; vec($read, fileno(FH), 1) = 1; select(FH); $| = 1; select(STDOUT); while (1) { #this is where it blocks if ($cn = accept($sock[$numsocks], FH)) { my($port,$iaddr) = sockaddr_in($cn); my $name = gethostbyaddr($iaddr,AF_INET); print "Connection from $name (", inet_ntoa($iaddr), ") on +port $port\n"; vec($read, fileno($sock[$numsocks]), 1) = 1; $numsocks++; select($sock[$numsocks-1]); $| = 1; select(STDOUT); $| = 1; } #uh.. i probably messed up somewhere below.. please excuse its slo +ppiness : ) $read = ''; for($i=0; $i<$numsocks; $i++) { vec($read, fileno($sock[$i]), 1) = 1; } select($readx=$read,undef,undef,undef); for($i=0; $i<$numsocks; $i++) { print "Checking socket $i...\n"; if (vec($readx, fileno($sock[$i]), 1)) { sysread $sock[$i], $a, 10; print "$a"; #my @lines = split(/\n/, $a); #if (@lines > 0) { # $lines[0] = $message[$i] . $lines[0]; # $message[$i] = ""; #} #$message[$i] .= pop(@lines); #foreach $ii (@lines) { # print "$lines[$ii]\n"; #} print "Read socket $i..\n"; } } } exit;
Edit 2001-03-16 by tye to change title (was "Zip")
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: accept() on non-blocking socket still hangs
by tye (Sage) on Mar 17, 2001 at 03:15 UTC | |
|
Re: accept() on non-blocking socket still hangs
by Anonymous Monk on Mar 17, 2001 at 03:13 UTC | |
by Zip (Initiate) on Mar 17, 2001 at 07:57 UTC | |
by chromatic (Archbishop) on Mar 18, 2001 at 07:12 UTC | |
by tye (Sage) on Mar 18, 2001 at 13:52 UTC | |
by jyothi vajja (Initiate) on Nov 23, 2004 at 08:48 UTC |