IO::Socket::INET(3pm)
π NAME π
IO::Socket::INET β ok so this is the object interface for AF_INET domain sockets, bestie. That's the whole vibe. Internet sockets. Objects. Slay.
β¨ SYNOPSIS β¨
use IO::Socket::INET;
That's literally it, you're in. π
π¦ DESCRIPTION π¦
IO::Socket::INET gives you an object interface for creating and using sockets in the AF_INET domain. It's built on top of the IO::Socket interface, and it inherits all the methods defined by IO::Socket β so everything your fave IO::Socket can do, this one can do too. No cap. π
πΈ CONSTRUCTOR πΈ
new ( [ARGS] )
Creates an IO::Socket::INET object β and heads up, that object is a reference to a newly created symbol (see the Symbol package, iykyk). new optionally takes arguments, and those arguments come in key-value pairs. Very demure, very mindful.
In addition to the key-value pairs accepted by IO::Socket, IO::Socket::INET also provides these:
PeerAddrβ remote host address, in the form<hostname>[:<port>]PeerHostβ synonym forPeerAddr(same thing, different font π )PeerPortβ 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), like"tcp"|"udp"| ...Typeβ socket type, likeSOCK_STREAM|SOCK_DGRAM| ...Listenβ queue size for listenReuseAddrβ setSO_REUSEADDRbefore bindingReuseβ setSO_REUSEADDRbefore binding (deprecated, preferReuseAddrβ she's outdated, let her go π)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
Now the actual behaviour, pay attention bestie π
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() is called. And if the Listen argument is given but false, the queue size will be set to 5. Little default, doing its thing.
Ok so you can use MultiHomed on a socket that's in non-blocking mode, it's not illegal β but it's of little use, and here's why: the first connect will never fail with a timeout, because the connect call won't block in the first place. So there's nothing to time out on. Make it make sense? It does, actually.
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 is used if the service isn't known by the system. And the PeerPort spec can also be embedded in the PeerAddr by preceding it with a : β sneaky, but iconic.
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 you didn't specify it. She figures it out. π
If the constructor is only passed a single argument, it's assumed to be a PeerAddr specification.
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";
Look at that last one go. Beautiful. π«Ά
π When it goes wrong π
If the constructor fails, it will return undef and set the $IO::Socket::errstr package variable to contain an error message. Like this:
$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 β so you may still find older code looking there instead. Bless her, she's doing both. π
$sock = IO::Socket::INET->new(...)
or die "Cannot create socket - $@\n";
π METHODS π
sockaddr ()β returns the address part of the sockaddr structure for the socketsockport ()β returns the port number that the socket is using on the local hostsockhost ()β returns the address part of the sockaddr structure for the socket, in text formxx.xx.xx.xxpeeraddr ()β returns the address part of the sockaddr structure for the socket on the peer hostpeerport ()β returns the port number for the socket on the peer host.peerhost ()β returns the address part of the sockaddr structure for the socket on the peer host, in text formxx.xx.xx.xx
π§ SEE ALSO π§
Socket, IO::Socket β go read those too, they're the supporting cast and honestly they ate. β
π AUTHOR π
Graham Barr. Currently maintained by the Perl Porters. Please report all bugs at <https://github.com/Perl/perl5/issues>. (Report the bugs, bestie, they wanna hear it. π)
π 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.
and that's the whole page, bestie! πβ¨π
this little legend is from perl v5.42.3, dated 2026-11-03 β go forth and socket responsibly π¦πΈ
π¦ where this page comes from
- alpine 3.24: perl-doc 5.42.3-r0 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms