Perl Socket Functions
Internet Sockets and Perl – Part 2
Writing a Perl Module
Foreword: Perl comes with a module, called Socket. This module has functions for creating and using sockets. I talk about the functions in this part of the series.
By: Chrysanthus Date Published: 18 Oct 2014
Introduction
use Socket;
The socket Function
The syntax for this function is:
socket (SOCKET,DOMAIN,TYPE,PROTOCOL)
The function opens a new socket. Whenever anything is opened in Perl, what is returned is called a filehandle, even though we may not necessarily be dealing with a real file. The filehandle is held by the argument, SOCKET, which is not preceded by the $ sign. You can use any name such as SOCK for SOCKET, but do not precede it with the $ sign. The value for the DOMAIN argument can be the constant, AF_INET or AF_INET6. Neither value is preceded by the $ sign. If the IP address for the destination socket is IPv4, then the value has to be AF_INET. If the IP address for the destination socket is IPv6 then the value has to be, AF_INET6. The TYPE argument can be the constant, SOCK_STREAM or SOCK_DGRAM for the socket types, Stream or Datagram, respectively. The argument, PROTOCOL, can be gotten from “TCP” or “UDP” (see below).
The getprotobyname Function
This function takes the string "tcp" or "udp" and returns the corresponding special name for the protocol suited for the socket() function. The syntax is:
getprotobyname("abbreviation")
where abbreviation can be "tcp" or "udp".
The gethostbyname Function
Remember, each domain name has a corresponding IP address. The ordinary user prefers to use the domain name for a computer (website) instead of the IP address because it is easier to remember the domain name than the IP address. This function takes the domain name as argument and returns the IP address. The syntax is:
gethostbyname("domanName")
The www in a website address is not really part of the domain name. For example, you should type "google.com" and not "www.google.com", as argument for the function.
A Packed Address is a combination of the port number and the IP address of a program at either end of the socket channel. Remember, a program at the end of a socket channel is mainly identified by the combination of the port and the IP address. This function can take the port number and the IP address and return the packed address. The syntax is:
sockaddr_in(port, IP)
The connect Function
The syntax is:
connect (SOCKET, packedAddress)
The creation of the socket with the socket() function does not do connection. You have to do the connection with the connect function. Here, SOCKET is the filehandle created with the socket() function and the second argument is the packed address of the remote socket. This connect function is normally used at the client, to connect to a server that is listening. This function returns true if it succeeds, false otherwise. Note: the creation of a socket creates an endpoint for communication and returns a file descriptor (filehandle) for the socket. The creation of a socket describes the kind of IP address to use, the socket type, and the protocol, for the local host (computer). Note the difference between creating a socket and doing a connection.
The bind Function
When a socket is created using the socket() function, it is only given a protocol family, but not assigned a socket address (packed address). The bind() function associates a socket with a packed address (socket address). This function is typically used at the server. The syntax is:
bind (SOCKET, NAME)
where SOCKET is the filehandle (see above) and NAME is the packed address. This function returns true if it succeeds or false otherwise.
After a socket has been associated with a socket address (packed address), listen() prepares it for incoming connections. However, this is only necessary for the stream-oriented (connection-oriented) data modes, i.e, for TCP protocol and its socket type, SOCK_STREAM. The syntax is:
listen (SOCKET,QUEUESIZE)
where SOCKET is the filehandle (a valid socket descriptor) and QUEUESIZE is an integer representing the number of pending connections that can be queued up at any one time. The operating system usually places a cap on this value. This function returns true if it succeeds or false otherwise. This function is usually applied at the server. Note: a socket descriptor is also called a file descriptor. The listen function makes the socket listen for a connection. Once a connection is accepted, the connection is dequeued.
The accept() Method
When a program (socket) is listening for stream-oriented connections (for TCP protocol and its socket type, SOCK_STREAM) from other hosts (clients), it is notified of such events and must initialize the connection using the accept() function. The accept() function creates a new socket for each connection and removes the connection from the listen queue. The syntax is:
accept (NEWSOCKET,GENERICSOCKET)
The completion of the accept operation results in two sockets, the generic (original or listen socket) and the new socket. All further communication with the remote host (client) now occurs through this new socket. The function returns the packed address for the new socket if it succeeds, false otherwise.
The syntax of this function is:
send (SOCKET,MSG,FLAGS)
This function sends the string message, MSG of the socket, SOCKET to the socket at the other end of the channel. It returns the number of characters sent, or the undefined value on error. You can set FLAGS to zero.
The recv Method
The syntax of this function is:
recv (SOCKET,SCALAR,LENGTH,FLAGS)
This function receives a message on the socket. It attempts to receive LENGTH characters of data into variable SCALAR from the specified SOCKET filehandle. SCALAR will be grown or shrunk to the length actually read. Choose LENGTH to be the maximum number of characters expected. The function returns the address of the sender if SOCKET's protocol supports this; returns an empty string otherwise. If there's an error, it returns the undefined value. As for FLAGS for this and the previous function, type 0.
The close Function
The socket function opens a socket making computer recourses available for the socket’s use. The close function closes the socket releasing the system’s recourses allocated to the socket. The syntax is:
close SOCKET
Any socket opened should be closed after use.
I know you have been longing to see some code. No problem. Let us take a break here and continue in the next part of the series, where you will see socket coding.
Chrys
Related Links
Internet Sockets and PerlPerl pack and unpack Functions
Writing MySQL Protocol Packets in PurePerl
Developing a PurePerl MySQL API
Using the PurePerl MySQL API
Database
Perl Course
MySQL Course
More Related Links
Perl Mailsend
PurePerl MySQL API
Perl Course - Professional and Advanced
Major in Website Design
Web Development Course
Producing a Pure Perl Library
MySQL Course
BACK NEXT