Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:
Specifically I'm wondering if sysread and syswrite are what are should be using. I've seen server code that uses while (<$client>) { instead of sysread and print $client $request instead of syswrite. I think I'm confused as to which one is better.#!/usr/bin/perl -w use IO::Select; use IO::Socket; use strict; $|++; my $port = 9999; my $server = IO::Socket::INET->new ( Proto => 'tcp', LocalPort => $port, Listen => SOMAXCONN, Reuse => 1); (! $server) && die "Could not setup server - $!\n"; $server->autoflush(1); while (my $client = $server->accept()) { my $request; my $selector = IO::Select->new($client); if ($selector->can_read(10)) { my $buffer; while (sysread($client, $buffer, 1024)) { $request .= $buffer; } } else { print "Selector *cannot* read or timed out after 10 seconds.\n"; next; } #print $request, "\n"; syswrite($client, $request); $client->shutdown(1); }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: socket - echo server
by pfaut (Priest) on Jan 17, 2003 at 23:24 UTC | |
by vek (Prior) on Jan 18, 2003 at 16:33 UTC | |
|
Re: socket - echo server
by pg (Canon) on Jan 18, 2003 at 22:09 UTC |