Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[PATCH] IO::Socket Documentation #16236

Closed
p5pRT opened this issue Nov 10, 2017 · 11 comments
Closed

[PATCH] IO::Socket Documentation #16236

p5pRT opened this issue Nov 10, 2017 · 11 comments

Comments

@p5pRT
Copy link

p5pRT commented Nov 10, 2017

Migrated from rt.perl.org#132422 (status was 'resolved')

Searchable as RT132422$

@p5pRT
Copy link
Author

p5pRT commented Nov 10, 2017

From capoeirab@cpan.org

Created by capoeirab@cpan.org

The documentation for IO​::Socket hasn't been updated in quite some time.
This patch goes through modernizing the documentation and providing many
more examples. Next up will be IO​::Socket​::INET and IO​::Socket​::UNIX.

Once all of the documentation has been updated, hopefully the updates
will make their way back to the CPAN releases for the IO​:: dist.

Perl Info

Flags:
    category=library
    severity=low
    Type=Patch
    PatchStatus=HasPatch
    module=IO::Socket

Site configuration information for perl 5.27.6:

Configured by cwhitener at Thu Nov  9 22:04:00 EST 2017.

Summary of my perl5 (revision 5 version 27 subversion 6) configuration:
  Derived from: 29d69c3c41c7e93f884256b1087face64d5fdd1e
  Platform:
    osname=linux
    osvers=4.10.0-19-generic
    archname=x86_64-linux
    uname='linux lappy 4.10.0-19-generic #21-ubuntu smp thu apr 6 17:04:57 utc 2017 x86_64 x86_64 x86_64 gnulinux '
    config_args='-des -Dusedevel'
    hint=recommended
    useposix=true
    d_sigaction=define
    useithreads=undef
    usemultiplicity=undef
    use64bitint=define
    use64bitall=define
    uselongdouble=undef
    usemymalloc=n
    default_inc_excludes_dot=define
    bincompat5005=undef
  Compiler:
    cc='cc'
    ccflags ='-fwrapv -fno-strict-aliasing -pipe -fstack-protector-strong -I/usr/local/include -D_LARGEFILE_SOURCE -D_FILE_OFFSET_BITS=64'
    optimize='-O2'
    cppflags='-fwrapv -fno-strict-aliasing -pipe -fstack-protector-strong -I/usr/local/include'
    ccversion=''
    gccversion='7.2.0'
    gccosandvers=''
    intsize=4
    longsize=8
    ptrsize=8
    doublesize=8
    byteorder=12345678
    doublekind=3
    d_longlong=define
    longlongsize=8
    d_longdbl=define
    longdblsize=16
    longdblkind=3
    ivtype='long'
    ivsize=8
    nvtype='double'
    nvsize=8
    Off_t='off_t'
    lseeksize=8
    alignbytes=8
    prototype=define
  Linker and Libraries:
    ld='cc'
    ldflags =' -fstack-protector-strong -L/usr/local/lib'
    libpth=/usr/local/lib /usr/lib/gcc/x86_64-linux-gnu/7/include-fixed /usr/include/x86_64-linux-gnu /usr/lib /lib/x86_64-linux-gnu /lib/../lib /usr/lib/x86_64-linux-gnu /usr/lib/../lib /lib
    libs=-lpthread -lnsl -lgdbm -ldb -ldl -lm -lcrypt -lutil -lc -lgdbm_compat
    perllibs=-lpthread -lnsl -ldl -lm -lcrypt -lutil -lc
    libc=libc-2.26.so
    so=so
    useshrplib=false
    libperl=libperl.a
    gnulibc_version='2.26'
  Dynamic Linking:
    dlsrc=dl_dlopen.xs
    dlext=so
    d_dlsymun=undef
    ccdlflags='-Wl,-E'
    cccdlflags='-fPIC'
    lddlflags='-shared -O2 -L/usr/local/lib -fstack-protector-strong'

Locally applied patches:
    uncommitted-changes


@INC for perl 5.27.6:
    lib
    /home/cwhitener/.perlbrew/libs/perl-5.26.1@normal/lib/perl5/x86_64-linux
    /home/cwhitener/.perlbrew/libs/perl-5.26.1@normal/lib/perl5
    /usr/local/lib/perl5/site_perl/5.27.6/x86_64-linux
    /usr/local/lib/perl5/site_perl/5.27.6
    /usr/local/lib/perl5/5.27.6/x86_64-linux
    /usr/local/lib/perl5/5.27.6


Environment for perl 5.27.6:
    HOME=/home/cwhitener
    LANG=en_US.UTF-8
    LANGUAGE (unset)
    LD_LIBRARY_PATH (unset)
    LOGDIR (unset)
    PATH=/home/cwhitener/.perlbrew/libs/perl-5.26.1@normal/bin:/home/cwhitener/perl5/perlbrew/bin:/home/cwhitener/perl5/perlbrew/perls/perl-5.26.1/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin
    PERL5LIB=/home/cwhitener/.perlbrew/libs/perl-5.26.1@normal/lib/perl5
    PERLBREW_BASHRC_VERSION=0.78
    PERLBREW_HOME=/home/cwhitener/.perlbrew
    PERLBREW_LIB=normal
    PERLBREW_MANPATH=/home/cwhitener/.perlbrew/libs/perl-5.26.1@normal/man:/home/cwhitener/perl5/perlbrew/perls/perl-5.26.1/man
    PERLBREW_PATH=/home/cwhitener/.perlbrew/libs/perl-5.26.1@normal/bin:/home/cwhitener/perl5/perlbrew/bin:/home/cwhitener/perl5/perlbrew/perls/perl-5.26.1/bin
    PERLBREW_PERL=perl-5.26.1
    PERLBREW_ROOT=/home/cwhitener/perl5/perlbrew
    PERLBREW_VERSION=0.78
    PERL_BADLANG (unset)
    PERL_LOCAL_LIB_ROOT=/home/cwhitener/.perlbrew/libs/perl-5.26.1@normal
    PERL_MB_OPT=--install_base /home/cwhitener/.perlbrew/libs/perl-5.26.1@normal
    PERL_MM_OPT=INSTALL_BASE=/home/cwhitener/.perlbrew/libs/perl-5.26.1@normal
    SHELL=/bin/bash

@p5pRT
Copy link
Author

p5pRT commented Nov 10, 2017

From capoeirab@cpan.org

0001-Update-the-documentation-for-IO-Socket-to-be-a-bit-m.patch
From 6f5acfdaa93ba4527f8782561408fd040518d948 Mon Sep 17 00:00:00 2001
From: Chase Whitener <cwhitener@gmail.com>
Date: Thu, 9 Nov 2017 23:25:29 -0500
Subject: [PATCH] Update the documentation for IO::Socket to be a bit more
 complete and have more examples.

---
 dist/IO/lib/IO/Socket.pm | 488 ++++++++++++++++++++++++++++++++++++-----------
 1 file changed, 381 insertions(+), 107 deletions(-)

diff --git a/dist/IO/lib/IO/Socket.pm b/dist/IO/lib/IO/Socket.pm
index c78aeecc1a..b5cdea1561 100644
--- a/dist/IO/lib/IO/Socket.pm
+++ b/dist/IO/lib/IO/Socket.pm
@@ -24,7 +24,7 @@ require IO::Socket::UNIX if ($^O ne 'epoc' && $^O ne 'symbian');
 
 @ISA = qw(IO::Handle);
 
-$VERSION = "1.38";
+$VERSION = "1.39";
 
 @EXPORT_OK = qw(sockatmark);
 
@@ -387,181 +387,454 @@ IO::Socket - Object interface to socket communications
 
 =head1 SYNOPSIS
 
-    use IO::Socket;
+    #!/usr/bin/env perl
+    use strict;
+    use warnings;
+    use feature 'say';
+
+    use IO::Socket qw(AF_INET AF_UNIX SOCK_STREAM SHUT_WR);
+
+    # create a new AF_INET socket
+    my $sock = IO::Socket->new(Domain => AF_INET);
+    # which is the same as
+    $sock = IO::Socket::INET->new();
+
+    # create a new AF_UNIX socket
+    $sock = IO::Socket->new(Domain => AF_UNIX);
+    # which is the same as
+    $sock = IO::Socket::UNIX->new();
+
+    # Let's create a TCP server on localhost:3333
+    my $server = IO::Socket->new(
+        Domain => AF_INET,
+        Type => SOCK_STREAM,
+        Proto => 'tcp',
+        LocalHost => '0.0.0.0',
+        LocalPort => 3333,
+        ReusePort => 1,
+        Listen => 5,
+    ) || die "Can't open socket: $@";
+    say "Waiting on 3333";
+
+    while (1) {
+        # waiting for a new client connection
+        my $client = $server->accept();
+
+        # get information about a newly connected client
+        my $client_address = $client->peerhost();
+        my $client_port = $client->peerport();
+        say "Connection from $client_address:$client_port";
+
+        # read up to 1024 characters from the connected client
+        my $data = "";
+        $client->recv($data, 1024);
+        say "received data: $data";
+
+        # write response data to the connected client
+        $data = "ok";
+        $client->send($data);
+
+        # notify client that response has been sent
+        $client->shutdown(SHUT_WR);
+    }
+
+    $server->close();
+
+
+    # A client for such a server could be
+    my $client = IO::Socket->new(
+        Domain => AF_INET,
+        Type => SOCK_STREAM,
+        proto => 'tcp',
+        PeerPort => 3333,
+        PeerHost => '0.0.0.0',
+    ) || die "Can't open socket: $@";
+
+    say "Sending Hello World!";
+    my $size = $client->send("Hello World!");
+    say "Sent data of length: $size";
+
+    $client->shutdown(SHUT_WR);
+
+    my $buffer;
+    $client->recv($buffer, 1024);
+    say "Got back $buffer";
+
+    $client->close();
 
 =head1 DESCRIPTION
 
-C<IO::Socket> provides an object interface to creating and using sockets. It
-is built upon the L<IO::Handle> interface and inherits all the methods defined
-by L<IO::Handle>.
+L<IO::Socket> provides an object-oriented, L<IO::Handle>-based interface to
+creating and using sockets via L<Socket>, which provides a near one-to-one
+interface to the C socket library.
 
-C<IO::Socket> only defines methods for those operations which are common to all
-types of socket. Operations which are specified to a socket in a particular 
-domain have methods defined in sub classes of C<IO::Socket>
+L<IO::Socket> is a base class that really only defines methods for those
+operations which are common to all types of sockets. Operations which are
+specific to a particular socket domain have methods defined in subclasses of
+L<IO::Socket>. See L<IO::Socket::INET> and L<IO::Socket::UNIX> for examples of
+such a subclass.
 
-C<IO::Socket> will export all functions (and constants) defined by L<Socket>.
+L<IO::Socket> will export all functions (and constants) defined by L<Socket>.
 
-=head1 CONSTRUCTOR
+=head1 CONSTRUCTOR ARGUMENTS
 
-=over 4
+Given that L<IO::Socket> doesn't have attributes in the traditional sense, the
+following arguments, rather than attributes, can be passed into the
+constructor.
 
-=item new ( [ARGS] )
+Constructor arguments should be passed in C<< key => 'value' >> pairs.
 
-Creates an C<IO::Socket>, which is a reference to a
-newly created symbol (see the C<Symbol> package). C<new>
-optionally takes arguments, these arguments are in key-value pairs.
-C<new> only looks for one key C<Domain> which tells new which domain
-the socket will be in. All other arguments will be passed to the
-configuration method of the package for that domain, See below.
+The only required argument is L<IO::Socket/"Domain">.
 
- NOTE NOTE NOTE NOTE NOTE NOTE NOTE NOTE NOTE NOTE NOTE NOTE
+=head2 Blocking
 
-As of VERSION 1.18 all IO::Socket objects have autoflush turned on
-by default. This was not the case with earlier releases.
+    my $sock = IO::Socket->new(..., Blocking => 1);
+    $sock = IO::Socket->new(..., Blocking => 0);
 
- NOTE NOTE NOTE NOTE NOTE NOTE NOTE NOTE NOTE NOTE NOTE NOTE
+If defined but false, the socket will be set to non-blocking mode. If not
+specified it defaults to C<1> (blocking mode).
 
-=back
+=head2 Domain
 
-=head1 METHODS
+    my $sock = IO::Socket->new(Domain => IO::Socket::AF_INET);
+    $sock = IO::Socket->new(Domain => IO::Socket::AF_UNIX);
 
-See L<perlfunc> for complete descriptions of each of the following
-supported C<IO::Socket> methods, which are just front ends for the
-corresponding built-in functions:
+The socket domain will define which subclass of L<IO::Socket> to use. The two
+options available along with this distribution are C<AF_INET> and C<AF_UNIX>.
 
-    socket
-    socketpair
-    bind
-    listen
-    accept
-    send
-    recv
-    peername (getpeername)
-    sockname (getsockname)
-    shutdown
+C<AF_INET> is for the internet address family of sockets and is handled via
+L<IO::Socket::INET>. C<AF_INET> sockets are bound to an internet address and
+port.
 
-Some methods take slightly different arguments to those defined in L<perlfunc>
-in attempt to make the interface more flexible. These are
+C<AF_UNIX> is for the unix domain socket and is handled via
+L<IO::Socket::UNIX>. C<AF_UNIX> sockets are bound to the file system as their
+address name space.
 
-=over 4
+This argument is B<required>. All other arguments are optional.
 
-=item accept([PKG])
+=head2 Listen
 
-perform the system call C<accept> on the socket and return a new
-object. The new object will be created in the same class as the listen
-socket, unless C<PKG> is specified. This object can be used to
-communicate with the client that was trying to connect.
+    my $sock = IO::Socket->new(..., Listen => 5);
 
-In a scalar context the new socket is returned, or undef upon
-failure. In a list context a two-element array is returned containing
-the new socket and the peer address; the list will be empty upon
-failure.
+Listen should be an integer value or left unset.
 
-The timeout in the [PKG] can be specified as zero to effect a "poll",
-but you shouldn't do that because a new IO::Select object will be
-created behind the scenes just to do the single poll.  This is
-horrendously inefficient.  Use rather true select() with a zero
-timeout on the handle, or non-blocking IO.
+If provided, this argument will place the socket into listening mode. New
+connections can then be accepted using the L<IO::Socket/"accept"> method. The
+value given is used as the C<listen(2)> queue size.
 
-=item socketpair(DOMAIN, TYPE, PROTOCOL)
+If the C<Listen> argument is given, but false, the queue size will be set to
+5.
 
-Call C<socketpair> and return a list of two sockets created, or an
-empty list on failure.
+=head2 Timeout
 
-=back
+    my $sock = IO::Socket->new(..., Timeout => 5);
 
-Additional methods that are provided are:
+The timeout value, in seconds, for this socket connection. How exactly this
+value is utilized is defined in the socket domain subclasses that make use of
+the value.
 
-=over 4
+=head2 Type
 
-=item atmark
+    my $sock = IO::Socket->new(..., Type => IO::Socket::SOCK_STREAM);
 
-True if the socket is currently positioned at the urgent data mark,
-false otherwise.
+The socket type that will be used. These are usually C<SOCK_STREAM>,
+C<SOCK_DGRAM>, or C<SOCK_RAW>. If this argument is left undefined an attempt
+will be made to infer the type from the service name.
 
-    use IO::Socket;
+For example, you'll usually use C<SOCK_STREAM> with a C<tcp> connection and
+C<SOCK_DGRAM> with a C<udp> connection.
 
-    my $sock = IO::Socket::INET->new('some_server');
+=head1 CONSTRUCTORS
+
+L<IO::Socket> extends the L<IO::Handle> constructor.
+
+=head2 new
+
+    my $sock = IO::Socket->new();
+
+    # get a new IO::Socket::INET instance
+    $sock = IO::Socket->new(Domain => IO::Socket::AF_INET);
+    # get a new IO::Socket::UNIX instance
+    $sock = IO::Socket->new(Domain => IO::Socket::AF_UNIX);
+
+    # Domain is the only required argument
+    $sock = IO::Socket->new(
+        Domain => IO::Socket::AF_INET, # AF_INET, AF_UNIX
+        Type => IO::Socket::SOCK_STREAM, # SOCK_STREAM, SOCK_DGRAM, ...
+        Proto => 'tcp', # 'tcp', 'udp', IPPROTO_TCP, IPPROTO_UDP
+        # and so on...
+    );
+
+Creates an C<IO::Socket>, which is a reference to a newly created symbol (see
+the L<Symbol> package). C<new> optionally takes arguments, these arguments
+are defined in L<IO::Socket/"CONSTRUCTOR ARGUMENTS">.
+
+Any of the L<IO::Socket/"CONSTRUCTOR ARGUMENTS"> may be passed to the
+constructor, but if any arguments are provided, then one of them must be
+the L<IO::Socket/"Domain"> argument. The L<IO::Socket/"Domain"> argument can,
+by default, be either C<AF_INET> or C<AF_UNIX>. Other domains can be used if a
+proper subclass for the domain family is registered. All other arguments will
+be passed to the C<configuration> method of the package for that domain.
+
+=head1 METHODS
+
+L<IO::Socket> inherits all methods from L<IO::Handle> and implements the
+following new ones.
+
+=head2 accept
+
+    my $client_sock = $sock->accept();
+    my $inet_sock = $sock->accept('IO::Socket::INET');
+
+The accept method will perform the system call C<accept> on the socket and
+return a new object. The new object will be created in the same class as the
+listen socket, unless a specific package name is specified. This object can be
+used to communicate with the client that was trying to connect.
+
+This differs slightly from the C<accept> function in L<perlfunc>.
+
+In a scalar context the new socket is returned, or C<undef> upon
+failure. In a list context a two-element array is returned containing
+the new socket and the peer address; the list will be empty upon failure.
+
+=head2 atmark
+
+    my $integer = $sock->atmark();
+    # read in some data on a given socket
+    my $data;
     $sock->read($data, 1024) until $sock->atmark;
 
-Note: this is a reasonably new addition to the family of socket
-functions, so all systems may not support this yet.  If it is
-unsupported by the system, an attempt to use this method will
-abort the program.
+    # or, export the function to use:
+    use IO::Socket 'sockatmark';
+    $sock->read($data, 1024) until sockatmark($sock);
 
-The atmark() functionality is also exportable as sockatmark() function:
+True if the socket is currently positioned at the urgent data mark, false
+otherwise. If your system doesn't yet implement C<sockatmark> this will throw
+an exception.
 
-	use IO::Socket 'sockatmark';
+If your system does not support C<sockatmark>, the C<use> declaration will
+fail at compile time.
 
-This allows for a more traditional use of sockatmark() as a procedural
-socket function.  If your system does not support sockatmark(), the
-C<use> declaration will fail at compile time.
+=head2 autoflush
 
-=item connected
+    # by default, autoflush will be turned on when referenced
+    $sock->autoflush(); # turns on autoflush
+    # turn off autoflush
+    $sock->autoflush(0);
+    # turn on autoflush
+    $sock->autoflush(1);
+
+This attribute isn't overridden from L<IO::Handle>'s implementation. However,
+since we turn it on by default, it's worth mentioning here.
+
+=head2 bind
+
+    use Socket qw(pack_sockaddr_in);
+    my $port = 3000;
+    my $ip_address = '0.0.0.0';
+    my $packed_addr = pack_sockaddr_in($port, $ip_address);
+    $sock->bind($packed_addr);
+
+Binds a network address to a socket, just as C<bind(2)> does. Returns true if
+it succeeded, false otherwise. You should provide a packed address of the
+appropriate type for the socket.
+
+=head2 connected
+
+    my $peer_addr = $sock->connected();
+    if ($peer_addr) {
+        say "We're connected to $peer_addr";
+    }
 
 If the socket is in a connected state, the peer address is returned. If the
-socket is not in a connected state, undef is returned.
+socket is not in a connected state, C<undef> is returned.
 
-Note that connected() considers a half-open TCP socket to be "in a connected
-state".  Specifically, connected() does not distinguish between the
+Note that this method considers a half-open TCP socket to be "in a connected
+state".  Specifically, it does not distinguish between the
 B<ESTABLISHED> and B<CLOSE-WAIT> TCP states; it returns the peer address,
-rather than undef, in either case.  Thus, in general, connected() cannot
+rather than C<undef>, in either case.  Thus, in general, it cannot
 be used to reliably learn whether the peer has initiated a graceful shutdown
 because in most cases (see below) the local TCP state machine remains in
-B<CLOSE-WAIT> until the local application calls shutdown() or close();
-only at that point does connected() return undef.
+B<CLOSE-WAIT> until the local application calls L<IO::Socket/"shutdown"> or
+C<close>. Only at that point does this function return C<undef>.
 
 The "in most cases" hedge is because local TCP state machine behavior may
 depend on the peer's socket options. In particular, if the peer socket has
-SO_LINGER enabled with a zero timeout, then the peer's close() will generate
-a RST segment, upon receipt of which the local TCP transitions immediately to
-B<CLOSED>, and in that state, connected() I<will> return undef.
+C<SO_LINGER> enabled with a zero timeout, then the peer's C<close> will
+generate a C<RST> segment. Upon receipt of that segment, the local TCP
+transitions immediately to B<CLOSED>, and in that state, this method I<will>
+return C<undef>.
+
+=head2 getsockopt
 
-=item protocol
+    my $value = $sock->getsockopt(SOL_SOCKET, SO_REUSEADDR);
+    my $buf = $socket->getsockopt(SOL_SOCKET, SO_RCVBUF);
+    say "Receive buffer is $buf bytes";
 
-Returns the numerical number for the protocol being used on the socket, if
-known. If the protocol is unknown, as with an AF_UNIX socket, zero
+Get an option associated with the socket. Levels other than C<SOL_SOCKET>
+may be specified here. As a convenience, this method will unpack a byte buffer
+of the correct size back into a number.
+
+=head2 listen
+
+    $sock->listen(5);
+
+Does the same thing that the C<listen(2)> system call does. Returns true if it
+succeeded, false otherwise. Listens to a socket with a given queue size.
+
+=head2 peername
+
+    my $sockaddr_in = $sock->peername();
+
+Returns the packed C<sockaddr> address of the other end of the socket
+connection. It calls C<getpeername>.
+
+
+=head2 protocol
+
+    my $proto = $sock->protocol();
+
+Returns the number for the protocol being used on the socket, if
+known. If the protocol is unknown, as with an C<AF_UNIX> socket, zero
 is returned.
 
-=item sockdomain
+=head2 recv
+
+    my $buffer = "";
+    my $length = 1024;
+    my $flags;
+    $sock->recv($buffer, $length, $flags);
+
+Receives a message on a socket. Attempts to receive C<$length> characters of
+data into C<$buffer> from the specified socket. C<$buffer> will be grown or
+shrunk to the length actually read. Takes the same flags as the system call of
+the same name. Returns the address of the sender if socket's protocol supports
+this; returns an empty string otherwise. If there's an error, returns
+C<undef>. This call is actually implemented in terms of the C<recvfrom(2)>
+system call.
+
+Flags are ORed together values, such as C<MSG_BCAST>, C<MSG_OOB>,
+C<MSG_TRUNC>.
+
+Note the characters: depending on the status of the socket, either (8-bit)
+bytes or characters are received. By default all sockets operate on bytes, but
+for example if the socket has been changed using C<binmode> to operate with
+the C<:encoding(UTF-8)> I/O layer (see the C<open> pragma), the I/O will
+operate on UTF8-encoded Unicode characters, not bytes. Similarly for the
+C<:encoding> layer: in that case pretty much any characters can be read.
+
+=head2 send
+
+    my $message = "Hello, world!";
+    my $flags;
+    my $to = '0.0.0.0'; # optional destination
+    my $sent = $sock->send($message);
+    $sent = $sock->send($message, $flags);
+    $sent = $sock->send($message, $flags, $to);
+
+Sends a message on a socket. Attempts to send the scalar message to the
+socket. Takes the same flags as the system call of the same name. On
+unconnected sockets, you must specify a destination to send to, in which case
+it does a C<sendto(2)> syscall. Returns the number of characters sent, or
+C<undef> on error. The C<sendmsg(2)> syscall is currently unimplemented.
+
+=head2 setsockopt
+
+    $sock->setsockopt(SOL_SOCKET, SO_REUSEADDR, 1);
+    $sock->setsockopt(SOL_SOCKET, SO_RCVBUF, 64*1024);
 
-Returns the numerical number for the socket domain type. For example, for
-an AF_INET socket the value of &AF_INET will be returned.
+Set option associated with the socket. Levels other than C<SOL_SOCKET>
+may be specified here. As a convenience, this method will convert a number
+into a packed byte buffer.
 
-=item sockopt(OPT [, VAL])
+=head2 shutdown
 
-Unified method to both set and get options in the SOL_SOCKET level. If called
-with one argument then getsockopt is called, otherwise setsockopt is called.
+    $sock->shutdown(SHUT_RD); # we stopped reading data
+    $sock->shutdown(SHUT_WR); # we stopped writing data
+    $sock->shutdown(SHUT_RDWR); # we stopped using this socket
 
-=item getsockopt(LEVEL, OPT)
+Shuts down a socket connection in the manner indicated by the value passed in,
+which has the same interpretation as in the syscall of the same name.
 
-Get option associated with the socket. Other levels than SOL_SOCKET
-may be specified here.
+This is useful with sockets when you want to tell the other side you're done
+writing but not done reading, or vice versa. It's also a more insistent form
+of C<close> because it also disables the file descriptor in any
+forked copies in other processes.
 
-=item setsockopt(LEVEL, OPT, VAL)
+Returns C<1> for success; on error, returns C<undef> if the socket is
+not a valid filehandle, or returns C<0> and sets C<$!> for any other failure.
 
-Set option associated with the socket. Other levels than SOL_SOCKET
-may be specified here.
+=head2 sockdomain
 
-=item socktype
+    my $domain = $sock->sockdomain();
 
-Returns the numerical number for the socket type. For example, for
-a SOCK_STREAM socket the value of &SOCK_STREAM will be returned.
+Returns the number for the socket domain type. For example, for
+an C<AF_INET> socket the value of C<&AF_INET> will be returned.
 
-=item timeout([VAL])
+=head2 socket
+
+    my $sock = IO::Socket->new(); # no values given
+    # now let's actually get a socket with the socket method
+    # domain, type, and protocol are required
+    $sock = $sock->socket(AF_INET, SOCK_STREAM, 'tcp');
+
+Opens a socket of the specified kind and returns it. Domain, type, and
+protocol are specified the same as for the syscall of the same name.
+
+=head2 socketpair
+
+    my ($r, $w) = $sock->socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC);
+    ($r, $w) = IO::Socket::UNIX
+        ->socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC);
+
+Will return a list of two sockets created (read and write), or an empty list
+on failure.
+
+Differs slightly from C<socketpair> in L<perlfunc> in that the argument list
+is a bit simpler.
+
+=head2 sockname
+
+    my $packed_addr = $sock->sockname();
+
+Returns the packed C<sockaddr> address of this end of the connection. It's the
+same as C<getsockname(2)>.
+
+=head2 sockopt
+
+    my $value = $sock->sockopt(SO_REUSEADDR);
+    $sock->sockopt(SO_REUSEADDR, 1);
+
+Unified method to both set and get options in the C<SOL_SOCKET> level. If
+called with one argument then L<IO::Socket/"getsockopt"> is called, otherwise
+L<IO::Socket/"setsockopt"> is called.
+
+=head2 socktype
+
+    my $type = $sock->socktype();
+
+Returns the number for the socket type. For example, for
+a C<SOCK_STREAM> socket the value of C<&SOCK_STREAM> will be returned.
+
+=head2 timeout
+
+    my $seconds = $sock->timeout();
+    my $old_val = $sock->timeout(5); # set new and return old value
 
 Set or get the timeout value (in seconds) associated with this socket.
 If called without any arguments then the current setting is returned. If
 called with an argument the current setting is changed and the previous
 value returned.
 
-=back
+This method is available to all L<IO::Socket> implementations but may or may
+not be used by the individual domain subclasses.
 
 =head1 LIMITATIONS
 
-On some systems, for an IO::Socket object created with new_from_fd(),
-or created with accept() from such an object, the protocol(),
-sockdomain() and socktype() methods may return undef.
+On some systems, for an IO::Socket object created with C<new_from_fd>,
+or created with L<IO::Socket/"accept"> from such an object, the
+L<IO::Socket/"protocol">, L<IO::Socket/"sockdomain"> and
+L<IO::Socket/"socktype"> methods may return C<undef>.
 
 =head1 SEE ALSO
 
@@ -584,3 +857,4 @@ Feel free to use, modify and redistribute it as long as you retain
 the correct attribution.
 
 =cut
+
-- 
2.14.1

@p5pRT
Copy link
Author

p5pRT commented Nov 20, 2017

From @jkeenan

On Fri, 10 Nov 2017 04​:42​:30 GMT, capoeirab@​cpan.org wrote​:

This is a bug report for perl from capoeirab@​cpan.org,
generated with the help of perlbug 1.40 running under perl 5.27.6.

-----------------------------------------------------------------
[Please describe your issue here]

The documentation for IO​::Socket hasn't been updated in quite some
time.
This patch goes through modernizing the documentation and providing
many
more examples. Next up will be IO​::Socket​::INET and IO​::Socket​::UNIX.

Once all of the documentation has been updated, hopefully the updates
will make their way back to the CPAN releases for the IO​:: dist.

Thank you for taking the time to review the IO​::Socket documentation and submit a patch.

Because there are quite a lot of revisions in your patch, you should expect a certain amount of back-and-forth with respect to further revisions before this is applied to blead.

I am not an expert on sockets, so I'm not going to attempt to review the entire patch here. I hope that other readers will contribute constructive comments.

I will, however, comment on your revisions to the SYNOPSIS. Granted, the current synopsis is insufficient. However, we generally follow a style in which the synopsis focuses on terse descriptions of the most important methods or functions which a module provides. More detailed information on particular methods, including more fully fleshed-out examples should be deferred to the sections on those methods or, perhaps, to an EXAMPLES section later in the documentation. More specifically​:

=head1 SYNOPSIS

- use IO​::Socket;

We don't need any of the next four lines in SYNOPSIS.

+ #!/usr/bin/env perl
+ use strict;
+ use warnings;
+ use feature 'say';
+
+ use IO​::Socket qw(AF_INET AF_UNIX SOCK_STREAM SHUT_WR);
+
+ # create a new AF_INET socket

We don't need the 'my' in SYNOPSIS.

+ my $sock = IO​::Socket->new(Domain => AF_INET);

The next two lines should go in discussion of new().

+ # which is the same as
+ $sock = IO​::Socket​::INET->new();
+
+ # create a new AF_UNIX socket
+ $sock = IO​::Socket->new(Domain => AF_UNIX);

Same as above.

+ # which is the same as
+ $sock = IO​::Socket​::UNIX->new();
+

The balance of the revisions should be moved to an EXAMPLES section (once the code is verified).

+ # Let's create a TCP server on localhost​:3333
+ my $server = IO​::Socket->new(
+ Domain => AF_INET,
+ Type => SOCK_STREAM,
+ Proto => 'tcp',
+ LocalHost => '0.0.0.0',
+ LocalPort => 3333,
+ ReusePort => 1,
+ Listen => 5,
+ ) || die "Can't open socket​: $@​";
+ say "Waiting on 3333";
+
+ while (1) {
+ # waiting for a new client connection
+ my $client = $server->accept();
+
+ # get information about a newly connected client
+ my $client_address = $client->peerhost();
+ my $client_port = $client->peerport();
+ say "Connection from $client_address​:$client_port";
+
+ # read up to 1024 characters from the connected client
+ my $data = "";
+ $client->recv($data, 1024);
+ say "received data​: $data";
+
+ # write response data to the connected client
+ $data = "ok";
+ $client->send($data);
+
+ # notify client that response has been sent
+ $client->shutdown(SHUT_WR);
+ }
+
+ $server->close();
+
+
+ # A client for such a server could be
+ my $client = IO​::Socket->new(
+ Domain => AF_INET,
+ Type => SOCK_STREAM,
+ proto => 'tcp',
+ PeerPort => 3333,
+ PeerHost => '0.0.0.0',
+ ) || die "Can't open socket​: $@​";
+
+ say "Sending Hello World!";
+ my $size = $client->send("Hello World!");
+ say "Sent data of length​: $size";
+
+ $client->shutdown(SHUT_WR);
+
+ my $buffer;
+ $client->recv($buffer, 1024);
+ say "Got back $buffer";
+
+ $client->close();

The only IO​::Socket methods you used in these examples apart from new() were accept() and close(). Are they used often enough -- not just in creation of servers -- to warrant their being placed in the SYNOPSIS? Are there any other IO​::Socket methods whose importance warrants their being mentioned in the SYNOPSIS?

[Please do not change anything below this line]
-----------------------------------------------------------------
---
Flags​:
category=library
severity=low
Type=Patch
PatchStatus=HasPatch
module=IO​::Socket
---
Site configuration information for perl 5.27.6​:

Configured by cwhitener at Thu Nov 9 22​:04​:00 EST 2017.

Summary of my perl5 (revision 5 version 27 subversion 6)
configuration​:
Derived from​: 29d69c3
Platform​:
osname=linux
osvers=4.10.0-19-generic
archname=x86_64-linux
uname='linux lappy 4.10.0-19-generic #21-ubuntu smp thu apr 6
17​:04​:57 utc 2017 x86_64 x86_64 x86_64 gnulinux '
config_args='-des -Dusedevel'
hint=recommended
useposix=true
d_sigaction=define
useithreads=undef
usemultiplicity=undef
use64bitint=define
use64bitall=define
uselongdouble=undef
usemymalloc=n
default_inc_excludes_dot=define
bincompat5005=undef
Compiler​:
cc='cc'
ccflags ='-fwrapv -fno-strict-aliasing -pipe -fstack-protector-
strong -I/usr/local/include -D_LARGEFILE_SOURCE
-D_FILE_OFFSET_BITS=64'
optimize='-O2'
cppflags='-fwrapv -fno-strict-aliasing -pipe -fstack-protector-
strong -I/usr/local/include'
ccversion=''
gccversion='7.2.0'
gccosandvers=''
intsize=4
longsize=8
ptrsize=8
doublesize=8
byteorder=12345678
doublekind=3
d_longlong=define
longlongsize=8
d_longdbl=define
longdblsize=16
longdblkind=3
ivtype='long'
ivsize=8
nvtype='double'
nvsize=8
Off_t='off_t'
lseeksize=8
alignbytes=8
prototype=define
Linker and Libraries​:
ld='cc'
ldflags =' -fstack-protector-strong -L/usr/local/lib'
libpth=/usr/local/lib /usr/lib/gcc/x86_64-linux-gnu/7/include-
fixed /usr/include/x86_64-linux-gnu /usr/lib /lib/x86_64-linux-gnu
/lib/../lib /usr/lib/x86_64-linux-gnu /usr/lib/../lib /lib
libs=-lpthread -lnsl -lgdbm -ldb -ldl -lm -lcrypt -lutil -lc
-lgdbm_compat
perllibs=-lpthread -lnsl -ldl -lm -lcrypt -lutil -lc
libc=libc-2.26.so
so=so
useshrplib=false
libperl=libperl.a
gnulibc_version='2.26'
Dynamic Linking​:
dlsrc=dl_dlopen.xs
dlext=so
d_dlsymun=undef
ccdlflags='-Wl,-E'
cccdlflags='-fPIC'
lddlflags='-shared -O2 -L/usr/local/lib -fstack-protector-strong'

Locally applied patches​:
uncommitted-changes

---
@​INC for perl 5.27.6​:
lib
/home/cwhitener/.perlbrew/libs/perl-
5.26.1@​normal/lib/perl5/x86_64-linux
/home/cwhitener/.perlbrew/libs/perl-5.26.1@​normal/lib/perl5
/usr/local/lib/perl5/site_perl/5.27.6/x86_64-linux
/usr/local/lib/perl5/site_perl/5.27.6
/usr/local/lib/perl5/5.27.6/x86_64-linux
/usr/local/lib/perl5/5.27.6

---
Environment for perl 5.27.6​:
HOME=/home/cwhitener
LANG=en_US.UTF-8
LANGUAGE (unset)
LD_LIBRARY_PATH (unset)
LOGDIR (unset)
PATH=/home/cwhitener/.perlbrew/libs/perl-
5.26.1@​normal/bin​:/home/cwhitener/perl5/perlbrew/bin​:/home/cwhitener/perl5/perlbrew/perls/perl-
5.26.1/bin​:/usr/local/sbin​:/usr/local/bin​:/usr/sbin​:/usr/bin​:/sbin​:/bin​:/usr/games​:/usr/local/games​:/snap/bin
PERL5LIB=/home/cwhitener/.perlbrew/libs/perl-
5.26.1@​normal/lib/perl5
PERLBREW_BASHRC_VERSION=0.78
PERLBREW_HOME=/home/cwhitener/.perlbrew
PERLBREW_LIB=normal
PERLBREW_MANPATH=/home/cwhitener/.perlbrew/libs/perl-
5.26.1@​normal/man​:/home/cwhitener/perl5/perlbrew/perls/perl-5.26.1/man
PERLBREW_PATH=/home/cwhitener/.perlbrew/libs/perl-
5.26.1@​normal/bin​:/home/cwhitener/perl5/perlbrew/bin​:/home/cwhitener/perl5/perlbrew/perls/perl-
5.26.1/bin
PERLBREW_PERL=perl-5.26.1
PERLBREW_ROOT=/home/cwhitener/perl5/perlbrew
PERLBREW_VERSION=0.78
PERL_BADLANG (unset)
PERL_LOCAL_LIB_ROOT=/home/cwhitener/.perlbrew/libs/perl-
5.26.1@​normal
PERL_MB_OPT=--install_base /home/cwhitener/.perlbrew/libs/perl-
5.26.1@​normal
PERL_MM_OPT=INSTALL_BASE=/home/cwhitener/.perlbrew/libs/perl-
5.26.1@​normal
SHELL=/bin/bash

Thank you very much.

--
James E Keenan (jkeenan@​cpan.org)

@p5pRT
Copy link
Author

p5pRT commented Nov 20, 2017

The RT System itself - Status changed from 'new' to 'open'

@p5pRT
Copy link
Author

p5pRT commented Nov 20, 2017

From @xsawyerx

Not to overcrowd, but I'd like to respond to the patch responses. :)

On 11/20/2017 03​:41 PM, James E Keenan via RT wrote​:

On Fri, 10 Nov 2017 04​:42​:30 GMT, capoeirab@​cpan.org wrote​:

This is a bug report for perl from capoeirab@​cpan.org,
generated with the help of perlbug 1.40 running under perl 5.27.6.

-----------------------------------------------------------------
[Please describe your issue here]

The documentation for IO​::Socket hasn't been updated in quite some
time.
This patch goes through modernizing the documentation and providing
many
more examples. Next up will be IO​::Socket​::INET and IO​::Socket​::UNIX.

Once all of the documentation has been updated, hopefully the updates
will make their way back to the CPAN releases for the IO​:: dist.

Thank you for taking the time to review the IO​::Socket documentation and submit a patch.

Seconded.

[...]

I will, however, comment on your revisions to the SYNOPSIS. Granted, the current synopsis is insufficient. However, we generally follow a style in which the synopsis focuses on terse descriptions of the most important methods or functions which a module provides. More detailed information on particular methods, including more fully fleshed-out examples should be deferred to the sections on those methods or, perhaps, to an EXAMPLES section later in the documentation. More specifically​:

=head1 SYNOPSIS

- use IO​::Socket;

We don't need any of the next four lines in SYNOPSIS.

+ #!/usr/bin/env perl
+ use strict;
+ use warnings;
+ use feature 'say';
+
+ use IO​::Socket qw(AF_INET AF_UNIX SOCK_STREAM SHUT_WR);

I would leave the "use" statement to show how he loaded the module for
his use-case.

+
+ # create a new AF_INET socket

We don't need the 'my' in SYNOPSIS.

Please leave it. I am frustrated when we provide code that doesn't
compile with best practices like "use strict" or "use warnings". I would
like the core documentation examples to be examples of the code we would
like to see, debug, and work on.

+ my $sock = IO​::Socket->new(Domain => AF_INET);

The next two lines should go in discussion of new().

+ # which is the same as
+ $sock = IO​::Socket​::INET->new();
+
+ # create a new AF_UNIX socket
+ $sock = IO​::Socket->new(Domain => AF_UNIX);

Same as above.

+ # which is the same as
+ $sock = IO​::Socket​::UNIX->new();
+

The balance of the revisions should be moved to an EXAMPLES section (once the code is verified).

+ # Let's create a TCP server on localhost​:3333
+ my $server = IO​::Socket->new(
+ Domain => AF_INET,
+ Type => SOCK_STREAM,
+ Proto => 'tcp',
+ LocalHost => '0.0.0.0',
+ LocalPort => 3333,
+ ReusePort => 1,
+ Listen => 5,
+ ) || die "Can't open socket​: $@​";
+ say "Waiting on 3333";

This is pedantic, I know, but any chance to indent these? I would
appreciate it as a reader. :)

+
+ while (1) {
+ # waiting for a new client connection
+ my $client = $server->accept();
+
+ # get information about a newly connected client
+ my $client_address = $client->peerhost();
+ my $client_port = $client->peerport();
+ say "Connection from $client_address​:$client_port";
+
+ # read up to 1024 characters from the connected client
+ my $data = "";
+ $client->recv($data, 1024);
+ say "received data​: $data";
+
+ # write response data to the connected client
+ $data = "ok";
+ $client->send($data);
+
+ # notify client that response has been sent
+ $client->shutdown(SHUT_WR);
+ }
+
+ $server->close();
+
+
+ # A client for such a server could be
+ my $client = IO​::Socket->new(
+ Domain => AF_INET,
+ Type => SOCK_STREAM,
+ proto => 'tcp',
+ PeerPort => 3333,
+ PeerHost => '0.0.0.0',
+ ) || die "Can't open socket​: $@​";
+
+ say "Sending Hello World!";
+ my $size = $client->send("Hello World!");
+ say "Sent data of length​: $size";
+
+ $client->shutdown(SHUT_WR);
+
+ my $buffer;
+ $client->recv($buffer, 1024);
+ say "Got back $buffer";
+
+ $client->close();

The only IO​::Socket methods you used in these examples apart from new() were accept() and close(). Are they used often enough -- not just in creation of servers -- to warrant their being placed in the SYNOPSIS? Are there any other IO​::Socket methods whose importance warrants their being mentioned in the SYNOPSIS?

It might be easier to give succinct examples of the methods used and
leave the client/server examples under "EXAMPLES" and make a note of
"See EXAMPLES for writing a rudimentary client or server". That just
might be the easiest to follow.

Thank you again for the patch! :)

@p5pRT
Copy link
Author

p5pRT commented Nov 20, 2017

From @xsawyerx

On 11/20/2017 08​:46 PM, Sawyer X wrote​:

Not to overcrowd, but I'd like to respond to the patch responses. :)

On 11/20/2017 03​:41 PM, James E Keenan via RT wrote​:

On Fri, 10 Nov 2017 04​:42​:30 GMT, capoeirab@​cpan.org wrote​:

This is a bug report for perl from capoeirab@​cpan.org,
generated with the help of perlbug 1.40 running under perl 5.27.6.

-----------------------------------------------------------------
[Please describe your issue here]

The documentation for IO​::Socket hasn't been updated in quite some
time.
This patch goes through modernizing the documentation and providing
many
more examples. Next up will be IO​::Socket​::INET and IO​::Socket​::UNIX.

Once all of the documentation has been updated, hopefully the updates
will make their way back to the CPAN releases for the IO​:: dist.

[...]
+ my $sock = IO​::Socket->new(Domain => AF_INET);

The next two lines should go in discussion of new().

+ # which is the same as
+ $sock = IO​::Socket​::INET->new();
+
+ # create a new AF_UNIX socket
+ $sock = IO​::Socket->new(Domain => AF_UNIX);

Same as above.

+ # which is the same as
+ $sock = IO​::Socket​::UNIX->new();
+

The balance of the revisions should be moved to an EXAMPLES section (once the code is verified).

+ # Let's create a TCP server on localhost​:3333
+ my $server = IO​::Socket->new(
+ Domain => AF_INET,
+ Type => SOCK_STREAM,
+ Proto => 'tcp',
+ LocalHost => '0.0.0.0',
+ LocalPort => 3333,
+ ReusePort => 1,
+ Listen => 5,
+ ) || die "Can't open socket​: $@​";
+ say "Waiting on 3333";
This is pedantic, I know, but any chance to indent these? I would
appreciate it as a reader. :)

Smylers corrected me on this. Sorry. Thunderbird is now showing this
properly.

@p5pRT
Copy link
Author

p5pRT commented Nov 20, 2017

From cwhitener@gmail.com

Hi Everyone,

It was suggested that I put this in GitHub so that the PR area could be utilized to help with collaborative editing of this proposed patch (I'm the original submitter, but can't figure out my account at the moment).

So, this PR has been put in against the current perl5 mirror on GitHub​:

#12

Also, this gist shows what the proposed changes would look like fully rendered​:
https://gist.github.com/genio/1e47abce1e8899b7b14993e26c7b79b9

If we can agree on changes via this RT, email, IRC, or the PR linked above, I'll be more than happy to alter the patch here accordingly.

Thanks,
Chase

@p5pRT
Copy link
Author

p5pRT commented Nov 22, 2017

From cwhitener@gmail.com

Hi Everyone,

On removing the shebang and use lines and variable declarations with `my` from the synopsis, I tend to agree with Sawyer's viewpoint. We get lots of people newer to Perl join IRC or other forums and ask questions where they've obviously never seen use strict and use warnings and don't know why their code is failing. Having them be able to copy/paste full examples and run them without error is something I think there's great value in.

On moving some of the alternate constructor examples out of the synopsis and into the constructor area, I'm kind of half-and-half in agreement with you. I'd like to leave them in the synopsis to hopefully make it more clear that IO​::Socket somewhat acts as a factory rather than something useful in and of itself. Maybe repeating those examples in the constructor area would be better (also, further constructor docs will be added to the individual :​:INET and :​:UNIX subclasses once I get to working on those).

On which other methods to show in the synopsis, I completely agree that more should be shown. However, I also figured that more in-depth synopsis examples could be shown in the subclasses of IO​::Socket instead. Once :​:INET and :​:UNIX are updated in the same style, I could even argue that the synopsis in IO​::Socket itself gets trimmed down to just show the methods it makes available. Maybe it'd be better to focus the synopsis on the class functions available to make a subclass, such as ->register_domain() and ->configure()?

It might have been better to hold off on this patch until I'd also worked out the docs for :​:INET and :​:UNIX, but I didn't want to go through so much effort if people didn't appreciate the doc style change. Chicken/egg problem.

Thanks,
Chase

@p5pRT
Copy link
Author

p5pRT commented Dec 8, 2017

From @khwilliamson

Does anyone have further commentary about this?

What should be done?

@p5pRT
Copy link
Author

p5pRT commented Jan 17, 2018

From @khwilliamson

Closed at request of OP
--
Karl Williamson

@p5pRT
Copy link
Author

p5pRT commented Jan 17, 2018

@khwilliamson - Status changed from 'open' to 'resolved'

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant