Skip to content

Commit 5189f2e

Browse files
committed
IO::Socket::INET: echo server + client
1 parent b6e4b3f commit 5189f2e

File tree

1 file changed

+26
-1
lines changed

1 file changed

+26
-1
lines changed

lib/Type/IO/Socket/INET.pod

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,32 @@
88
99
C<IO::Socket::INET> provides TCP sockets, both the server and the client side.
1010
11-
=comment TODO: examples of both client and server
11+
Here is an example of a very simplistic "echo" server that listens on
12+
localhost, port 3333:
13+
14+
=begin code
15+
use v6;
16+
17+
my $listen = IO::Socket::INET.new(:listen, :localport(3333));
18+
loop {
19+
my $conn = $listen.accept;
20+
while my $buf = $conn.recv(:bin) {
21+
$conn.write: $buf;
22+
}
23+
$conn.close;
24+
}
25+
=end code
26+
27+
And a client that connects to it, and prints out what the server answers:
28+
29+
=begin code
30+
use v6;
31+
32+
my $conn = IO::Socket::INET.new(:host<localhost>, :port(3333));
33+
$conn.send: 'Hello, Perl 6';
34+
say $conn.recv;
35+
$conn.close;
36+
=end code
1237
1338
=head1 Methods
1439

0 commit comments

Comments
 (0)