0% found this document useful (0 votes)
2 views55 pages

TCP IP Socket Programming - Flow Control

The document covers socket programming in TCP/IP and UDP, focusing on process-to-process communication facilitated by port numbers. It details the TCP/IP protocol stack, including the roles of different layers and the characteristics of TCP and UDP. Additionally, it provides examples of socket programming in Java and discusses flow control problems related to TCP connections.

Uploaded by

hridayjadhav777
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views55 pages

TCP IP Socket Programming - Flow Control

The document covers socket programming in TCP/IP and UDP, focusing on process-to-process communication facilitated by port numbers. It details the TCP/IP protocol stack, including the roles of different layers and the characteristics of TCP and UDP. Additionally, it provides examples of socket programming in Java and discusses flow control problems related to TCP connections.

Uploaded by

hridayjadhav777
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Course

BCSE308L – Computer Networks

Topic

Socket Programming - TCPIP & UDP

Dr. B. Srinivasan
Assistant Professor Senior - SCOPE
Process-to-Process Delivery
• The transport layer is responsible for process-to-process delivery
• The delivery of a packet, part of a message, from one process to
another.
• Two processes communicate in a client/server relationship
• Three Types of Communication
1. Node-to-Node Communication (Data Link Layer)
2. Host-to-Host Communication (Network Layer)
3. Process-to-Process Communication (Transport Layer)
Process-to-Process Delivery
Types of Communication
1. Node-to-Node Communication (Data Link Layer)
o Happens between two directly connected devices (nodes).
o Example:
 Computer → Router → Router → Router → Computer
o Each step is one hop.
o Responsibility:
 Frame delivery
 Error detection (sometimes correction)
Types of Communication
2. Host-to-Host Communication (Network Layer)
o Communication between source computer and
destination computer.
o Uses IP addresses.
o Handles:
 Routing (choosing path)
 Logical addressing
Types of Communication
3. Process-to-Process Communication (Transport Layer)
o Communication between applications (processes) running
on the computers.
o Uses port numbers.
o Ensures:
 Correct application receives data
 Reliability (TCP) or speed (UDP)
Socket Programming
• Socket programming is a way to enable communication between
two devices over a network.
• A socket is an endpoint for sending and receiving data.
• It allows processes running on different systems to
communicate.
Port Numbers
• A port number uniquely identifies a specific application (process)
on a computer.
• While IP address identifies the device, port number identifies the
program (process) inside that device.
• Port numbers make process-to-process communication possible
• They ensure the right application receives the right data
• Port numbers range from 0 to 65,535
• Used by the Transport layer (TCP/UDP) to identify applications
Port Numbers

Destination Port Number


Source Port Number

Source Port Number Destination Port Number


IP Address vs Port Numbers

Simple Analogy

• IP address = Apartment building

• Port number = Room number

• Data = Message
Types of Port Numbers
1. Well-Known Ports (0 – 1023)
• Reserved for standard (core) services (servers)
• Fixed and globally recognized
• Examples:
• HTTP → 80
• HTTPS → 443
• FTP → 21
• DNS → 53
• Daytime → 13
Types of Port Numbers
2. Registered Ports (1024 – 49,151)
• Assigned to user or specific applications
• Not as strictly reserved as well-known ports
• Assigned by: Internet Assigned Numbers Authority (on request)
• Examples:
• Database services
• Custom applications
• Game servers
• MySQL → Port 3306
• PostgreSQL → Port 5432
• Tomcat → Port 8080
Types of Port Numbers
3. Dynamic / Ephemeral Ports (49,152 – 65,535)
• Used temporarily by clients
• Automatically assigned when a connection starts
• Examples:
• Your browser may use port 52000 to connect to a web server
Socket Address
Socket Address (IP + Port)
• A socket address = IP address + Port number
• Example: [Link] : 69
• IP Address → Identifies the host/device
• Port Number → Identifies the application/process
• Enables process-to-process communication
• Ensures data reaches the correct application on a host
Position of UDP, TCP, and SCTP in TCP/IP suite
TCP/IP Protocol Stack
Application Layer (Top Layer)
o Provides services directly to user applications
o Protocols:
 SMTP → Email sending
 FTP → File transfer
 TFTP → Simple file transfer
 DNS → Domain name to IP conversion
 SNMP → Network management
 BOOTP → Device booting over network
o Functions:
 Interface between user and network
 Defines data format and communication rules
o Examples: Browser, email client
TCP/IP Protocol Stack
Transport Layer
o Responsible for process-to-process communication
o Protocols:
 TCP (Transmission Control Protocol)
• Reliable (acknowledgment, retransmission)
• Connection-oriented
 UDP (User Datagram Protocol)
• Fast but unreliable
• Connectionless
 SCTP (Stream Control Transmission Protocol)
• Combines features of TCP and UDP
TCP/IP Protocol Stack
Network Layer
o Responsible for host-to-host communication
o Protocols:
 IP (Internet Protocol) → Core protocol for routing
 ICMP → Error reporting (e.g., ping)
 IGMP → Multicast communication
 ARP → Maps IP → MAC address
 RARP → Maps MAC → IP (older method)
o Functions:
 Logical addressing (IP address)
 Routing (path selection)
 Packet forwarding
TCP/IP Protocol Stack
Data Link Layer
o Responsible for node-to-node communication
o Functions:
 Transfers data between directly connected devices
 Converts packets into frames
 Handles:
• Error detection
• MAC addressing
TCP/IP Protocol Stack

Physical Layer (Bottom Layer)


o Responsible for actual transmission of bits
o Functions:
 Sends raw 0s and 1s over medium
 Defines:
• Voltage levels
• Cables, signals, connectors
TCP/IP Protocol Stack

Layer-by-Layer
o Application layer → creates data
o Transport layer → adds port numbers
o Network layer → adds IP address
o Data link layer → adds MAC address
o Physical layer → transmits bits
TCP
• TCP is a connection-oriented protocol;
• It creates a virtual connection between two TCPs to
send data.
• In addition, TCP uses flow and error control
mechanisms at the transport level.
TCP

Well-known Ports
used by TCP protocol
TCP
Stream delivery
TCP
1. Sending Process
o The application (like a file transfer program or browser)
generates data.
o This data is passed to TCP.
o TCP does not care about message boundaries — it treats
everything as a sequence of bytes.

2. TCP at Sender Side


o Breaks data into smaller chunks (segments).
o Adds headers (sequence number, error check, etc.).
o Sends them across the network.
TCP
3. Stream of Bytes (Network)
o Data travels as a continuous flow.
o Even though internally it is segmented, TCP ensures:
 Correct order
 No duplication
 No data loss (retransmission if needed)
TCP
4. TCP at Receiver Side
o Receives segments.
o Reorders them if needed.
o Removes duplicates.
o Reconstructs the original byte stream.
o Gets data as a continuous stream of bytes.
o It is the application's responsibility to interpret where
messages start/end.
TCP
TCP segment format
TCP segment format

Note

The value in the sequence number field


of a segment defines the
number of the first data byte
contained in that segment.
TCP segment format

Note

The value of the acknowledgment field


in a segment defines
the number of the next byte a party
expects to receive.
The acknowledgment number is
cumulative.
TCP segment format
Connection establishment using three-way handshaking

SYN (Client → Server)


• I want to start a connection
• My sequence number starts
at 8000

SYN + ACK (Server → Client)


• Connection accepted
• I received your request
• My sequence starts at 15000

ACK (Client → Server)


• I received your response
• Connection established
TCP segment format

Note

A SYN segment cannot carry data, but it


consumes one sequence number.

A SYN + ACK segment cannot


carry data, but does consume one
sequence number.
TCP segment format

Note

An ACK segment, if carrying no data,


consumes no sequence number.
Connection termination using three-way handshaking
TCP/IP Socket Programming In Java
Server Side Steps

Step - 01
o Create Server Socket
 Use ServerSocket with a port number
 Example:
• ServerSocket server = new ServerSocket(5000);
TCP/IP Socket Programming In Java
Server Side Steps

Step - 02
o Wait for Client Connection
 Use accept() method (blocking call)
 Returns a Socket object
TCP/IP Socket Programming In Java
Server Side Steps

Step - 03
o Establish Connection
 Communication channel is created between client
and server
TCP/IP Socket Programming In Java
Server Side Steps
Step - 04
o Create Input/Output Streams - Read and Write Data
 Common Input Streams  Common Output Streams
 BufferedReader  BufferedWriter
 DataInputStream  DataOutputStream
 ObjectInputStream  ObjectOutputStream
 Scanner  PrintWriter
TCP/IP Socket Programming In Java
Server Side Steps

Step - 05
o Close Connection
 Close streams and socket using close().
TCP/IP Socket Programming In Java
Client Side Steps
Step - 01
o Create Socket
 Connect to server using IP + port
 Example:
 Socket socket = new Socket("localhost", 5000);
Step - 02
o Establish Connection
 TCP connection is created automatically
TCP/IP Socket Programming In Java
Client Side Steps

Step - 03
o Create Input/Output Streams - Read and Write Data
 Common Input Streams  Common Output Streams
 BufferedReader  BufferedWriter
 DataInputStream  DataOutputStream
 ObjectInputStream  ObjectOutputStream
 Scanner  PrintWriter
TCP/IP Socket Programming In Java
Client Side Steps

Step - 04
o Close Connection
 Close streams and socket using close().
Problems on TCP/IP - Flow Control
Problem - 01
Two hosts P1 and P2 want to establish a TCP connection.
• Initial Sequence Number of P1 = 3500
• Initial Sequence Number of P2 = 7200
• Receiver window size:
• P1 = 2000 bytes
• P2 = 3000 bytes
Show the 3-way handshake with full TCP header details including:
• Sequence number
• Acknowledgment number
• Window size
Problems on TCP/IP - Flow Control
Problem – 01 - Solution
Step 1: SYN (P1 → P2)
• Flag: SYN
• Seq = 3500
• Ack = —
• Window = 2000
• P1 initiates connection and shares its window size
Problems on TCP/IP - Flow Control
Problem – 01 - Solution
Step 2: SYN + ACK (P2 → P1)
• Flag: SYN, ACK
• Seq = 7200
• Ack = 3501 (3500 + 1)
• Window = 3000
• P2 acknowledges P1 and shares its window
Problems on TCP/IP - Flow Control
Problem – 01 - Solution

Step 3: ACK (P1 → P2)


• Flag: ACK
• Seq = 3501
• Ack = 7201 (7200 + 1)
• Window = 2000
• Final acknowledgment — connection established
Problems on TCP/IP - Flow Control
Problem – 01 - Solution

P1 → P2 : SYN, Seq=3500, Win=2000

P2 → P1 : SYN+ACK, Seq=7200, Ack=3501, Win=3000

P1 → P2 : ACK, Seq=3501, Ack=7201, Win=2000


Problems on TCP/IP - Flow Control
Problem - 02
Two hosts P1 and P2 establish a TCP connection and transfer data.
Given:
• Initial Sequence Number:
• P1 = 1000
• P2 = 4000
• Receiver window size:
• P1 = 2000 bytes
• P2 = 1500 bytes
• MSS = 1000 bytes
• P1 wants to send 2500 bytes of data to P2
Problems on TCP/IP - Flow Control
Problem - 02
Tasks
• Show 3-way handshake
• Show data transfer with TCP headers
• Sequence number
• Acknowledgment number
• Window size
• Data size
Problems on TCP/IP - Flow Control
Problem – 02 - Solution

Handshake:
P1→P2: SYN, Seq=1000, Win=2000
P2→P1: SYN+ACK, Seq=4000, Ack=1001, Win=1500
P1→P2: ACK, Seq=1001, Ack=4001, Win=2000

Data:
P1→P2: Seq=1001, Ack=4001, Win=2000, Data=1000
P1→P2: Seq=2001, Ack=4001, Win=2000, Data=500
P2→P1: Seq=4001, Ack=2501, Win=1500

P1→P2: Seq=2501, Ack=4001, Win=2000, Data=1000


P2→P1: Seq=4001, Ack=3501, Win=1500
Problems on TCP/IP - Flow Control
Problem - 03
Two hosts P1 and P2 have completed data transfer and want to
terminate the TCP connection.
Given:
• Last byte sent by P1 = 5000
• Last byte sent by P2 = 8000
• Sequence numbers start from next expected byte
• Window Size:
• P1 = 2000 bytes
• P2 = 1500 bytes
• Show the TCP connection termination (4-way handshake)
with: Sequence number, Acknowledgment number and
Window size
Problems on TCP/IP - Flow Control
Problem – 03 - Solution

P1→P2: FIN, Seq=5001, Ack=8001, Win=2000

P2→P1: ACK, Seq=8001, Ack=5002, Win=1500

P2→P1: FIN, Seq=8001, Ack=5002, Win=1500

P1→P2: ACK, Seq=5002, Ack=8002, Win=2000


Thank You

You might also like