Sockets are the combination of IP addresses plus corresponding TCP/UDP port numbers. For applications to work with TCP/IP, Application Program Interface (API) is required. API serves as an interface between different software programs and facilitates their interaction, similar to the way the user interface facilitates interaction between humans and computers.

Sockets come in two primary flavors

An active socket is connected to a remote active socket via an open data connection. Closing the connection destroys the active sockets at each endpoint.

A passive socket is not connected, but rather awaits an incoming connection, which will spawn a new active socket.

A socket is not a port, though there is a close relationship between them. A socket is associated with a port, though this is a many-to-one relationship. Each port can have a single passive socket, awaiting incoming connections, and multiple active sockets, each corresponding to an open connection on the port.

                                       

Sockets are   the   combination   of   IP   address   plus corresponding TCP/UDP port numbers. It is like PBX phone systems, where the IP address is the phone number, and the port is the extension. Every paired of connected socket has a source IP/port and a destination IP/port. Users of Internet applications are normally aware of all except the local port number, this is allocated when connection is established and is almost entirely arbitrary unlike the well-known port numbers associated with popular applications.

There are three types of sockets:

1. Stream

Stream sockets provide reliable, connection-based communications. In connection- based communications, the two processes must establish a logical connection with each other. A stream of bytes is then sent without errors or duplication and is received in the order in which it was sent. Stream sockets correspond to the TCP protocol in TCP/IP.

2. Datagram 

Datagram sockets communicate via discrete messages, called datagrams, which are sent as packets. Datagram sockets are connectionless; that is, the communicating processes do not have a logical connection with each other. The delivery of their data is unreliable. The datagrams can be lost or duplicated, or they may not arrive in the order in which they were sent. Datagram sockets correspond to the UDP protocol in TCP/IP.

3. Raw

Raw sockets provide direct   access   to the   lower-layer   protocols,   for example, IP and the Internet Control Message Protocol (ICMP).