[boulder.pm] Socket troubles
Casey Bristow
Casey.Bristow at xor.com
Tue Sep 19 11:50:29 CDT 2000
the while loop is your friend..
my $sock = 'IO::Socket::INET'->new(
LocalHost => 'localhost',
LocalPort => 8001,
Listen => SOMAXCONN,
Proto => 'tcp',
Reuse => 1 )
or die "DIE: unable to bind to port 8001";
$sock->autoflush(1);
while ( $newsock = $sock->accept() ) {
while ( defined( $buffer = <$newsock> ) ) {
foreach ($buffer) {
print $_;
}
}
}
On Tue, 19 Sep 2000, Justin Crawford wrote:
> I'm trying to understand sockets. I have set up a server & a client and I
> can establish a connection, but the communication stops after the first
> server-->client interaction. So on my terminals it looks like this:
>
> server> socket_test.pl
> Server ready. Waiting for connections...
> I said hello.
>
> client> client_socket_test.pl
> Someone's there! They said:
> "Hello?"
> I asked if I was connected
>
>
> At this point the client should send something back to the server, but it
> doesn't get there. I read that autoflushing is built in when using
> IO::Socket (which I am, in version 5.003) so I'm assuming that there is
> something else I'm forgetting to do. But I don't know enough about IO to
> say for sure that all the flushing that needs doing is being done. Any
> ideas out there?
>
> Thanks a lot.
>
> Justin Crawford
> Oracle Database Administration
> University Management Systems
> 303-492-9083
>
>
> #SERVER-SIDE interaction
> #...
> print $sock "Hello?\n"; #to client
> print "I said hello.\n"; #to STDOUT
>
> my $listening = <$sock>; #from client
> print "He asked me was he connected, just like this:\n"; #to STDOUT
> print "\"$listening\"\n"; #to STDOUT
>
> print $sock "Yes you're connected.\n"; #to client
> print "I told him he was.\n"; #to STDOUT
>
> ----------------------------------------------------
> #CLIENT-SIDE interaction:
> #...
>
> my $listening = <$socket>; #from server
> print "Someone's there! They said:\n"; #to STDOUT
> print "\"$listening\""; #to STDOUT
>
> print $socket "Am I connected?\n"; #to server
> print "I asked if I was connected\n";#to STDOUT
>
> my $waiting = <$socket>; #from server
> print "He told me I was, just like this:\n"; #to STDOUT
> print "\"$waiting\""; #to STDOUT
>
> $socket->close;
>
>
>
>
>
--
-Casey
More information about the Boulder-pm
mailing list