Socket In UNIX

Introduction to Sockets

  • Sockets are the “virtual” endpoints of any kind of network communications done between 2 hosts over in a network.
  • Sockets are uniquely identified by:
    • An internet Address
    • An end-to-end protocol(TCP or UDP protocol)
    • A port number
  • One major difference between the socket and other inter-process communication is that socket allows communication between the process on different system unlike other which allows communication in a single system.
  • For example, when we connect to http://www.google.com in our web browser, it opens a socket on port 80 and connects to google.com to fetch the page and show it to us.

Diag-1: Sockets

Types of Socket:
There are three types of sockets:

  • Stream Socket
  • Datagram Socket
  • Raw Socket

Stream Socket:
These are connection-oriented sockets, which use Transport Control Protocol. Each packet has a sequence number and transmits data reliably and in order with the support of error and flow control.

Datagram Socket:
These are connectionless socket, which uses UDP. Each packet sent or received on a datagram socket is individually addressed. Order and reliability are not guaranteed with datagram sockets.

Raw Socket:
It allows direct sending and receiving of IP packets without any protocol-specific transport layer formatting that is packet bypasses Layer 4. One of the common examples of raw sockets is Wireshark(packet Analyzer).

Socket Address Family or Domain:
The address family specifies the format of address structure to be used on socket API
There is three important address family:

  • AF_INET Address Family
  • AF_INET6 Address Family
  • AF_UNIX Address Family

AF_INET Address Family:

  • This is the most common address family which is used for IPv4 internet addressing.
  • Addresses for AF_INET sockets are IP address (such as 130.99.128.1) or in its 32–bit form (X’82638001′) and port numbers.
  • The AF_INET address family uses the sockaddr_in structure.
struct sockaddr_in {
short int sin_family;
unsigned short int sin_port;
struct in_addr  sin_addr;
};

struct in_addr {
  unsigned long int s_addr;
};

AF_INET6 Address Family:

  • This is used for Internet Protocol version 6(IPv6).
  • AF_INET6 address family uses a 128 bit address.
  • It uses sockaddr_in6 structure.

AF_UNIX Address Family:

  • This address family is used for Unix Domain Sockets(UDS).
  • It provides inter-process communication between processes that run on the same system.
  • The address is actually a path name to an entry in the file system, hence also known as file system sockets.
  • Sockets with the address family AF_UNIX use the sockaddr_un address structure

Relevant Posts:



Categories: Operating system (OS)

2 replies

Trackbacks

  1. Index of Operating System - Tech Access
  2. Address Manipulation:inet_ntoa, inet_aton, inet_addr - Tech Access

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: