0% found this document useful (0 votes)
5 views56 pages

5COSC022W Lecture Week02 Networking Socket Programming (3)

The document discusses the fundamentals of networking and socket programming, focusing on the structure and protocols of the Internet, including the OSI and TCP/IP models. It explains IP addressing, subnetting, DHCP, NAT, and ARP, detailing how devices communicate within networks and how addresses are assigned and resolved. Key concepts such as network layers, address classes, and the importance of protocols in data transmission are also covered.

Uploaded by

ayash.20221941
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)
5 views56 pages

5COSC022W Lecture Week02 Networking Socket Programming (3)

The document discusses the fundamentals of networking and socket programming, focusing on the structure and protocols of the Internet, including the OSI and TCP/IP models. It explains IP addressing, subnetting, DHCP, NAT, and ARP, detailing how devices communicate within networks and how addresses are assigned and resolved. Key concepts such as network layers, address classes, and the importance of protocols in data transmission are also covered.

Uploaded by

ayash.20221941
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
You are on page 1/ 56

Lecture Week 2:

Networking and Socket


Programming
Dr. Hamed Hamzeh
30/01/2025
The Internet: a “nuts and bolts” view
Billions of connected mobile network
computing devices: national or global ISP
▪ hosts = end systems
▪ running network apps at
Internet’s “edge”

Packet switches: forward


local or
packets (chunks of data) Internet
regional ISP
▪ routers, switches
home network content
Communication links provider
network datacenter
▪ fiber, copper, radio, satellite network

▪ transmission rate: bandwidth


Networks enterprise
▪ collection of devices, routers, network
links: managed by an organization

2
The Internet: a “nuts and bolts” view
mobile network
4G
• Internet: “network of networks” national or global ISP

• Interconnected ISPs
▪ protocols are everywhere Skype
IP
Streaming
video
• control sending, receiving of
messages local or
regional ISP
• e.g., HTTP (Web), streaming video,
Skype, TCP, IP, WiFi, 4G, Ethernet home network content
provider
▪ Internet standards HTTP network datacenter
network

• RFC: Request for Comments Ethernet

• IETF: Internet Engineering Task Force TCP


enterprise
network

WiFi

3
What’s a protocol?

Human protocols: Network protocols:


▪ “what’s the time?” ▪ computers (devices) rather than humans
▪ “I have a question” ▪ all communication activity in Internet
▪ introductions governed by protocols

… specific messages sent


Protocols define the format, order of
… specific actions taken
when message received, messages sent and received among
or other events network entities, and actions taken
on msg transmission, receipt

4
What’s a protocol?
A human protocol and a computer network protocol:

Hi TCP connection
request
Hi TCP connection
response
Got the
time? GET http://gaia.cs.umass.edu/kurose_ross
2:00
<file>
time

Q: other human protocols?

5
OSI Model
• OSI Model (Open Systems Interconnection Model) is a reference tool and a
reference model for data communications in networks”. Proposed by the
International Standards Organization (ISO).

6
TCP/IP model

The layers in the TCP/IP protocol suite do


not exactly match those in the OSI
model.

TCP/IP Model
OSI Model
The original TCP/IP protocol suite was
defined as having four layers: host-to-
network, internet, transport, and
application.

However, when TCP/IP is compared to


OSI, we can say that the TCP/IP protocol
suite is made of five layers: physical, data
link, network, transport, and application.

7
Physical Address
Also known as a MAC (Media Access Control) address, it is a unique identifier
assigned to network interfaces for communication within a network.

Length: MAC addresses are 48 bits long and commonly expressed as six
groups of two hexadecimal digits.

8
Port Address

01 02 03
Ports are virtual Range: Ports are Types: Well-known
endpoints for identified by ports (0-1023),
communication in a numbers from 0 to registered ports
network. 65535. (1024-49151), and
dynamic or private
ports (49152-65535).
IP Class A

Range: 1.0.0.0 to 127.0.0.0

Used for large organizations with many networks and devices

Could support up to 127 networks with 16,777,214 devices each

Used by large companies, government agencies, and universities


IIP Class BP Class B

Range: 128.0.0.0 to 191.255.0.0

Used for medium-sized organizations with a moderate number of networks and


devices

Could support up to 16,384 networks with 65,534 devices each

Used by universities, hospitals, and mid-sized businesses


IP Class C
Range: 192.0.0.0 to 223.255.255.0

Used for small organizations and home networks

Could support up to 2,097,152 networks with 254 devices each

Most commonly used class for home and small business networks
IP Class D and E

Class D (224.0.0.0 to 239.255.255.255):


Reserved for multicast groups.

Class E (240.0.0.0 to 255.255.255.255):


Reserved for experimental purposes.
IP Classes:
Network and
Host
Private IP Addresses
Reserved IP addresses for use within a private network.

10.0.0.0 to 10.255.255.255
Range: Examples include addresses 172.16.0.0 to 172.31.255.255
from the following ranges:
192.168.0.0 to 192.168.255.255

Usage: Ideal for internal network communication within an


organization or a home network.
Subnetting
Subnets
223.1.1.1

▪What’s a subnet ? 223.1.2.1

• Through subnetting, network 223.1.1.2


223.1.1.4 223.1.2.9
traffic can travel a shorter
distance without passing 223.1.3.27
through unnecessary routers to 223.1.1.3
223.1.2.2

reach its destination.


▪ IP addresses have structure: 223.1.3.1 223.1.3.2

• subnet part: devices in same subnet


have common high order bits
network consisting of 3 subnets
• host part: remaining low order bits
Subnets
subnet 223.1.1.0/24
223.1.1.1 subnet 223.1.2.0/24

Recipe for defining subnets: 223.1.2.1

▪detach each interface from its 223.1.1.2


223.1.1.4 223.1.2.9

host or router, creating


“islands” of isolated networks 223.1.1.3
223.1.3.27
223.1.2.2

▪each isolated network is


subnet
called a subnet 223.1.3.0/24 223.1.3.1 223.1.3.2

subnet mask: /24


(high-order 24 bits: subnet part of IP address)
Subnets 223.1.1.2

subnet 223.1.1/24
223.1.1.1
▪ where are the 223.1.1.4

subnets? 223.1.1.3

▪ what are the 223.1.9.2 223.1.7.0


/24 subnet subnet 223.1.9/24
subnet 223.1.7/24

addresses?
223.1.9.1 223.1.7.1
223.1.8.1 223.1.8.0

subnet 223.1.2/24 223.1.2.6 subnet 223.1.8/24 223.1.3.27


subnet 223.1.3/24
223.1.2.1 223.1.2.2 223.1.3.1 223.1.3.2
What is a Subnet Mask?

A subnet mask is a 32-bit number that


divides an IP address into network and
host portions.
• 11111111.11111111.11111111.00000000

This subnet mask is typically shown in


the equivalent, more readable form:

• 255.255.255.0
Subnetting
IP addressing: CIDR

CIDR: Classless InterDomain Routing (pronounced “cider”)


• subnet portion of address of arbitrary length
• address format: a.b.c.d/x, where x is # bits in subnet portion of
address
subnet host
part part
11001000 00010111 00010000 00000000
200.23.16.0/23
Innovate Inc. initially used a
single network: 192.168.1.0/24
(254 usable IPs).

Subnetting
Example Expanded to three
departments: Development,
Scenario: Marketing, Sales.
Innovate Inc.
Facing network congestion,
security concerns, and IP
address exhaustion.
The Solution: Subnetting
• Divide the 192.168.1.0 network into four subnets.
• Use subnet mask 255.255.255.192 (or /26 in CIDR notation).
• One subnet for each department (Development, Marketing, Sales)
and one for shared resources.
Subnet Network Address Subnet Mask Usable IP Range Purpose
192.168.1.1 -
Subnet 1 192.168.1.0 255.255.255.192 Development
192.168.1.62
192.168.1.65 -
Subnet 2 192.168.1.64 255.255.255.192 Marketing
192.168.1.126
192.168.1.129 -
Subnet 3 192.168.1.128 255.255.255.192 Sales
192.168.1.190
192.168.1.193 -
Subnet 4 192.168.1.192 255.255.255.192 Shared Resources
192.168.1.254
Subnetting Calculation
Step 1 - Binary
• Convert Subnet Mask to Binary:
• 255.255.255.192 becomes 11111111.11111111.11111111.11000000

Step 2 - Network & Host Bits:


• Binary Subnet Mask: 11111111.11111111.11111111.11000000
• Network Bits: The 1s in the subnet mask (26 bits in this case: /26).
• Host Bits: The 0s in the subnet mask (6 bits).

Step 3 - Number of Subnets:


• The number of subnets is determined by the number of bits borrowed from the host portion to create the
subnet id.
• In this case, we borrowed 2 bits (the two 1s that are in the host portion of the original subnet mask).
• The calculation is 2 raised to the number of borrowed bits: 2^2 = 4 subnets.
Subnetting Calculation
Step 4 - Hosts per Subnet:

• Formula: 2^(number of host bits) – 2


• Calculation:2^6 - 2 = 64 - 2 = 62 usable hosts per subnet.(Subtract 2 for the network address and
broadcast address).

Step 5 - Usable IP Range:

• Network Address: 192.168.1.0


• Broadcast Address:
• Convert last octet of network address to binary: 00000000
• Set host bits to 1: 00111111
• Convert back to decimal: 63
• Broadcast Address: 192.168.1.63
• Usable Range: 192.168.1.1 to 192.168.1.62
IP addresses: how to get one?
That’s actually two questions:
1. Q: How does a host get IP address within its network (host part of
address)?
2. Q: How does a network get IP address for itself (network part of
address)

How does host get IP address?


▪ hard-coded by sysadmin in config file (e.g., /etc/rc.config in UNIX)
▪ DHCP: Dynamic Host Configuration Protocol: dynamically get address
from as server
DHCP: Dynamic Host Configuration Protocol

goal: host dynamically obtains IP address from network server when it


“joins” network
▪ can renew its lease on address in use
▪ allows reuse of addresses (only hold address while connected/on)
▪ support for mobile users who join/leave network

DHCP overview:
▪ host broadcasts DHCP discover msg [optional]
▪ DHCP server responds with DHCP offer msg [optional]
▪ host requests IP address: DHCP request msg
▪ DHCP server sends address: DHCP ack msg
NAT: network address translation
NAT: all devices in local network share just one IPv4 address as
far as outside world is concerned
rest of local network (e.g., home
Internet network) 10.0.0/24

10.0.0.1
138.76.29.7 10.0.0.4

10.0.0.2

10.0.0.3

all datagrams leaving local network have datagrams with source or destination in
same source NAT IP address: 138.76.29.7, this network have 10.0.0/24 address for
but different source port numbers source, destination (as usual)
ARP: address resolution protocol
Question: how to determine interface’s MAC address, knowing its IP
address?
ARP table: each IP node (host,
ARP
router) on LAN has table
137.196.7.78
ARP
1A-2F-BB-76-09-AD • IP/MAC address mappings for
ARP some LAN nodes:
LAN < IP address; MAC address; TTL>
71-65-F7-2B-08-53
137.196.7.23
58-23-D7-FA-20-B0
137.196.7.14 • TTL (Time To Live): time after
ARP 0C-C4-11-6F-E3-98 which address mapping will be
137.196.7.88
forgotten (typically 20 min)

30
ARP protocol in action
example: A wants to send datagram to B
• B’s MAC address not in A’s ARP table, so A uses ARP to find B’s MAC address

A broadcasts ARP query, containing B's IP addr


Ethernet frame (sent to FF-FF-FF-FF-FF-FF)
1 • destination MAC address = FF-FF-FF-FF-FF-FF
• all nodes on LAN receive ARP query C Source MAC: 71-65-F7-2B-08-53
Source IP: 137.196.7.23
ARP table in A Target IP address: 137.196.7.14

IP addr MAC addr TTL
TTL
A B
1
71-65-F7-2B-08-53 58-23-D7-FA-20-B0
137.196.7.23 137.196.7.14

D
31
ARP protocol in action
Example: A wants to send datagram to B
• B’s MAC address not in A’s ARP table, so A uses ARP to find B’s MAC address

ARP message into Ethernet frame


(sent to 71-65-F7-2B-08-53)
C Target IP address: 137.196.7.14
Target MAC address:
ARP table in A 58-23-D7-FA-20-B0

IP addr MAC addr TTL
TTL
A B
2
71-65-F7-2B-08-53 58-23-D7-FA-20-B0
137.196.7.23 137.196.7.14

2 B replies to A with ARP response,


giving its MAC address
D

32
ARP protocol in action
example: A wants to send datagram to B
• B’s MAC address not in A’s ARP table, so A uses ARP to find B’s MAC address

C
ARP table in A
IP addr MAC addr TTL
TTL
137.196. 58-23-D7-FA-20-B0 500
A B
7.14

71-65-F7-2B-08-53 58-23-D7-FA-20-B0
137.196.7.23 137.196.7.14

3 A receives B’s reply, adds B entry


into its local ARP table
D

33
TCP: overview
▪ point-to-point: ▪ cumulative ACKs
• one sender, one receiver ▪ pipelining:
▪ reliable, in-order byte steam: • TCP congestion and flow control
• no “message boundaries" set window size
▪ full duplex data: ▪ connection-oriented:
• bi-directional data flow in same • handshaking (exchange of control
connection messages) initializes sender,
• MSS: maximum segment size receiver state before data exchange
▪ flow controlled:
• sender will not overwhelm receiver
Transport Layer Actions

Sender:
application ▪ is passed an application- application
app. msg
layer message
transport ▪ determines segment TThhtransport
app. msg
header fields values
network (IP) ▪ creates segment network (IP)

link ▪ passes segment to IP link

physical physical
Transport Layer Actions

Receiver:
application ▪ receives segment from IP application
▪ checks header values
app. msg
transport ▪ extracts application-layer transport
message
network (IP)
network (IP) ▪ demultiplexes message up
link to application via socket link

physical physical
Th app. msg
Agreeing to establish a connection
2-way handshake:

Q: will 2-way handshake always


Let’s talk work in network?
ESTAB
ESTAB
OK ▪ variable delays
▪ retransmitted messages (e.g.
req_conn(x)) due to message loss
▪ message reordering
choose x
req_conn(x) ▪ can’t “see” other side
ESTAB
acc_conn(x)
ESTAB

Transport Layer: 3-37


2-way handshake scenarios

choose x
req_conn(x)
ESTAB
acc_conn(x)

ESTAB
data(x+1) accept
data(x+1)
ACK(x+1)
connection
x completes

No problem!

Transport Layer: 3-38


2-way handshake scenarios

choose x
req_conn(x)
ESTAB
retransmit acc_conn(x)
req_conn(x)

ESTAB
req_conn(x)

connection
client x completes server
terminates forgets x

ESTAB
acc_conn(x)
Problem: half open
connection! (no client)
Transport Layer: 3-39
2-way handshake scenarios
choose x
req_conn(x)
ESTAB
retransmit acc_conn(x)
req_conn(x)

ESTAB
data(x+1) accept
data(x+1)
retransmit
data(x+1)
connection
x completes server
client
terminates forgets x
req_conn(x)
ESTAB
data(x+1) accept
data(x+1)
Problem: dup data
accepted!
TCP 3-way handshake
Server state
serverSocket = socket(AF_INET,SOCK_STREAM)
Client state serverSocket.bind((‘’,serverPort))
serverSocket.listen(1)
clientSocket = socket(AF_INET, SOCK_STREAM) connectionSocket, addr = serverSocket.accept()
LISTEN
clientSocket.connect((serverName,serverPort)) LISTEN
choose init seq num, x
send TCP SYN msg
SYNSENT SYNbit=1, Seq=x
choose init seq num, y
send TCP SYNACK
msg, acking SYN SYN RCVD
SYNbit=1, Seq=y
ACKbit=1; ACKnum=x+1
received SYNACK(x)
ESTAB indicates server is live;
send ACK for SYNACK;
this segment may contain ACKbit=1, ACKnum=y+1
client-to-server data
received ACK(y)
indicates client is live
ESTAB

Transport Layer: 3-41


Two principal Internet transport protocols
application
transport

▪TCP: Transmission Control Protocol network


mobile network
data link
physical
national or global ISP
• reliable, in-order delivery
• congestion control
• flow control
• connection setup local or
regional ISP

▪UDP: User Datagram Protocol home network content


provider
• unreliable, unordered delivery network datacenter
application
network
transport

• no-frills extension of “best-effort” IP network


data link
physical

enterprise
network
UDP: User Datagram Protocol
Why is there a UDP?
▪ “no frills,” “bare bones”
Internet transport protocol ▪ no connection
establishment (which can
▪ “best effort” service, UDP add RTT delay)
segments may be: ▪ simple: no connection state
• lost at sender, receiver
• delivered out-of-order to app ▪ small header size
▪ connectionless: ▪ no congestion control
• no handshaking between UDP ▪ UDP can blast away as fast as
desired!
sender, receiver
▪ can function in the face of
• each UDP segment handled congestion
independently of others
UDP: User Datagram Protocol
▪ UDP use:
▪ streaming multimedia apps (loss tolerant, rate sensitive)
▪ DNS
▪ SNMP
▪ HTTP/3
▪ if reliable transfer needed over UDP (e.g., HTTP/3):
▪ add needed reliability at application layer
▪ add congestion control at application layer
Sockets
goal: learn how to build client/server applications that
communicate using sockets
socket: door between application process and end-end-transport
protocol
application application
socket controlled by
process process app developer

transport transport
network network controlled
link by OS
link Internet
physical physical
How do
sockets
work?
Client/server socket interaction: TCP

server (running on hostid) client


create socket,
port=x, for incoming
request:
serverSocket = socket()

wait for incoming create socket,


connection request
TCP connect to hostid, port=x
connectionSocket = connection setup clientSocket = socket()
serverSocket.accept()

send request using


read request from clientSocket
connectionSocket

write reply to
connectionSocket read reply from
clientSocket
close
connectionSocket close
clientSocket
Java
Networking
Packages
Java Socket
Socket Programming

• Socket programming enables


communication between computers
over a network.
• Java provides the java.net package for
networking.
• Two types of sockets: ServerSocket
(listens for incoming connections) and
Socket (initiates a connection).
Server Side - The ServerSocket accepts incoming client
connections.
Accepting Upon accepting a connection, a new Socket is
Connections created for communication with that client.
• Input and output streams are used for reading from and
Server Side - Reading writing to the socket.
and Writing Data • BufferedReader and PrintWriter simplify reading and
writing text-based data.
• The client creates a Socket and connects it to the
server's IP address and port.
Client Side - Connecting
• Communication follows a similar pattern as on
to a Server the server side.
• Properly closing connections is essential to release
Closing resources.

Connections • The close() method is used for both ServerSocket and


Socket.
import java.io.*;
import java.net.*; Importing Packages

public class Client {


public static void main(String[] args) {
try {
Socket socket = new Socket("localhost", 12345); Create a socket to connect to the server

BufferedReader reader = new BufferedReader(new InputStreamReader(socket.getInputStream()));


PrintWriter writer = new PrintWriter(socket.getOutputStream(), true);

// Send a message to the server


writer.println("Hello, server!");

// Receive and print the response from the server


String serverResponse = reader.readLine();
System.out.println("Server says: " + serverResponse); Create input and output streams for
communication with the server
// Close resources
reader.close();
writer.close();
socket.close();

} catch (IOException e) { Close Resources


e.printStackTrace();
}
}
}
import java.io.*;
import java.net.*;
Importing Packages
public class Server {
public static void main(String[] args) {
try {

ServerSocket serverSocket = new ServerSocket(12345);


System.out.println("Server is listening on port 12345..."); Create a server socket listening on port 12345

Socket clientSocket = serverSocket.accept(); Accept Incoming client connections


System.out.println("Client connected: " + clientSocket.getInetAddress());

BufferedReader reader = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));


PrintWriter writer = new PrintWriter(clientSocket.getOutputStream(), true);

// Read data from the client and echo it back


String clientMessage;
Accept Incoming client connections
while ((clientMessage = reader.readLine()) != null) {
System.out.println("Received from client: " + clientMessage);
writer.println("Server echoes: " + clientMessage);
}

reader.close();
writer.close();
clientSocket.close();
serverSocket.close();
Close
} catch (IOException e) { resources
e.printStackTrace();
}
}
}

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy