1IO::Socket::INET(3pm) Perl Programmers Reference Guide IO::Socket::INET(3pm)
2
3
4
6 IO::Socket::INET - Object interface for AF_INET domain sockets
7
9 use IO::Socket::INET;
10
12 "IO::Socket::INET" provides an object interface to creating and using
13 sockets in the AF_INET domain. It is built upon the IO::Socket
14 interface and inherits all the methods defined by IO::Socket.
15
17 new ( [ARGS] )
18 Creates an "IO::Socket::INET" object, which is a reference to a
19 newly created symbol (see the "Symbol" package). "new" optionally
20 takes arguments, these arguments are in key-value pairs.
21
22 In addition to the key-value pairs accepted by IO::Socket,
23 "IO::Socket::INET" provides.
24
25 PeerAddr Remote host address <hostname>[:<port>]
26 PeerHost Synonym for PeerAddr
27 PeerPort Remote port or service <service>[(<no>)] | <no>
28 LocalAddr Local host bind address hostname[:port]
29 LocalHost Synonym for LocalAddr
30 LocalPort Local host bind port <service>[(<no>)] | <no>
31 Proto Protocol name (or number) "tcp" | "udp" | ...
32 Type Socket type SOCK_STREAM | SOCK_DGRAM | ...
33 Listen Queue size for listen
34 ReuseAddr Set SO_REUSEADDR before binding
35 Reuse Set SO_REUSEADDR before binding (deprecated,
36 prefer ReuseAddr)
37 ReusePort Set SO_REUSEPORT before binding
38 Broadcast Set SO_BROADCAST before binding
39 Timeout Timeout value for various operations
40 MultiHomed Try all addresses for multi-homed hosts
41 Blocking Determine if connection will be blocking mode
42
43 If "Listen" is defined then a listen socket is created, else if the
44 socket type, which is derived from the protocol, is SOCK_STREAM
45 then connect() is called. If the "Listen" argument is given, but
46 false, the queue size will be set to 5.
47
48 Although it is not illegal, the use of "MultiHomed" on a socket
49 which is in non-blocking mode is of little use. This is because the
50 first connect will never fail with a timeout as the connect call
51 will not block.
52
53 The "PeerAddr" can be a hostname or the IP-address on the
54 "xx.xx.xx.xx" form. The "PeerPort" can be a number or a symbolic
55 service name. The service name might be followed by a number in
56 parenthesis which is used if the service is not known by the
57 system. The "PeerPort" specification can also be embedded in the
58 "PeerAddr" by preceding it with a ":".
59
60 If "Proto" is not given and you specify a symbolic "PeerPort" port,
61 then the constructor will try to derive "Proto" from the service
62 name. As a last resort "Proto" "tcp" is assumed. The "Type"
63 parameter will be deduced from "Proto" if not specified.
64
65 If the constructor is only passed a single argument, it is assumed
66 to be a "PeerAddr" specification.
67
68 If "Blocking" is set to 0, the connection will be in nonblocking
69 mode. If not specified it defaults to 1 (blocking mode).
70
71 Examples:
72
73 $sock = IO::Socket::INET->new(PeerAddr => 'www.perl.org',
74 PeerPort => 'http(80)',
75 Proto => 'tcp');
76
77 $sock = IO::Socket::INET->new(PeerAddr => 'localhost:smtp(25)');
78
79 $sock = IO::Socket::INET->new(Listen => 5,
80 LocalAddr => 'localhost',
81 LocalPort => 9000,
82 Proto => 'tcp');
83
84 $sock = IO::Socket::INET->new('127.0.0.1:25');
85
86 $sock = IO::Socket::INET->new(
87 PeerPort => 9999,
88 PeerAddr => inet_ntoa(INADDR_BROADCAST),
89 Proto => 'udp',
90 LocalAddr => 'localhost',
91 Broadcast => 1 )
92 or die "Can't bind : $IO::Socket::errstr\n";
93
94 If the constructor fails it will return "undef" and set the
95 $IO::Socket::errstr package variable to contain an error message.
96
97 $sock = IO::Socket::INET->new(...)
98 or die "Cannot create socket - $IO::Socket::errstr\n";
99
100 For legacy reasons the error message is also set into the global $@
101 variable, and you may still find older code which looks here
102 instead.
103
104 $sock = IO::Socket::INET->new(...)
105 or die "Cannot create socket - $@\n";
106
107 METHODS
108 sockaddr ()
109 Return the address part of the sockaddr structure for the socket
110
111 sockport ()
112 Return the port number that the socket is using on the local host
113
114 sockhost ()
115 Return the address part of the sockaddr structure for the socket in
116 a text form xx.xx.xx.xx
117
118 peeraddr ()
119 Return the address part of the sockaddr structure for the socket on
120 the peer host
121
122 peerport ()
123 Return the port number for the socket on the peer host.
124
125 peerhost ()
126 Return the address part of the sockaddr structure for the socket on
127 the peer host in a text form xx.xx.xx.xx
128
130 Socket, IO::Socket
131
133 Graham Barr. Currently maintained by the Perl Porters. Please report
134 all bugs at <https://github.com/Perl/perl5/issues>.
135
137 Copyright (c) 1996-8 Graham Barr <gbarr@pobox.com>. All rights
138 reserved. This program is free software; you can redistribute it
139 and/or modify it under the same terms as Perl itself.
140
141
142
143perl v5.36.0 2022-08-30 IO::Socket::INET(3pm)