Sockets explained

They act as endpoints in a two-way communication channel.
For communication of two machines or applications each app/machine creates a socket.
Each socket is associated with IP and port. 

OSI
Sockets operate at layer 4 - transport layer.
Application layer calls down the socket api, for example browser, backend server etc

Types of Sockets:
- TCP(Transmission Control Protocol)
Connection oriented and responsible for ensuring reliable, ordered, error checked, non duplicate data transmission.
Before data is sent a three way handshake is performed to establish the connection.(exchange of SYN and ACK packets)
        - Client sends a SYN(synchronize) packet to initiate a connection.
        - SYN-ACK server responds with a SYN-ACK packet, acknowledging the receipt of SYN and sending its own SYN request.        - ACK the client sends an ACK (acknowledgment) packet confirming the servers response and the connection is established.

Its ideal and widely used for applications like browing(HTTP). database access, file transfer(FTP), email(SMTP)
If any packet is lost or corrupted they will be retransmitted.
Even if packets arrive out of order TCP ensures reassambling in correct order.
Each TCP segment has a checksum that allows the receiver to detect errors in the data.

- UDP(User Datagram Protocol)
UDP is connectionless and unreliable. 
doesnt establish a connection.
It sends datagrams to the target ip:port without handshake.
Receiver doesnt confirm the receipt of the data.
UDP makes no guarantee that the data will be delivered.
No guarentee of delivery, ordering or integrity.
Error handling is done on application, if needed.
Faster and lightweight.

Ideal for real-time applications like streaming where speed is cruicial.


Socket Lifecycle

Socket is created and bound to a specific ip and port.
Socket doesnt engage in communication but waits for incoming connections.
Once a client initiates a connection, server accepts and creates a new socket instance that is dedicated to that client and original socket continues listening for new requests.
This new socket instance is channel for all communication between server and that client.
A server can handle many different clients concurrently each with its own socket.

Every socket is identified by a 5 tuple: protocol, source ip, source port, dest. ip and dest port.
Allows os to distinguish between multiple simultaneous connections.
Multiple browser tabs connecting to blog.kurttekin.com:80 will each have a unique source port assigned by the os which allows the server and the client to differentiate between those sessions.


Socket Security

Sockets transmit raw data unless explicitly encrpyted.
Secure communication achived via Transport Layer Security(TLS)/Secure Sockets Layer(SSL)
SSL/TLS encrypts data so even if someone intercepts/sniffs packets they wont be able to read.
When handshake happens they exchange encyption key.
All data encrypted using that key and only the client and server can decrypt this data.

Sockets are everywhere

Email(SMTP, IMAP, POP3)
File Transfers(FTP/SFTP)
DNS Resolution
SSH, RDP, VNC, VPN, P2P
Distributed systems and microservices 
Restful apis over HTTP 
gRPC over HTTP/2