IO::SOCKET::INET(3perl)
π IO::Socket::INET π
okay bestie, welcome to IO::Socket::INET β it's the Perl manual page for making AF_INET domain sockets with an object interface, and honestly? she's iconic. β¨
β¨ SYNOPSIS β¨
use IO::Socket::INET;
that's it. that's the whole vibe check to get started. π
πΈ DESCRIPTION πΈ
So like, IO::Socket::INET gives you an object interface for creating and using sockets in the AF_INET domain. She's built on top of the IO::Socket interface and inherits literally all the methods IO::Socket defines. no cap. π
π CONSTRUCTOR π
π¦ new ( [ARGS] ) π¦
Creates an IO::Socket::INET object β which is a reference to a newly created symbol, see the Symbol package if you wanna go deeper. new optionally takes arguments, and those arguments come in key-value pairs. π«Ά
On top of the key-value pairs that IO::Socket already accepts, IO::Socket::INET brings these to the group chat:
PeerAddrβ Remote host address, in the form<hostname>[:<port>]πPeerHostβ Synonym forPeerAddrPeerPortβ Remote port or service, in the form<service>[(<no>)] | <no>LocalAddrβ Local host bind address,hostname[:port]LocalHostβ Synonym forLocalAddrLocalPortβ Local host bind port,<service>[(<no>)] | <no>Protoβ Protocol name (or number):"tcp" | "udp" | ...Typeβ Socket type:SOCK_STREAM | SOCK_DGRAM | ...Listenβ Queue size for listenReuseAddrβ SetSO_REUSEADDRbefore bindingReuseβ SetSO_REUSEADDRbefore binding (deprecated, preferReuseAddr) β οΈReusePortβ SetSO_REUSEPORTbefore bindingBroadcastβ SetSO_BROADCASTbefore bindingTimeoutβ Timeout value for various operationsMultiHomedβ Try all addresses for multi-homed hostsBlockingβ Determine if connection will be blocking mode
okay so here's how the logic flows: if Listen is defined, then a listen socket gets created. otherwise, if the socket type β which is derived from the protocol β is SOCK_STREAM, then connect() gets called. and if the Listen argument is given but it's false, the queue size gets set to 5. π
Now, lowkey important: even though it's not illegal, using MultiHomed on a socket that's in non-blocking mode is of little use. Why? Because the first connect will never fail with a timeout, since the connect call won't block. so like, don't. π
The PeerAddr can be a hostname or an IP address in the xx.xx.xx.xx form. The PeerPort can be a number or a symbolic service name. That service name might be followed by a number in parentheses, which gets used if the service isn't known by the system. And plot twist β the PeerPort spec can also be embedded right inside the PeerAddr by preceding it with a :. π
If Proto isn't given and you specify a symbolic PeerPort port, then the constructor will try to derive Proto from the service name. As a last resort, Proto tcp is assumed. The Type parameter gets deduced from Proto if it's not specified. π§
And if the constructor is only passed a single argument, it's assumed to be a PeerAddr specification. simple as that.
If Blocking is set to 0, the connection will be in nonblocking mode. If it's not specified, it defaults to 1 (blocking mode). π
Examples:
$sock = IO::Socket::INET->new(PeerAddr => 'www.perl.org',
PeerPort => 'http(80)',
Proto => 'tcp');
$sock = IO::Socket::INET->new(PeerAddr => 'localhost:smtp(25)');
$sock = IO::Socket::INET->new(Listen => 5,
LocalAddr => 'localhost',
LocalPort => 9000,
Proto => 'tcp');
$sock = IO::Socket::INET->new('127.0.0.1:25');
$sock = IO::Socket::INET->new(
PeerPort => 9999,
PeerAddr => inet_ntoa(INADDR_BROADCAST),
Proto => 'udp',
LocalAddr => 'localhost',
Broadcast => 1 )
or die "Can't bind : $IO::Socket::errstr\n";
If the constructor fails it will return undef and set the $IO::Socket::errstr package variable to contain an error message. π«Ά
$sock = IO::Socket::INET->new(...)
or die "Cannot create socket - $IO::Socket::errstr\n";
For legacy reasons the error message is also set into the global $@ variable, and you may still find older code which looks here instead. it's giving old money, honestly.
$sock = IO::Socket::INET->new(...)
or die "Cannot create socket - $@\n";
π METHODS π
sockaddr ()β Return the address part of thesockaddrstructure for the socketsockport ()β Return the port number that the socket is using on the local hostsockhost ()β Return the address part of thesockaddrstructure for the socket in a text formxx.xx.xx.xxpeeraddr ()β Return the address part of thesockaddrstructure for the socket on the peer hostpeerport ()β Return the port number for the socket on the peer host.peerhost ()β Return the address part of thesockaddrstructure for the socket on the peer host in a text formxx.xx.xx.xx
π SEE ALSO π
Socket, IO::Socket β go read those too, bestie, they're the blueprint. β
π AUTHOR π
Graham Barr. Currently maintained by the Perl Porters. Please report all bugs at <https://github.com/Perl/perl5/issues>. (yes, actual URL, go be a good citizen and file that bug π)
π COPYRIGHT π
Copyright (c) 1996-8 Graham Barr <gbarr@pobox.com>. All rights reserved.
This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
periodt. that's the whole page, and you ate it. π
okay so that was IO::Socket::INET from the Perl Programmers Reference Guide β this version is from perl v5.44.0, dated 2026-10-17, and it's exactly the same facts as the IO::Socket::INET(3perl) manpage, just with way better energy. slay, go make some sockets, and don't forget ReuseAddr instead of the deprecated Reuse. mwah π«Άπβ¨
π¦ where this page comes from
- arch rolling: perl 5.44.0-1 Β· Artistic-1.0-Perl AND GPL-1.0-or-later
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms