What are sockets?

NOTE: The information provided in this manual about sockets is just an introduction, and is intended to give you a basic understanding of how to write Irie Pascal programs that use sockets. If you intend to write serious programs (i.e. programs that you use for important purposes and which must be robust and reliable) that use sockets, then you should consult more advanced and complete sources of information about sockets.

Introduction To Sockets

Sockets were first introduced by the University of California, Berkeley, in their version of UNIX, as a method for processes (i.e. running programs) to communicate with each other. Sockets are now widely supported by many operating systems, and the concept has been expanded to include processes communicating over a network.

Sockets provide a useful abstraction that insulates communicating processes, to some extent, from the details about how the communication is done. Programs can basically treat each socket as an end-point in a communications link.

Most programs that use sockets follow a client/server model, and are either clients or servers. The major difference between client and server socket programs is the way in which they establish connections with each other. Basically a server socket program creates a socket and then uses this socket to listen for attempts by client socket programs to connect. A client socket program, on the other-hand creates a socket and then tries to connect this socket to the socket created by the server program. Once a connection has been established, the difference between client and server socket programs becomes less important because data can be sent and received between sockets, in both directions.

Socket Names

The term name, when applied to sockets, means the unique reference used to identify sockets within an address family. The elements that make up the name of a socket could vary depending on the address family the socket belongs to. Most of the sockets that you are likely to use will belong to the internet address family, and their names will be made of the following three elements:

  1. Address family - Identifes the name as belonging to the internet address family
  2. Host address - IP address (e.g.
  3. Port number - TCP/IP port number

Socket Types

Stream Sockets

Stream sockets are connection-oriented, and must be in a connected state (i.e. they must have established a connection to another socket) before any data can be sent or received. A connection to another socket is created with a call to the connect function. Once connected, data can be transferred, between the sockets, using the send, sendto, recv, and recvfrom functions.

Datagram Sockets

Datagram sockets are not connection-oriented, and are used to send and receive blocks of data, called datagrams.

In order to send datagrams, both the source socket and the name of one or more destination sockets, must be specified. The sendto function, which allows you to specify both a source socket and the name of one or more destination sockets, is normally used to send datagrams. The send function can also be used to send datagrams, however because the send function does not allow you to specify the name for the destination datagram socket or sockets, the name of the destination datagram socket or sockets must have been previously specified by the connect function.

The recvfrom function can be used to receive datagrams, and can optionally return the name of the socket that sent the datagram. The recv function can also be used to receive datagrams, however the connect function must have been used previously to specify the name for the source datagram socket or sockets.

Reference Information

As you might expect, the internet is an excellent source of information about sockets. The following four web sites seemed to contain particularly useful information about sockets, at the time this manual was created: