[comp.lang.perl] Multiple simultaneous accepts with an array.

ea08+@andrew.cmu.edu (Eric A. Anderson) (04/25/91)

I am writing a program which I want to have listen on a port, and when
it gets connections, keep adding all the connections to a list.
So I wrote the following line :
        ($addr = accept($DataSock[++$#DataSock],CONNSOCK)) || die $!;
Now what happens when this runs is that the first connection is opened
fine, but when a second connection comes in, the first one is killed.
I have also noticed that this doesn't work :
print fileno($one) . "\n";
$test = $one;
print fileno($test) . "@@\n";
Where one has acceptedon the port.
The fileno is printed for one, but nothing is printed for test.
Is there any easy way to deal with this?
        -Eric
*********************************************************
"My life is full of additional complications spinning around until
 it makes my head snap off."
           -Unc. Known.
"You are very smart, shut up."
           -In "The Princess Bride"
*********************************************************

ea08+@andrew.cmu.edu (Eric A. Anderson) (04/30/91)

I got no responses the last time I think I sent this out, and there
were problems with the netnews software at andrew at about that time,
so on the assumption that this did not succesfully make it out, I will
post it again.
--------------
I am writing a program which I want to have listen on a port, and when
it gets connections, keep adding all the connections to a list.
So I wrote the following line :
        ($addr = accept($DataSock[++$#DataSock],CONNSOCK)) || die $!;
Now what happens when this runs is that the first connection is opened
fine, but when a second connection comes in, the first one is killed.
I have also noticed that this doesn't work :
print fileno($one) . "\n";
$test = $one;
print fileno($test) . "@@\n";
Where one has acceptedon the port.
The fileno is printed for one, but nothing is printed for test.
Is there any easy way to deal with this?
        -Eric
*********************************************************
"My life is full of additional complications spinning around until
 it makes my head snap off."
           -Unc. Known.
"You are very smart, shut up."
           -In "The Princess Bride"
*********************************************************

lwall@jpl-devvax.jpl.nasa.gov (Larry Wall) (05/04/91)

In article <kc75C4S00awWQKxhsR@andrew.cmu.edu> ea08+@andrew.cmu.edu (Eric A. Anderson) writes:
: I got no responses the last time I think I sent this out, and there
: were problems with the netnews software at andrew at about that time,
: so on the assumption that this did not succesfully make it out, I will
: post it again.
: --------------
: I am writing a program which I want to have listen on a port, and when
: it gets connections, keep adding all the connections to a list.
: So I wrote the following line :
:         ($addr = accept($DataSock[++$#DataSock],CONNSOCK)) || die $!;
: Now what happens when this runs is that the first connection is opened
: fine, but when a second connection comes in, the first one is killed.

That's because you accepted them both on the null filehandle.  accept()
and open() do not generate filehandles--you have to do that yourself:

    $genhandle = "FH000";	# We'll increment this magically.
    ...
    $DataSock[++$#DataSock] = $handle = $genhandle++;
    ($addr = accept($handle,CONNSOCK)) || die $!;

I think accept() and open() will take arbitrary expressions returning
a filehandle, but many of the I/O operators require either a filehandle
or a simple scalar variable holding a filehandle:

	$handle = $DataSock[$whatever];
	print $handle $stuff;
	$stuff = <$handle>;

Larry