Socket Functions Related to Outgoing Connections

We'll now look at the functions related to creating sockets and establishing outgoing TCP connections in more detail.

$boolean = socket (SOCKET,$domain,$type,$protocol)

Given a filehandle name, a domain, a type, and a protocol, socket() creates a new socket and associates it with the named filehandle. On success, the function returns a true value. On error, socket() returns undef and leaves the error message in $!.

The domain, type, and protocol are all small integers. Appropriate values for the first two are constants defined in the Socket module, but the protocol value must be determined at run time by calling getprotobyname(). For creating TCP sockets, the idiom is typically

socket(SOCK,AF_INET,SOCK_STREAM, ...

Get Network Programming with Perl now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.