Home > Articles > Web Development > Perl

  • Print
  • + Share This
Like this article? We recommend

Like this article? We recommend

Basic Socket Functions

The following are some of the most basic function names:

socket (...)
bind (...)
connect (...)
select (...)
listen (...)
accept (...)
recv (...)
send (...)
shutdown (...)
close (...)

In this section, I have tried to explain the basic socket functions as they are defined in Perl. However, the general concept is the same for usage in C under UNIX and WINSOCK under Windows.

socket(Socket-Desc, Domain, Type, Protocol)

This function is used to create a new socket.

Socket-Desc is n unique identifier (like a file handle) for the socket created.

Domain is the address family, such as AF_INET (address family Internet), when ports are used for communication.

Type is the way in which data will be transmitted, such as in a continuous, consecutive, connection-oriented, reliable stream of bytes (SOCK_STREAM) in independent individual, connectionless, unreliable packets (SOCK_DGRAM) in the form of raw bytes (SOCK_RAW).

Protocol is the low-level protocol used for data transmission. This is TCP for stream sockets and UDP for datagram sockets.

bind(Socket-Desc, Packed-Structure-Address)

This function associates the server socket descriptor with information specified in the second parameter, especially the port on which the server will be listening for requests from clients.

Socket-Desc is the unique socket descriptor obtained from the socket function.

Packed-Structure-Address is a collection of information, such as the address family, the server port, any client address indicator stored in a particular format that can be understood by the bind function. An example is: <unsigned short><short in network order><null padded 4 char string><8 null bytes>.

connect(Socket-Desc, Packed-Structure-Address)

The connect function is used by a client to establish connection to a server.

Socket-Desc is the client socket descriptor.

Packed-Structure-Address is a collection of information about the server to connect to, such as the address family, server port, and server IP address, stored in a particular format that can be understood by the connect function. Here's an example: <unsigned short><short in network order><null padded 4 char string><8 null bytes>.

select(Socket-Desc/s)

The select function is used to check the status of a socket descriptor to determine, for example, whether it is ready for reading or writing, or whether it has an error condition pending.

listen(Socket-Desc, QueueSize)

This function is used by a connection-oriented server to specify the number of client requests that are allowed at a time and are queued for service.

Socket-Desc is the server socket descriptor on which the server is listening for requests from clients.

QueueSize specifies the number of client requests that can be queued until they can be serviced by the server on that socket. If there are more client requests than the number specified, then the client making the request might receive "connection refused" errors.

accept (Client-Socket-Desc, Server-Socket-Desc)

The accept function is used by a connection-oriented server to wait for and accept a client connection request.

Client-Socket-Desc is the new socket descriptor that is created by the function to represent the client whose request the server has accepted and will be processing.

Server-Socket-Desc is the server socket descriptor on which the server listens and accepts client requests for processing.

recv (Socket-Desc, Data-Buffer, Data-Buffer-length, Flags)

This function is used to receive data.

Socket-Desc is the socket descriptor that receives the data.

Data-Buffer specifies where the data received is stored.

Data-Buffer-Length is the size of the buffer, in bytes.

Flags are optional; they affect the way data is received.

send (Socket-Desc, Data-Buffer, Flags)

This function is used to send data.

Socket-Desc is the socket descriptor used to send data.

Data-Buffer specifies where the data to be sent is stored.

Flags are optional; they affect the way data is sent.

shutdown (Socket-Desc, How)

This function is used before the close function in case of a connection-oriented socket because there might be information ready to be sent or received.

Socket-Desc specifies the socket descriptor to be closed.

If the How value is 0, receiving bytes is disabled in this socket. If it is 1, sending bytes is disabled on this socket. If it is 2, both sending and receiving bytes is disabled on this socket.

close (Socket-Desc)

This function is used to close the socket and free the Socket-Desc.

Socket-Desc is the socket descriptor representing the socket to be closed.

This function does not seem to work in Perl, but it is a socket system call in C under UNIX.

  • + Share This
  • 🔖 Save To Your Account