# Perspective of Connecting
![[Pasted image 20250125222940.png]]

### **Encoding Techniques**

Encoding techniques are used to convert data into signals for transmission over communication channels. These techniques can be categorized based on the type of data (analog or digital) and the type of signal (analog or digital).

---

### **1. Analog Data to Analog Signal Conversion**
Methods used:  
- **Amplitude Modulation (AM)**  
- **Frequency Modulation (FM)**  
- **Phase Modulation (PM)**

---

### **2. Digital Data to Analog Signals**
Methods used:  
- **Amplitude Shift Keying (ASK)**  
- **Frequency Shift Keying (FSK)**  
- **Phase Shift Keying (PSK)**

---

### **3. Digital Data to Digital Signals**
This involves converting digital data into digital signals for transmission over digital channels. Below are the key techniques:

![[Pasted image 20250125223505.png]]![[Pasted image 20250125223512.png]]
![[Pasted image 20250125223523.png]]
![[Pasted image 20250125223532.png]]
![[Pasted image 20250125223538.png]]
#### **Manchester (1= -ve to +ve ; 0 = +ve to -ve)**


![[Pasted image 20250125223547.png]]
#### Differential Manchester (IEEE)
- always starts from +ve regardless of bit, 
- 0 = start from +ve and go to the opposite side and return back to same polarity
- 1 = change polarity
![[Pasted image 20250125232600.png]]
![[Pasted image 20250125223645.png]]
![[Pasted image 20250125223629.png]]
#### **Polar return to zero(responds to both 0 and 1)**
#### **Bi-Polar return to zero(responds only to 1 and alternates bw the polarity )**

![[Pasted image 20250125225141.png]]
![[Pasted image 20250125230441.png]]

# Error Detection and Correction

### Need for Error Detection and Correction
- **Data Integrity**: Ensures data is not corrupted during transmission.
- **Reliability**: Improves the reliability of communication systems.
- **Efficiency**: Reduces the need for retransmissions, saving bandwidth and time.

### Presence in the OSI Model
- **Data Link Layer**: Implements error detection and correction (e.g., CRC, Hamming Code).
- **Transport Layer**: Ensures end-to-end error detection and correction (e.g., TCP checksums).

### How Error Detection and Correction Happens
1. **Error Detection**:
   - **Parity Checks**: Adds a parity bit to ensure the number of 1s is even or odd.
   - **Checksums**: Computes a value based on the data and appends it for verification.
   - **Cyclic Redundancy Check (CRC)**: Uses polynomial division to generate a checksum.
   - **Hamming Code**: Detects and corrects single-bit errors by adding redundant bits.

2. **Error Correction**:
   - **Automatic Repeat Request (ARQ)**: Requests retransmission of corrupted data.
   - **Forward Error Correction (FEC)**: Corrects errors at the receiver's end without retransmission (e.g., Hamming Code, Reed-Solomon).

### Effects of Error Detection and Correction
- **Positive Effects**:
  - Ensures accurate data delivery.
  - Reduces data loss and corruption.
  - Enhances communication reliability and efficiency.
- **Negative Effects**:
  - Adds overhead (extra bits or retransmissions).
  - May increase latency due to error correction processes.

# Framing (check u1 for breakdown)
![[Pasted image 20250126151738.png]]
![[Pasted image 20250126152049.png]]


## Types of Framing in Data Link Layer

Framing is a process in the Data Link Layer that divides the data stream into manageable units called **frames**. There are two main types of framing:

## 1. Fixed-Sized Framing
- **Definition**: Frames are of a fixed, predefined size.
- **Characteristics**:
  - Each frame has the same number of bits.
  - No need for additional delimiters to indicate the start or end of a frame.
- **Advantages**:
  - Simpler to implement.
  - Easier to manage and process.
- **Disadvantages**:
  - Inefficient for variable-sized data (leads to padding or wasted space).
- **Example**: ATM (Asynchronous Transfer Mode) uses fixed-sized frames called **cells** (53 bytes).

## 2. Variable-Sized Framing
- **Definition**: Frames are of variable sizes, depending on the data being transmitted.
- **Characteristics**:
  - Frames can be of different lengths.
  - Requires delimiters or special patterns to indicate the start and end of a frame.
- **Advantages**:
  - Efficient use of bandwidth (no padding required).
  - Suitable for transmitting data of varying sizes.
- **Disadvantages**:
  - More complex to implement.
  - Requires additional mechanisms to handle frame boundaries (e.g., byte stuffing or bit stuffing).
- **Examples**:
  - **Ethernet**: Uses a variable-sized frame with a maximum size of 1518 bytes.
  - **HDLC (High-Level Data Link Control)**: Uses bit stuffing to mark frame boundaries.

![[Pasted image 20250126152444.png]]![[Pasted image 20250126152451.png]]

# Byte Counting and Byte Stuffing Methods

## Byte Counting
- **Definition**: A framing method where a **count field** in the header specifies the number of bytes in the frame.
- **How It Works**:
  - The sender includes a field in the frame header that indicates the total number of bytes in the frame.
  - The receiver reads the count field to determine the frame's length and extracts the data accordingly.
- **Advantages**:
  - Simple to implement.
  - No need for special delimiters or escape sequences.
- **Disadvantages**:
  - If the count field is corrupted, the receiver may lose synchronization.
- **Example**:
  - Suppose a frame contains the following data: `5 Hello`.
    - `5` is the count field, indicating the frame has 5 bytes of data (`Hello`).
    - The receiver reads `5` and knows the next 5 bytes (`H`, `e`, `l`, `l`, `o`) belong to the frame.

---

## Byte Stuffing 

## What is Byte Stuffing?
A method to **mark the start/end of frames** in variable-sized framing. It uses special **flag bytes** (delimiters). If the flag byte appears in the data, it is "escaped" to avoid confusion.

**working:**  for every flag char encountered, add a esc char before it, same for esc char also (this is only for the image form the ppt not actual explanation)
![[Pasted image 20250126163605.png]]

---

## How It Works (Step-by-Step)
1. **Define Special Bytes**:
   - **Flag Byte**: Marks frame boundaries (e.g., `0x7E`).
   - **Escape Byte**: Signals that the next byte is "stuffed" (e.g., `0x7D`).

2. **Sender Side**:
   - Scan the data for the **flag byte**.
   - Replace every occurrence of the flag byte with:
     - **Escape Byte** + **Modified Flag Byte** (e.g., `0x7D 0x5E`).
   - Add the original flag byte at the **start** and **end** of the frame.

3. **Receiver Side**:
   - Detect the flag byte to identify frame boundaries.
   - If an escape byte is encountered, check the next byte:
     - Replace `0x7D 0x5E` with the original `0x7E` (flag byte).
     - Remove the escape byte to reconstruct the original data.
     - Two consecutive flag bytes indicate the end of one frame and start
	   Of the next one.

---

## Example (Step-by-Step)
### Scenario:
- **Flag Byte**: `0x7E` (marks frame boundaries).
- **Escape Byte**: `0x7D` (signals stuffing).
- **Original Data**: `A 0x7E B` (contains the flag byte `0x7E`).

### Sender Process:
1. Add start flag: `0x7E`.
2. Replace `0x7E` in data with `0x7D 0x5E` (escape byte + modified flag).
3. Add end flag: `0x7E`.
   - **Final Frame**: `[0x7E] A [0x7D 0x5E] B [0x7E]`.

### Receiver Process:
1. Strip start/end flags (`0x7E`).
2. Detect `0x7D` (escape byte) and replace `0x7D 0x5E` with `0x7E`.
   - **Reconstructed Data**: `A 0x7E B`.

---

## Why Use Byte Stuffing?
- **Avoid Misinterpretation**: Prevents the receiver from mistaking a data byte as a frame delimiter.
- **Flexibility**: Handles variable-sized data efficiently.

## Trade-Off:
- **Overhead**: Adds extra bytes (escape sequences), increasing frame size.
## Bit stuffing

Add a random char according to a pattern, for ex after 5 consecutive 1's add a zero. every other detail is the same as Byte stuffing

![[Pasted image 20250126163826.png]]

## Errors

## Types of errors:

### Single:

a single bit is modified
![[Pasted image 20250126164205.png]]
### Double:

two bits are modified
![[Pasted image 20250126164158.png]]
### Burst:

more than two bits are modified, where they need not be in a sequence. the first to the last bit that was corrupted is what is considered.

![[Pasted image 20250126164152.png]]

## Error Detection

Error detection means to decide whether the
received data is correct or not without having a
copy of the original message.

Error detection uses the concept of redundancy,
which means adding extra bits for detecting
errors at the destination.



### Parity checker

#### Simple:

One extra bit is transmitted in addition to the original bits.

To make the number of 1’s even in the case of even parity

odd in the case of odd parity

While creating a frame, the sender counts the amount of 1’s in it.

The receiver just counts how many 1s are in a frame.

If the number of 1s is even and even parity is utilized, the frame is
regarded as uncorrupted and approved.

Similarly for odd parity

The receiver can identify a single bit flip in transit by counting the number of
1s.

![[Pasted image 20250126164454.png]]

#### 2D parity checker:


![[Pasted image 20250126164718.png]]



### Checksum

![[Pasted image 20250126171656.png]]![[Pasted image 20250126171716.png]]
![[Pasted image 20250126171754.png]]![[Pasted image 20250126171813.png]]
![[Pasted image 20250126171840.png]]![[Pasted image 20250126172421.png]]

### CRC METHOD

**Senders side:**
1. There are two parts to this, the divisor and the dividend
2. dividend: Data ; divisor: is just a divisor
3. $n$ = len(divisor), append $n-1$ 0's to the data(dividend)
4. perform division, basically subtracting one by one.(no need to keep track of the quotient) 
	1.  after subtraction, the next data is considered starting only from the first $1$ (LSB $1$)  $\therefore$ all starting $0's$ are ignored
	2. after ignoring the $0's$ we must carry down the necessary number of bits in order to match $n$
5. finally after all subtraction is done, you are left with a remainder (CRC bit), once again ignore all the leading $0's$
6. append this to the data and send

**Receivers side:**
1. perform division as mentioned above with the new data(which includes the remainder)
2. if the new remainder turns out to be all $0's$, you should accept, else reject the transmission

![[Pasted image 20250126174128.png]]
![[Pasted image 20250126174149.png]]

![[Pasted image 20250126174506.png]]
# Reliable Transmission Mechanisms

## **CRC (Cyclic Redundancy Check)**
- **Purpose**: Detects errors in transmitted frames by appending a checksum to the data.
- **Limitation**: Only detects errors (does **not** correct them).

---

## **Error Correction Codes**
- **Capability**: Some codes (e.g., Hamming Code, Reed-Solomon) can **detect and correct** errors.
- **Drawback**: High overhead (extra bits reduce effective bandwidth).
- **Result**: Corrupt frames are typically **discarded** to avoid propagating errors.

---

## **Link-Level Protocol Requirements**
To ensure reliable delivery, protocols must handle:
1. **Lost/discarded frames** (due to errors or congestion).
2. **Mechanisms**:
   - **Acknowledgements (ACKs)**: Receiver sends confirmation of successful frame receipt.
   - **Timeouts**: Sender retransmits if ACK is not received within a predefined time.

---

## **Stop-and-Wait Protocol**
### **How It Works**
1. Sender transmits **one frame**.
2. Waits for an **ACK** from the receiver.
3. If **timeout occurs** (no ACK received), sender retransmits the frame.

### **Problem: Lost/Delayed ACKs**
- **Issue**: If an ACK is lost/delayed, the sender retransmits the same frame.
- **Consequence**: Receiver gets **duplicate frames** (e.g., interprets retransmitted frame 0 as frame 1).

### **Solution: 1-Bit Sequence Numbers**
- **Automatic Repeat Request (ARQ)**:
  - Assign a **1-bit sequence number** (0 or 1) to each frame.
  - Receiver distinguishes between original and retransmitted frames:
    - Example:
      - Sender sends frame `0` → Receiver ACKs `0`.
      - If ACK is lost, sender retransmits `0` → Receiver ignores duplicate `0` but resends ACK.

![[Pasted image 20250126175104.png]]
![[Pasted image 20250126175114.png]]

---

## **Assumptions of Basic Stop-and-Wait**
1. **Perfect Channel**: No data corruption (unrealistic in practice).
2. **No Negative ACKs (NACKs)**: Only positive acknowledgements.
3. **No Timeout Mechanism**: Assumes ACKs arrive instantly.
4. **No Sequence Numbers**: Frames are not uniquely identified.

---

## **Key Enhancements in Practical Protocols**
1. **Sequence Numbers**: Avoid duplicate delivery (e.g., 1-bit in ARQ).
2. **Timeout Timers**: Trigger retransmission if ACK is delayed/lost.
3. **Negative Acknowledgements (NACKs)**: Explicitly signal corrupted/missing frames.
4. **Error Detection**: CRC ensures data integrity before processing.

---

## **Example: Stop-and-Wait with 1-Bit Sequence Numbers**
1. **Sender**:
   - Sends frame `0` → Waits for ACK `0`.
   - Timeout → Retransmits frame `0`.
2. **Receiver**:
   - Receives frame `0` → Sends ACK `0`.
   - Receives duplicate `0` → Discards it, resends ACK `0` (to unblock sender).

