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.
For applications to work with TCP/IP, Application Program Interface (API) is required. There are no standards defining how applications should work with TCP/IP. However, the API of the BSD Unix with TCP/IP has become the de factor standards for many systems. Asocket is an end point for interprocess communication, in this case, over a network running TCP/IP. The socket interface can support a number of underlying transport mechanisms. Ideally, a program written with socket calls can be used with different network architectures and different local interprocess communication facilities with few or no changes.
Sockets can simultaneously transmit and receive data from another process, using semantics that depend on the type of socket. There are three types of sockets: stream, datagram, and raw, each of which represents a different type of communications service.
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.
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.
Raw sockets provide direct access to the lower-layer protocols, for example, IP and the Internet Control Message Protocol (ICMP).
The sockets mechanism is accessed via a number of functions as follows:
| socket() | create a socket |
| bind() | associate a socket with a network address |
| connect() | connect a socket to a remote network address |
| listen() | wait for incoming connection attempts |
| accept() | accept incoming connection attempts |
In addition the functions setsockopt(), getsockopt(), fcntl() and ioctl() may be used to manipulate the properties of a socket, the function select() may be used to identify sockets with particular communication statuses. The function close() may be used to close a socket liaison.
Data can be written to a socket using any of the functions write(), writev(), send(), sendto() and sendmsg(). Data can be read from a socket using any of the functions read(), readv(), recv(), recvfrom() and recvmsg().
The sockets mechanism is usually used to implement client-server applications. The client process is directly or indirectly user driven whereas the server process sits on a host waiting for incoming connections. A server process will run unattended and continuously. In the Unix environment such processes are called daemon processes.

Sockets in TCP/IP Networking 1

Sockets in TCP/IP Networking 2
