SlideShare a Scribd company logo
+
Network Programming
+A Client-Server Transaction
■ Many network applications are based on the client-server
model:
■ A server process and one or more client processes
■ Server manages some resource
■ Server provides service by manipulating resource for clients
■ Server activated by request from client
Client
process
Server
process
1. Client sends request
3. Server sends response
4. Client
handles
response
2. Server
handles
request
Resource
Note: clients and servers are processes running on hosts
(can be the same or different hosts)
+Computer Networks
■ A network is a group of connected systems that are able to
communicate in order to exchange data.
■ There are many kinds of networks, some examples..
■ LAN (Local Area Network) spans a building or campus
■ Ethernet is most prominent example
■ WAN (Wide Area Network) spans country or world
■ Typically high-speed point-to-point telecom lines
■ An internetwork (internet) is an interconnected set of networks
■ The Global IP Internet (uppercase “I”) is the most famous
example of an internet (lowercase “i”)
+Computer Networks con’t
■ Network devices that originate, route and terminate data are
called 'nodes'.
■ Nodes or 'hosts' can be personal computers, phones, servers as
well as special networking hardware.
■ Two such devices can be said to be 'networked' together when
one device is able to exchange information with the other device.
■ A 'router' is an networking device that forwards data between
networks in an internet.
■ A 'link' is the means of connecting one location to another for
the purpose of transmitting and receiving data across networks.
■ phone lines, fiberoptic cables, bluetooth, ethernet, wireless, etc...
+Logical Structure of an internet
■ Ad hoc interconnection of networks
■ No particular topology
■ Vastly different router & link capacities
■ Send packets from source to destination by hopping through networks
■ Router forms bridge from one network to another
■ Different packets may take different routes
router
router
router
router
router
router
host
host
+The Notion of an internet Protocol
■ How is it possible to send bits across incompatible LANs and
WANs?
■ Solution: protocol software running on each host and router
■ A protocol is a set of rules that governs how hosts and routers
should cooperate when they transfer data from network to
network.
■ The rules define the syntax, semantics and synchronization of
communication and possible error recovery methods.
■ Protocols may be implemented by hardware, software, or a
combination of both.
+What Does an internet Protocol Do?
■ Provides a naming scheme
■ An internet protocol defines a uniform format for host addresses
■ Each host (and router) is assigned at least one of these internet
addresses that uniquely identifies it
■ Provides a delivery mechanism
■ An internet protocol defines a standard transfer unit (packet)
■ Packet consists of header and payload
■ Header: contains info such as packet size, source and destination
addresses
■ Payload: contains data bits sent from source host
■ Lots of other things…
■ Example, what order do the bytes go on the wire? Little-endian or Big-
endian?
+Global IP Internet (upper case)
■ Most famous example of an internet
■ Based on the TCP/IP protocol family
■ IP (Internet Protocol) :
■ Provides basic naming scheme and unreliable delivery capability
of packets (datagrams) from host-to-host
■ UDP (Unreliable Datagram Protocol)
■ Uses IP to provide unreliable datagram delivery from
process-to-process
■ TCP (Transmission Control Protocol)
■ Uses IP to provide reliable byte streams from process-to-process
■ Accessed via a mix of Unix file I/O and functions from the
sockets interface
+Organization of an Internet Application
TCP/IP
Client
Network
adapter
Global IP Internet
TCP/IP
Server
Network
adapter
Internet client host Internet server host
Sockets interface
(system calls)
Hardware interface
(interrupts)
User code
Kernel code
Hardware
and firmware
+A Programmer’s View of the Internet
■ Hosts are mapped to a set of 32-bit IP addresses
■ 128.122.49.30
■ The set of IP addresses is mapped to a set of identifiers called
Internet domain names
■ 128.122.49.30 is mapped to cs.nyu.edu
■ A process on one Internet host can communicate with a process
on another Internet host over a connection
+Domain Naming System (DNS)
■ The Internet maintains a mapping between IP addresses and
domain names in a huge worldwide distributed database called
DNS
■ Conceptually, programmers can view the DNS database as a
collection of millions of host entries.
■ Each host entry defines the mapping between a set of domain
names and IP addresses.
■ These names can be resolved on the command line using
nslookup command.
■ Many names can be mapped to the same IP or multiple IPs
mapped to same name.
■ Why? Geolocation.
+Internet Connections
■ Clients and servers communicate by sending streams of bytes over
connections. Each connection is:
■ Point-to-point: connects a pair of processes.
■ Full-duplex: data can flow in both directions at the same time,
■ Reliable: stream of bytes sent by the source is eventually received by
the destination in the same order it was sent.
■ A port is a 16-bit integer that identifies a process:
■ Ephemeral port: Assigned automatically by client kernel when client
makes a connection request. (Also used by server in some cases)
■ Well-known port: Associated with some service provided by a server
(e.g., port 80 is associated with Web servers)
■ A socket is an endpoint of a connection
■ Socket address is an IPaddress:port pair
+Well-known Ports and Service Names
■ Popular services have permanently assigned well-known ports
and corresponding well-known service names:
■ Examples
■ echo server: 7/echo
■ ssh servers: 22/ssh
■ email server: 25/smtp
■ web servers: 80/http
■ 0-1023 have special semantics
■ https://en.wikipedia.org/wiki/List_of_TCP_and_UDP_port_numbers
■ Mappings between well-known ports and service names is
contained in the file /etc/services on each Linux machine.
+Anatomy of a Connection
■ A connection is uniquely identified by the socket addresses of
its endpoints (socket pair)
■ (cliaddr:cliport, servaddr:servport)
Connection socket pair
(128.2.194.242:51213, 208.216.181.15:80)
Server
(port 80)
Client
Client socket address
128.2.194.242:51213
Server socket address
208.216.181.15:80
Client host address
128.2.194.242
Server host address
208.216.181.15
51213 is an ephemeral port
allocated by the kernel
80 is a well-known port
associated with Web servers
+Sockets
■ What is a socket?
■ To the kernel, a socket is an endpoint of communication
■ To an application, a socket is a file descriptor that lets the
application read/write from/to the network
■ All Unix I/O devices, including networks, are modeled as files
■ Clients and servers communicate with each other by reading
from and writing to socket descriptors
■ The main distinction between regular file I/O and socket I/O is
how the application “opens” the socket descriptors
Client Server
clientfd serverfd
+Socket Address Structures
■ Generic socket address:
■ For address arguments to connect, bind, and accept
■ Necessary only because C did not have generic (void*) pointers
when the sockets interface was designed
struct sockaddr {
uint16_t sa_family; /* Protocol family */
char sa_data[14]; /* Address data. */
};
sa_family
Family Specific
+Socket Address Structures con’t
■ Internet-specific socket address:
■ Must cast struct sockaddr_in* to struct sockaddr*
for functions that take socket address arguments.
0 0 0 0 0 0 0 0
sa_family
Family Specific
struct sockaddr_in {
uint16_t sin_family; /* Protocol family (always AF_INET) */
uint16_t sin_port; /* Port num in network byte order */
struct in_addr sin_addr; /* IP addr in network byte order */
unsigned char sin_zero[8]; /* Pad to sizeof(struct sockaddr) */
};
sin_port
AF_INET
sin_addr
sin_family
+
5. Drop client
4. Disconnect client
3. Exchange
data
2. Start client 1. Start server
Client /
Server
Session
Client
Server
socket
socket
bind
listen
read
write
read
write
Connection
request
read
close
close
EOF
accept
connect
Client/Server
+Sockets Interface: socket
■ Clients and servers use the socket function to create a socket
descriptor:
■ Example:
int socket(int domain, int type, int protocol)
int clientfd = Socket(AF_INET, SOCK_STREAM, 0);
Indicates that we are
using 32-bit IPV4
addresses
Indicates that the socket
will be the end point of a
TCP connection
+Sockets Interface: bind
■ A server uses bind to ask the kernel to associate the server’s
socket address with a socket descriptor:
■ The process can read bytes that arrive on the connection whose
endpoint is addr by reading from descriptor sockfd.
■ Similarly, writes to sockfd are transferred along connection
whose endpoint is addr.
int bind(int sockfd, sockaddr* addr, socklen_t addrlen);
+Sockets Interface: listen
■ By default, kernel assumes that descriptor from socket
function is an active socket that will be on the client end of a
connection.
■ A server calls listen to tell the kernel that a descriptor will
be used by a server rather than a client:
■ Converts sockfd from an active socket to a listening socket
that can accept connection requests from clients.
■ backlog is a hint about the number of outstanding
connection requests that the kernel should queue up before
starting to refuse requests.
int listen(int sockfd, int backlog);
+Sockets Interface: accept
■ Servers wait for connection requests from clients by calling
accept:
■ Waits for connection request to arrive on the connection bound
to listenfd, then fills in client’s socket address in addr and
size of the socket address in addrlen.
■ Returns a connected descriptor that can be used to
communicate with the client via Unix I/O routines.
int accept(int listenfd, sockaddr* addr, int* addrlen);
+Connected vs. Listening Descriptors
■ Listening descriptor
■ End point for client connection requests
■ Created once and exists for lifetime of the server
■ Connected descriptor
■ End point of the connection between client and server
■ A new descriptor is created each time the server accepts a connection
request from a client
■ Exists only as long as it takes to service client
■ Why the distinction?
■ Allows for concurrent servers that can communicate over many client
connections simultaneously
■ E.g., Each time we receive a new request, we fork a child to handle
the request
+Sockets Interface: connect
■ A client establishes a connection with a server by calling
connect:
■ Attempts to establish a connection with server at socket
address addr
■ If successful, then clientfd is now ready for reading and
writing.
■ addrlen is sizeof(sockaddr_in)
int connect(int clientfd, sockaddr* addr, socklen_t addrlen);
+accept & connect Illustrated
listenfd(3)
Client
1. Server blocks in accept,
waiting for connection
request on listening
descriptor listenfd
clientfd
Server
listenfd(3)
Client
clientfd
Server
2. Client makes connection
request by calling and blocking in
connect
Connection
request
listenfd(3)
Client
clientfd
Server
3. Server returns connfd from
accept. Client returns from
connect. Connection is now
established between clientfd and
connfd
connfd(4)
+Socket Client & Server
■ Lets take a look at some code…
■ See lecture24/client.c and lecture24/server.c

More Related Content

Similar to + Network Programming.pdf

lecture-2-tcp-ip.ppt
lecture-2-tcp-ip.pptlecture-2-tcp-ip.ppt
lecture-2-tcp-ip.ppt
KrishnaGupta717939
 
Networking.pptx
Networking.pptxNetworking.pptx
Networking.pptx
Esubesisay
 
Socket programming
Socket programmingSocket programming
Socket programmingDivya Sharma
 
Application Layer and Socket Programming
Application Layer and Socket ProgrammingApplication Layer and Socket Programming
Application Layer and Socket Programmingelliando dias
 
LEC_10_Week_10_Server_Configuration_in_Linux.pdf
LEC_10_Week_10_Server_Configuration_in_Linux.pdfLEC_10_Week_10_Server_Configuration_in_Linux.pdf
LEC_10_Week_10_Server_Configuration_in_Linux.pdf
MahtabAhmedQureshi
 
Network Programming-Python-13-8-2023.pptx
Network Programming-Python-13-8-2023.pptxNetwork Programming-Python-13-8-2023.pptx
Network Programming-Python-13-8-2023.pptx
ssuser23035c
 
Lan chat system
Lan chat systemLan chat system
Lan chat system
Wipro
 
Networking in python by Rj
Networking in python by RjNetworking in python by Rj
Linux Systems Prograramming: Unix Domain, Internet Domain (TCP, UDP) Socket P...
Linux Systems Prograramming: Unix Domain, Internet Domain (TCP, UDP) Socket P...Linux Systems Prograramming: Unix Domain, Internet Domain (TCP, UDP) Socket P...
Linux Systems Prograramming: Unix Domain, Internet Domain (TCP, UDP) Socket P...
RashidFaridChishti
 
SOHO Network Setup Tutorial
SOHO Network Setup Tutorial SOHO Network Setup Tutorial
SOHO Network Setup Tutorial
junaidahmedsaba
 
Os 2
Os 2Os 2
Ports & sockets
Ports  & sockets Ports  & sockets
Ports & sockets
myrajendra
 
Socket programming using C
Socket programming using CSocket programming using C
Socket programming using C
Ajit Nayak
 
Networking basics PPT
Networking basics PPTNetworking basics PPT
Networking basics PPT
Ehsan Ullah Kakar
 
Lecture 2 -_understanding_networks_2013
Lecture 2 -_understanding_networks_2013Lecture 2 -_understanding_networks_2013
Lecture 2 -_understanding_networks_2013Travis Leong Ping
 
Computer Networks basics
Computer Networks basicsComputer Networks basics
Computer Networks basics
BiplabaSamantaray
 
Лекц 7
Лекц 7Лекц 7
Лекц 7Muuluu
 
Networking essentials lect1
Networking essentials lect1Networking essentials lect1
Networking essentials lect1
Roman Brovko
 

Similar to + Network Programming.pdf (20)

Lecture set 7
Lecture set 7Lecture set 7
Lecture set 7
 
lecture-2-tcp-ip.ppt
lecture-2-tcp-ip.pptlecture-2-tcp-ip.ppt
lecture-2-tcp-ip.ppt
 
Networking.pptx
Networking.pptxNetworking.pptx
Networking.pptx
 
Socket programming
Socket programmingSocket programming
Socket programming
 
Application Layer and Socket Programming
Application Layer and Socket ProgrammingApplication Layer and Socket Programming
Application Layer and Socket Programming
 
Md13 networking
Md13 networkingMd13 networking
Md13 networking
 
LEC_10_Week_10_Server_Configuration_in_Linux.pdf
LEC_10_Week_10_Server_Configuration_in_Linux.pdfLEC_10_Week_10_Server_Configuration_in_Linux.pdf
LEC_10_Week_10_Server_Configuration_in_Linux.pdf
 
Network Programming-Python-13-8-2023.pptx
Network Programming-Python-13-8-2023.pptxNetwork Programming-Python-13-8-2023.pptx
Network Programming-Python-13-8-2023.pptx
 
Lan chat system
Lan chat systemLan chat system
Lan chat system
 
Networking in python by Rj
Networking in python by RjNetworking in python by Rj
Networking in python by Rj
 
Linux Systems Prograramming: Unix Domain, Internet Domain (TCP, UDP) Socket P...
Linux Systems Prograramming: Unix Domain, Internet Domain (TCP, UDP) Socket P...Linux Systems Prograramming: Unix Domain, Internet Domain (TCP, UDP) Socket P...
Linux Systems Prograramming: Unix Domain, Internet Domain (TCP, UDP) Socket P...
 
SOHO Network Setup Tutorial
SOHO Network Setup Tutorial SOHO Network Setup Tutorial
SOHO Network Setup Tutorial
 
Os 2
Os 2Os 2
Os 2
 
Ports & sockets
Ports  & sockets Ports  & sockets
Ports & sockets
 
Socket programming using C
Socket programming using CSocket programming using C
Socket programming using C
 
Networking basics PPT
Networking basics PPTNetworking basics PPT
Networking basics PPT
 
Lecture 2 -_understanding_networks_2013
Lecture 2 -_understanding_networks_2013Lecture 2 -_understanding_networks_2013
Lecture 2 -_understanding_networks_2013
 
Computer Networks basics
Computer Networks basicsComputer Networks basics
Computer Networks basics
 
Лекц 7
Лекц 7Лекц 7
Лекц 7
 
Networking essentials lect1
Networking essentials lect1Networking essentials lect1
Networking essentials lect1
 

Recently uploaded

A Strategic Approach: GenAI in Education
A Strategic Approach: GenAI in EducationA Strategic Approach: GenAI in Education
A Strategic Approach: GenAI in Education
Peter Windle
 
Overview on Edible Vaccine: Pros & Cons with Mechanism
Overview on Edible Vaccine: Pros & Cons with MechanismOverview on Edible Vaccine: Pros & Cons with Mechanism
Overview on Edible Vaccine: Pros & Cons with Mechanism
DeeptiGupta154
 
The approach at University of Liverpool.pptx
The approach at University of Liverpool.pptxThe approach at University of Liverpool.pptx
The approach at University of Liverpool.pptx
Jisc
 
Acetabularia Information For Class 9 .docx
Acetabularia Information For Class 9  .docxAcetabularia Information For Class 9  .docx
Acetabularia Information For Class 9 .docx
vaibhavrinwa19
 
Natural birth techniques - Mrs.Akanksha Trivedi Rama University
Natural birth techniques - Mrs.Akanksha Trivedi Rama UniversityNatural birth techniques - Mrs.Akanksha Trivedi Rama University
Natural birth techniques - Mrs.Akanksha Trivedi Rama University
Akanksha trivedi rama nursing college kanpur.
 
South African Journal of Science: Writing with integrity workshop (2024)
South African Journal of Science: Writing with integrity workshop (2024)South African Journal of Science: Writing with integrity workshop (2024)
South African Journal of Science: Writing with integrity workshop (2024)
Academy of Science of South Africa
 
Lapbook sobre os Regimes Totalitários.pdf
Lapbook sobre os Regimes Totalitários.pdfLapbook sobre os Regimes Totalitários.pdf
Lapbook sobre os Regimes Totalitários.pdf
Jean Carlos Nunes Paixão
 
Chapter 4 - Islamic Financial Institutions in Malaysia.pptx
Chapter 4 - Islamic Financial Institutions in Malaysia.pptxChapter 4 - Islamic Financial Institutions in Malaysia.pptx
Chapter 4 - Islamic Financial Institutions in Malaysia.pptx
Mohd Adib Abd Muin, Senior Lecturer at Universiti Utara Malaysia
 
S1-Introduction-Biopesticides in ICM.pptx
S1-Introduction-Biopesticides in ICM.pptxS1-Introduction-Biopesticides in ICM.pptx
S1-Introduction-Biopesticides in ICM.pptx
tarandeep35
 
Digital Artifact 2 - Investigating Pavilion Designs
Digital Artifact 2 - Investigating Pavilion DesignsDigital Artifact 2 - Investigating Pavilion Designs
Digital Artifact 2 - Investigating Pavilion Designs
chanes7
 
Francesca Gottschalk - How can education support child empowerment.pptx
Francesca Gottschalk - How can education support child empowerment.pptxFrancesca Gottschalk - How can education support child empowerment.pptx
Francesca Gottschalk - How can education support child empowerment.pptx
EduSkills OECD
 
Supporting (UKRI) OA monographs at Salford.pptx
Supporting (UKRI) OA monographs at Salford.pptxSupporting (UKRI) OA monographs at Salford.pptx
Supporting (UKRI) OA monographs at Salford.pptx
Jisc
 
Introduction to AI for Nonprofits with Tapp Network
Introduction to AI for Nonprofits with Tapp NetworkIntroduction to AI for Nonprofits with Tapp Network
Introduction to AI for Nonprofits with Tapp Network
TechSoup
 
How libraries can support authors with open access requirements for UKRI fund...
How libraries can support authors with open access requirements for UKRI fund...How libraries can support authors with open access requirements for UKRI fund...
How libraries can support authors with open access requirements for UKRI fund...
Jisc
 
The Accursed House by Émile Gaboriau.pptx
The Accursed House by Émile Gaboriau.pptxThe Accursed House by Émile Gaboriau.pptx
The Accursed House by Émile Gaboriau.pptx
DhatriParmar
 
TESDA TM1 REVIEWER FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
TESDA TM1 REVIEWER  FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...TESDA TM1 REVIEWER  FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
TESDA TM1 REVIEWER FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
EugeneSaldivar
 
June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...
June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...
June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...
Levi Shapiro
 
1.4 modern child centered education - mahatma gandhi-2.pptx
1.4 modern child centered education - mahatma gandhi-2.pptx1.4 modern child centered education - mahatma gandhi-2.pptx
1.4 modern child centered education - mahatma gandhi-2.pptx
JosvitaDsouza2
 
Digital Artifact 1 - 10VCD Environments Unit
Digital Artifact 1 - 10VCD Environments UnitDigital Artifact 1 - 10VCD Environments Unit
Digital Artifact 1 - 10VCD Environments Unit
chanes7
 
Chapter 3 - Islamic Banking Products and Services.pptx
Chapter 3 - Islamic Banking Products and Services.pptxChapter 3 - Islamic Banking Products and Services.pptx
Chapter 3 - Islamic Banking Products and Services.pptx
Mohd Adib Abd Muin, Senior Lecturer at Universiti Utara Malaysia
 

Recently uploaded (20)

A Strategic Approach: GenAI in Education
A Strategic Approach: GenAI in EducationA Strategic Approach: GenAI in Education
A Strategic Approach: GenAI in Education
 
Overview on Edible Vaccine: Pros & Cons with Mechanism
Overview on Edible Vaccine: Pros & Cons with MechanismOverview on Edible Vaccine: Pros & Cons with Mechanism
Overview on Edible Vaccine: Pros & Cons with Mechanism
 
The approach at University of Liverpool.pptx
The approach at University of Liverpool.pptxThe approach at University of Liverpool.pptx
The approach at University of Liverpool.pptx
 
Acetabularia Information For Class 9 .docx
Acetabularia Information For Class 9  .docxAcetabularia Information For Class 9  .docx
Acetabularia Information For Class 9 .docx
 
Natural birth techniques - Mrs.Akanksha Trivedi Rama University
Natural birth techniques - Mrs.Akanksha Trivedi Rama UniversityNatural birth techniques - Mrs.Akanksha Trivedi Rama University
Natural birth techniques - Mrs.Akanksha Trivedi Rama University
 
South African Journal of Science: Writing with integrity workshop (2024)
South African Journal of Science: Writing with integrity workshop (2024)South African Journal of Science: Writing with integrity workshop (2024)
South African Journal of Science: Writing with integrity workshop (2024)
 
Lapbook sobre os Regimes Totalitários.pdf
Lapbook sobre os Regimes Totalitários.pdfLapbook sobre os Regimes Totalitários.pdf
Lapbook sobre os Regimes Totalitários.pdf
 
Chapter 4 - Islamic Financial Institutions in Malaysia.pptx
Chapter 4 - Islamic Financial Institutions in Malaysia.pptxChapter 4 - Islamic Financial Institutions in Malaysia.pptx
Chapter 4 - Islamic Financial Institutions in Malaysia.pptx
 
S1-Introduction-Biopesticides in ICM.pptx
S1-Introduction-Biopesticides in ICM.pptxS1-Introduction-Biopesticides in ICM.pptx
S1-Introduction-Biopesticides in ICM.pptx
 
Digital Artifact 2 - Investigating Pavilion Designs
Digital Artifact 2 - Investigating Pavilion DesignsDigital Artifact 2 - Investigating Pavilion Designs
Digital Artifact 2 - Investigating Pavilion Designs
 
Francesca Gottschalk - How can education support child empowerment.pptx
Francesca Gottschalk - How can education support child empowerment.pptxFrancesca Gottschalk - How can education support child empowerment.pptx
Francesca Gottschalk - How can education support child empowerment.pptx
 
Supporting (UKRI) OA monographs at Salford.pptx
Supporting (UKRI) OA monographs at Salford.pptxSupporting (UKRI) OA monographs at Salford.pptx
Supporting (UKRI) OA monographs at Salford.pptx
 
Introduction to AI for Nonprofits with Tapp Network
Introduction to AI for Nonprofits with Tapp NetworkIntroduction to AI for Nonprofits with Tapp Network
Introduction to AI for Nonprofits with Tapp Network
 
How libraries can support authors with open access requirements for UKRI fund...
How libraries can support authors with open access requirements for UKRI fund...How libraries can support authors with open access requirements for UKRI fund...
How libraries can support authors with open access requirements for UKRI fund...
 
The Accursed House by Émile Gaboriau.pptx
The Accursed House by Émile Gaboriau.pptxThe Accursed House by Émile Gaboriau.pptx
The Accursed House by Émile Gaboriau.pptx
 
TESDA TM1 REVIEWER FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
TESDA TM1 REVIEWER  FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...TESDA TM1 REVIEWER  FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
TESDA TM1 REVIEWER FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
 
June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...
June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...
June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...
 
1.4 modern child centered education - mahatma gandhi-2.pptx
1.4 modern child centered education - mahatma gandhi-2.pptx1.4 modern child centered education - mahatma gandhi-2.pptx
1.4 modern child centered education - mahatma gandhi-2.pptx
 
Digital Artifact 1 - 10VCD Environments Unit
Digital Artifact 1 - 10VCD Environments UnitDigital Artifact 1 - 10VCD Environments Unit
Digital Artifact 1 - 10VCD Environments Unit
 
Chapter 3 - Islamic Banking Products and Services.pptx
Chapter 3 - Islamic Banking Products and Services.pptxChapter 3 - Islamic Banking Products and Services.pptx
Chapter 3 - Islamic Banking Products and Services.pptx
 

+ Network Programming.pdf

  • 2. +A Client-Server Transaction ■ Many network applications are based on the client-server model: ■ A server process and one or more client processes ■ Server manages some resource ■ Server provides service by manipulating resource for clients ■ Server activated by request from client Client process Server process 1. Client sends request 3. Server sends response 4. Client handles response 2. Server handles request Resource Note: clients and servers are processes running on hosts (can be the same or different hosts)
  • 3. +Computer Networks ■ A network is a group of connected systems that are able to communicate in order to exchange data. ■ There are many kinds of networks, some examples.. ■ LAN (Local Area Network) spans a building or campus ■ Ethernet is most prominent example ■ WAN (Wide Area Network) spans country or world ■ Typically high-speed point-to-point telecom lines ■ An internetwork (internet) is an interconnected set of networks ■ The Global IP Internet (uppercase “I”) is the most famous example of an internet (lowercase “i”)
  • 4. +Computer Networks con’t ■ Network devices that originate, route and terminate data are called 'nodes'. ■ Nodes or 'hosts' can be personal computers, phones, servers as well as special networking hardware. ■ Two such devices can be said to be 'networked' together when one device is able to exchange information with the other device. ■ A 'router' is an networking device that forwards data between networks in an internet. ■ A 'link' is the means of connecting one location to another for the purpose of transmitting and receiving data across networks. ■ phone lines, fiberoptic cables, bluetooth, ethernet, wireless, etc...
  • 5. +Logical Structure of an internet ■ Ad hoc interconnection of networks ■ No particular topology ■ Vastly different router & link capacities ■ Send packets from source to destination by hopping through networks ■ Router forms bridge from one network to another ■ Different packets may take different routes router router router router router router host host
  • 6. +The Notion of an internet Protocol ■ How is it possible to send bits across incompatible LANs and WANs? ■ Solution: protocol software running on each host and router ■ A protocol is a set of rules that governs how hosts and routers should cooperate when they transfer data from network to network. ■ The rules define the syntax, semantics and synchronization of communication and possible error recovery methods. ■ Protocols may be implemented by hardware, software, or a combination of both.
  • 7. +What Does an internet Protocol Do? ■ Provides a naming scheme ■ An internet protocol defines a uniform format for host addresses ■ Each host (and router) is assigned at least one of these internet addresses that uniquely identifies it ■ Provides a delivery mechanism ■ An internet protocol defines a standard transfer unit (packet) ■ Packet consists of header and payload ■ Header: contains info such as packet size, source and destination addresses ■ Payload: contains data bits sent from source host ■ Lots of other things… ■ Example, what order do the bytes go on the wire? Little-endian or Big- endian?
  • 8. +Global IP Internet (upper case) ■ Most famous example of an internet ■ Based on the TCP/IP protocol family ■ IP (Internet Protocol) : ■ Provides basic naming scheme and unreliable delivery capability of packets (datagrams) from host-to-host ■ UDP (Unreliable Datagram Protocol) ■ Uses IP to provide unreliable datagram delivery from process-to-process ■ TCP (Transmission Control Protocol) ■ Uses IP to provide reliable byte streams from process-to-process ■ Accessed via a mix of Unix file I/O and functions from the sockets interface
  • 9. +Organization of an Internet Application TCP/IP Client Network adapter Global IP Internet TCP/IP Server Network adapter Internet client host Internet server host Sockets interface (system calls) Hardware interface (interrupts) User code Kernel code Hardware and firmware
  • 10. +A Programmer’s View of the Internet ■ Hosts are mapped to a set of 32-bit IP addresses ■ 128.122.49.30 ■ The set of IP addresses is mapped to a set of identifiers called Internet domain names ■ 128.122.49.30 is mapped to cs.nyu.edu ■ A process on one Internet host can communicate with a process on another Internet host over a connection
  • 11. +Domain Naming System (DNS) ■ The Internet maintains a mapping between IP addresses and domain names in a huge worldwide distributed database called DNS ■ Conceptually, programmers can view the DNS database as a collection of millions of host entries. ■ Each host entry defines the mapping between a set of domain names and IP addresses. ■ These names can be resolved on the command line using nslookup command. ■ Many names can be mapped to the same IP or multiple IPs mapped to same name. ■ Why? Geolocation.
  • 12. +Internet Connections ■ Clients and servers communicate by sending streams of bytes over connections. Each connection is: ■ Point-to-point: connects a pair of processes. ■ Full-duplex: data can flow in both directions at the same time, ■ Reliable: stream of bytes sent by the source is eventually received by the destination in the same order it was sent. ■ A port is a 16-bit integer that identifies a process: ■ Ephemeral port: Assigned automatically by client kernel when client makes a connection request. (Also used by server in some cases) ■ Well-known port: Associated with some service provided by a server (e.g., port 80 is associated with Web servers) ■ A socket is an endpoint of a connection ■ Socket address is an IPaddress:port pair
  • 13. +Well-known Ports and Service Names ■ Popular services have permanently assigned well-known ports and corresponding well-known service names: ■ Examples ■ echo server: 7/echo ■ ssh servers: 22/ssh ■ email server: 25/smtp ■ web servers: 80/http ■ 0-1023 have special semantics ■ https://en.wikipedia.org/wiki/List_of_TCP_and_UDP_port_numbers ■ Mappings between well-known ports and service names is contained in the file /etc/services on each Linux machine.
  • 14. +Anatomy of a Connection ■ A connection is uniquely identified by the socket addresses of its endpoints (socket pair) ■ (cliaddr:cliport, servaddr:servport) Connection socket pair (128.2.194.242:51213, 208.216.181.15:80) Server (port 80) Client Client socket address 128.2.194.242:51213 Server socket address 208.216.181.15:80 Client host address 128.2.194.242 Server host address 208.216.181.15 51213 is an ephemeral port allocated by the kernel 80 is a well-known port associated with Web servers
  • 15. +Sockets ■ What is a socket? ■ To the kernel, a socket is an endpoint of communication ■ To an application, a socket is a file descriptor that lets the application read/write from/to the network ■ All Unix I/O devices, including networks, are modeled as files ■ Clients and servers communicate with each other by reading from and writing to socket descriptors ■ The main distinction between regular file I/O and socket I/O is how the application “opens” the socket descriptors Client Server clientfd serverfd
  • 16. +Socket Address Structures ■ Generic socket address: ■ For address arguments to connect, bind, and accept ■ Necessary only because C did not have generic (void*) pointers when the sockets interface was designed struct sockaddr { uint16_t sa_family; /* Protocol family */ char sa_data[14]; /* Address data. */ }; sa_family Family Specific
  • 17. +Socket Address Structures con’t ■ Internet-specific socket address: ■ Must cast struct sockaddr_in* to struct sockaddr* for functions that take socket address arguments. 0 0 0 0 0 0 0 0 sa_family Family Specific struct sockaddr_in { uint16_t sin_family; /* Protocol family (always AF_INET) */ uint16_t sin_port; /* Port num in network byte order */ struct in_addr sin_addr; /* IP addr in network byte order */ unsigned char sin_zero[8]; /* Pad to sizeof(struct sockaddr) */ }; sin_port AF_INET sin_addr sin_family
  • 18. + 5. Drop client 4. Disconnect client 3. Exchange data 2. Start client 1. Start server Client / Server Session Client Server socket socket bind listen read write read write Connection request read close close EOF accept connect Client/Server
  • 19. +Sockets Interface: socket ■ Clients and servers use the socket function to create a socket descriptor: ■ Example: int socket(int domain, int type, int protocol) int clientfd = Socket(AF_INET, SOCK_STREAM, 0); Indicates that we are using 32-bit IPV4 addresses Indicates that the socket will be the end point of a TCP connection
  • 20. +Sockets Interface: bind ■ A server uses bind to ask the kernel to associate the server’s socket address with a socket descriptor: ■ The process can read bytes that arrive on the connection whose endpoint is addr by reading from descriptor sockfd. ■ Similarly, writes to sockfd are transferred along connection whose endpoint is addr. int bind(int sockfd, sockaddr* addr, socklen_t addrlen);
  • 21. +Sockets Interface: listen ■ By default, kernel assumes that descriptor from socket function is an active socket that will be on the client end of a connection. ■ A server calls listen to tell the kernel that a descriptor will be used by a server rather than a client: ■ Converts sockfd from an active socket to a listening socket that can accept connection requests from clients. ■ backlog is a hint about the number of outstanding connection requests that the kernel should queue up before starting to refuse requests. int listen(int sockfd, int backlog);
  • 22. +Sockets Interface: accept ■ Servers wait for connection requests from clients by calling accept: ■ Waits for connection request to arrive on the connection bound to listenfd, then fills in client’s socket address in addr and size of the socket address in addrlen. ■ Returns a connected descriptor that can be used to communicate with the client via Unix I/O routines. int accept(int listenfd, sockaddr* addr, int* addrlen);
  • 23. +Connected vs. Listening Descriptors ■ Listening descriptor ■ End point for client connection requests ■ Created once and exists for lifetime of the server ■ Connected descriptor ■ End point of the connection between client and server ■ A new descriptor is created each time the server accepts a connection request from a client ■ Exists only as long as it takes to service client ■ Why the distinction? ■ Allows for concurrent servers that can communicate over many client connections simultaneously ■ E.g., Each time we receive a new request, we fork a child to handle the request
  • 24. +Sockets Interface: connect ■ A client establishes a connection with a server by calling connect: ■ Attempts to establish a connection with server at socket address addr ■ If successful, then clientfd is now ready for reading and writing. ■ addrlen is sizeof(sockaddr_in) int connect(int clientfd, sockaddr* addr, socklen_t addrlen);
  • 25. +accept & connect Illustrated listenfd(3) Client 1. Server blocks in accept, waiting for connection request on listening descriptor listenfd clientfd Server listenfd(3) Client clientfd Server 2. Client makes connection request by calling and blocking in connect Connection request listenfd(3) Client clientfd Server 3. Server returns connfd from accept. Client returns from connect. Connection is now established between clientfd and connfd connfd(4)
  • 26. +Socket Client & Server ■ Lets take a look at some code… ■ See lecture24/client.c and lecture24/server.c