M
Monty
Hope this is the right forum for this...
I'm trying to learn sockets, and I'm studying some examples given
vrious places on the web. So far I'm with the explanations, but one
example leaves me perplexed and I'm hoping some one can shed light on
it. I'm hoping to write a chat-like client-server program, and I
found this explanatory code:
.... create socket as before ...
11 use IO::Select;
12 $read_set = new IO::Select(); # create handle set for reading
13 $read_set->add($s); # add the main socket to the set
14
15 while (1) { # forever
16 # get a set of readable handles (blocks until at least one handle
is ready)
17 my ($rh_set) = IO::Select->select($read_set, undef, undef, 0);
18 # take all readable handles in turn
19 foreach $rh (@$rh_set) {
20 # if it is the main socket then we have an incoming connection
and
21 # we should accept() it and then add the new socket to the
$read_set
22 if ($rh == $s) {
23 $ns = $rh->accept();
24 $read_set->add($ns);
25 }
26 # otherwise it is an ordinary socket and we should read and
process the request
27 else {
28 $buf = <$rh>;
29 if($buf) { # we get normal input
30 # ... process $buf ...
31 }
32 else { # the client has closed the socket
33 # remove the socket from the $read_set and close it
34 $read_set->remove($rh);
35 close($rh);
36 }
37 }
38 }
39 }
Some of you probably recognize this.
Regardless of your own opinion of this code, I'm wondering about the
$read_set and $rh_set variables defined here. Are they, in the perl-
ative or figurative sense, lists or arrays containing socket or file
handles? I don't quite follow the perldoc on IO::Select to figure
this out. Also, can sockets generated this way (using IO::Socket) be
bidirectional?
Thanks
I'm trying to learn sockets, and I'm studying some examples given
vrious places on the web. So far I'm with the explanations, but one
example leaves me perplexed and I'm hoping some one can shed light on
it. I'm hoping to write a chat-like client-server program, and I
found this explanatory code:
.... create socket as before ...
11 use IO::Select;
12 $read_set = new IO::Select(); # create handle set for reading
13 $read_set->add($s); # add the main socket to the set
14
15 while (1) { # forever
16 # get a set of readable handles (blocks until at least one handle
is ready)
17 my ($rh_set) = IO::Select->select($read_set, undef, undef, 0);
18 # take all readable handles in turn
19 foreach $rh (@$rh_set) {
20 # if it is the main socket then we have an incoming connection
and
21 # we should accept() it and then add the new socket to the
$read_set
22 if ($rh == $s) {
23 $ns = $rh->accept();
24 $read_set->add($ns);
25 }
26 # otherwise it is an ordinary socket and we should read and
process the request
27 else {
28 $buf = <$rh>;
29 if($buf) { # we get normal input
30 # ... process $buf ...
31 }
32 else { # the client has closed the socket
33 # remove the socket from the $read_set and close it
34 $read_set->remove($rh);
35 close($rh);
36 }
37 }
38 }
39 }
Some of you probably recognize this.
Regardless of your own opinion of this code, I'm wondering about the
$read_set and $rh_set variables defined here. Are they, in the perl-
ative or figurative sense, lists or arrays containing socket or file
handles? I don't quite follow the perldoc on IO::Select to figure
this out. Also, can sockets generated this way (using IO::Socket) be
bidirectional?
Thanks