# 50.012 Lecture 2: Application Layer Overview & Socket Programming
## Network Application Layer
There are 2 application architectures, client-server and peer-to-peer.
### Client-Server Paradigm
**Server**:
* Always-on host
* Permanent IP Address
* Data centers for scaling.
**Client**:
* Communicate with server
* May be intermittently connected
* May have dynamic IP Addresses
* Do not communicate directly with each other.
### Peer-to-Peer Paradigm
* No always-on server
* Arbitrary end systems communicate directly
* Peers request service from other peers, provide service in return to other peers.
* Self scalability -- new peers bring new service capacity, as well as new service demands
* Peers are intermittently connected and change IP addresses
* Complex management.
### Communicating Processes
**Process** is a program running within a host.
Within the same host, two processes could communicate using inter-process communication, which is defined by the OS.
Processes in different hosts communicate by exchanging **messages**.
Client process refers to the process that **initiates** communication.
Server process refers to the process that **waits** to be contacted.
Applications with P2P architecture also have client & server processes.
### Sockets
Processes send/receive messages to/from its **socket**.
Socket is analogous to a door.
* sending a process pushes the message out of the door.
* sending process relies on transport infrastructure on the other side of the door to deliver message to socket at the receiving process.
### Addressing Processes
In order to receive messages, each process must have an **identifier**.
Host device has a unique 32-bit IP Address.
The identifier includes the type of protocol, IP address, and port numbers associated with process on host.
### Application-layer Protocol
It defines:
* Type of messages exchanged, e.g. request, response.
* Message syntax, i.e. what fields in messages & how fields are delineated
* Message semantics, i.e. meaning of information in fields.
* Rules for when and how processes send & respond to messages
There are 2 types of protocols: open protocols and proprietary protocols.
Open protocols are defined in RFCs, and they allow for interoperability. e.g. HTTP, SMTP.
Proprietary protocols are not published to the public. Since the development is closed, it's easier to change and optimize without worrying the impact on other users (no users outside the organization will be using the protocol). e.g. Skype
### HTTP
HTTP stands for hypertext transfer protocol. It is the web's application layer protocol.
HTTP is built with the client/server model: client needs to send a request first, and server will send a response.
#### HTTP Request
#### HTTP Response
## Creating Network Applications
Network applications: Programs that can run on (different) end systems and are able to communicate over network.
The network-core devices (e.g. routers) do not need to run user-supplied applications. Users only need to deploy their applications to the end systems.
### Transport Services
An application usually needs some (or all) of the following services to operate:
#### Process addressing
* (Required) to identify the destination and source of packets.
#### Reliable data transfer
* Some apps require 100% reliable data transfer (TCP).
* Other apps (e.g. audio) can tolerate some loss (UDP).
#### Timing
* Some applications require low delay to be effective (e.g. internet calls, real time online games)
#### Throughput
* Some applications require minimum amount of throughput to be effective.
#### Security
### Internet Transport Protocol Services
#### TCP
* Reliable transport
* Flow control (sender won't overwhelm receiver)
* Congestion control (throttle sender when network overloaded)
* Does not provide timing, minimum throughput guarantee, security.
* Connection-oriented (setup required between client and server processes)
#### UDP
* Unreliable data transfer
* Does not provide reliability, flow control, congestion control, timing, throughput guarantee, security, or connection setup
* Lightweight
Sometimes, it's better to implement the reliability on the application layer rather than on the transport layer.
TCP uses TLS to be implemented at the application layer.
TLS socket API sends cleartext passwords into socket, which will be encrypted before sent over the Internet.
### Socket Programming
There are 2 socket types for 2 transport services:
* UDP (User Datagram Protocol): unreliable datagram
* TCP (Transmission Control Protocol): reliable, byte stream-oriented
#### UDP
No "connection" between client & server
* No handshake before data transfer
* Sender explicitly attaches IP destination address and port number to each packet
* Receiver extracts sender IP address and port number from the received packet
Transmitted data may be lost or received out-of-order.
#### TCP
Client must contact the server
* Server process must first be running
* Server must have created a socket that welcomes client's contact.
Client contacts server by creating TCP socket, specifying IP address, port number of server process.
When client creates the socket, client TCP establishes connection to server TCP.
When contacted by client, server TCP creates a new socket for the server process to communicate with that particular client.
* This allows server to talk with multiple clients.
* Source port numbers are used to distinguish clients.