Please enable JavaScript.
Coggle requires JavaScript to display documents.
Sockets (What is it? (A socket is a communication mechanism that allows…
Sockets
What is it?
A socket is a communication mechanism that allows client/server to be developed locally on a single machine or across networks.
You can think os socket connections as telephones calls into a busy building. A call comes into the building and a receptionists sends the call to the correct department.
Sockets are created and used differently from pipes because they make a clear distinction between client and server. The socket mechanism can implement multiple clients attached to a single server.
Socket Attributes
Domains
Domains specify the network medium that the socket communication will use. The most common domain is AF_INET which refers to internet networking.
The underlying protocol is internet protocol (IP), only has one address family, imposes a particular way of specifying computers on a network. This is called the IP Address.
All IP addresses are represented by four numbers under 256, a so called dotted quad. When a client connects across a network via sockets, it needs the IP address of the server computer.
Type
A socket domain may have a number of different ways of communication. Internet protocols provide two communication mechanisms with distinct levels of service which are streams and datagrams.
Stream Sockets
They provide a connection that is sequenced and reliable two way byte stream. Data sent is guaranteed not to be lost, duplicated, or reordered without an indication that an error has occurred.
When specified by the type SOCK_STREAM they are implemented in the AF_INET domain by the TCP/IP connections. Stream sockets are the most commonly used in programming network applications.
Datagram Sockets
They are specified by SOCK_DGRAM and the don't establish and maintain a connection. There is a limit on the size of the datagram that can be sent. The message can get lost, duplicated, or arrive out of sequence.
They are implemented in the AF_INET domain by the UDP/IP connections and provide unsequenced and unreliable services. They are inexpensive in terms of resources because network connections need not be maintained.
Protocol
Where the underlying transport mechanism allows for more than one protocol to provide requested socket type, you can select a specific protocol for a socket.
UNIX don't require you to choose a protocol other than the default which makes the operation way less complex.
Creating a Socket
The socket system call created a socket and returns a descriptor that can be used for accessing the socket. The command is: int socket (int domain, int type, int protocol);
The domain parameter specifies the address family, the type parameter specifies the type of communication to be used in this socket, the protocol parameter specifies the protocol to be employed.
Most common domains that can be used are AF_UNIX, for local sockets in UNIX and Linux file systems, and AF_INET, used for UNIX network sockets. The types can be SOCK_STREAM and SOCK_DGRAM. The protocol used can be defined as 0 for default choice.
Naming Sockets
To make a socket available for use by other processes a server program needs to give the socket a name. AF_UNIX sockets are associated with a file system pathname. AF_INET sockets are associated with an IP port number.
The bind system call assigns the specified address in the parameter to the unnamed socket associated with the file descriptor "socket". The length of the address structure is passed as address_len.
The system call is: int bind(int socket, const struct sockaddr *address, size_t address_len);