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 (20)

PPTX
Linux Systems Prograramming: Unix Domain, Internet Domain (TCP, UDP) Socket P...
RashidFaridChishti
 
PPT
chapter-4-networking hjgjjgj did hfhhfhj
AmitDeshai
 
PDF
CISSP Prep: Ch 5. Communication and Network Security (Part 1)
Sam Bowne
 
PPT
lecture03 on socket programming000000.ppt
SoumabhaRoy
 
PPT
lecture03for socket programming college.ppt
SoumabhaRoy
 
PPT
Computer Network in Network software.ppt
mcjaya2024
 
PPTX
Lecture 3 computer communications and networks
anasbro009
 
PPT
Sockets
Gopaiah Sanaka
 
PPT
MK-PPT Chapter 1.ppt computer networks foundation
AliMarhoon18
 
PDF
4. Communication and Network Security
Sam Bowne
 
PDF
CH1-LECTURE_1.pdf
TamiratDejene1
 
PPTX
Networking concepts by Sachidananda M H
Sachidananda M H
 
PDF
Ajp notes-chapter-04
Ankit Dubey
 
PPTX
Introduction to socket programming nbv
Nagasuri Bala Venkateswarlu
 
PPTX
TCP/IP Protocol Architeture
Manoj Kumar
 
PDF
Linux Internals - Interview essentials 2.0
Emertxe Information Technologies Pvt Ltd
 
PDF
lab04.pdf
SaidiCalala
 
PPT
Fundamentals of Networking
Israel Marcus
 
PPT
Application Layer
ushabarad142
 
PPTX
Sept 2017 internetworking
shahin raj
 
Linux Systems Prograramming: Unix Domain, Internet Domain (TCP, UDP) Socket P...
RashidFaridChishti
 
chapter-4-networking hjgjjgj did hfhhfhj
AmitDeshai
 
CISSP Prep: Ch 5. Communication and Network Security (Part 1)
Sam Bowne
 
lecture03 on socket programming000000.ppt
SoumabhaRoy
 
lecture03for socket programming college.ppt
SoumabhaRoy
 
Computer Network in Network software.ppt
mcjaya2024
 
Lecture 3 computer communications and networks
anasbro009
 
Sockets
Gopaiah Sanaka
 
MK-PPT Chapter 1.ppt computer networks foundation
AliMarhoon18
 
4. Communication and Network Security
Sam Bowne
 
CH1-LECTURE_1.pdf
TamiratDejene1
 
Networking concepts by Sachidananda M H
Sachidananda M H
 
Ajp notes-chapter-04
Ankit Dubey
 
Introduction to socket programming nbv
Nagasuri Bala Venkateswarlu
 
TCP/IP Protocol Architeture
Manoj Kumar
 
Linux Internals - Interview essentials 2.0
Emertxe Information Technologies Pvt Ltd
 
lab04.pdf
SaidiCalala
 
Fundamentals of Networking
Israel Marcus
 
Application Layer
ushabarad142
 
Sept 2017 internetworking
shahin raj
 

Recently uploaded (20)

PDF
Lesson 1 : Science and the Art of Geography Ecosystem
marvinnbustamante1
 
PDF
VCE Literature Section A Exam Response Guide
jpinnuck
 
PDF
Wikinomics How Mass Collaboration Changes Everything Don Tapscott
wcsqyzf5909
 
PDF
Andreas Schleicher_Teaching Compass_Education 2040.pdf
EduSkills OECD
 
PPTX
ESP 10 Edukasyon sa Pagpapakatao PowerPoint Lessons Quarter 1.pptx
Sir J.
 
PPTX
Iván Bornacelly - Presentation of the report - Empowering the workforce in th...
EduSkills OECD
 
PPTX
A Case of Identity A Sociological Approach Fix.pptx
Ismail868386
 
PPTX
2025 Completing the Pre-SET Plan Form.pptx
mansk2
 
PDF
COM and NET Component Services 1st Edition Juval Löwy
kboqcyuw976
 
PPTX
Project 4 PART 1 AI Assistant Vocational Education
barmanjit380
 
PDF
CAD25 Gbadago and Fafa Presentation Revised-Aston Business School, UK.pdf
Kweku Zurek
 
PDF
THE PSYCHOANALYTIC OF THE BLACK CAT BY EDGAR ALLAN POE (1).pdf
nabilahk908
 
PPTX
How to Configure Refusal of Applicants in Odoo 18 Recruitment
Celine George
 
PPTX
How to Create & Manage Stages in Odoo 18 Helpdesk
Celine George
 
PPTX
Photo chemistry Power Point Presentation
mprpgcwa2024
 
DOCX
DLL english grade five goof for one week
FlordelynGonzales1
 
PDF
Supply Chain Security A Comprehensive Approach 1st Edition Arthur G. Arway
rxgnika452
 
PPTX
How to Add New Item in CogMenu in Odoo 18
Celine George
 
PPTX
Urban Hierarchy and Service Provisions.pptx
Islamic University of Bangladesh
 
PPT
M&A5 Q1 1 differentiate evolving early Philippine conventional and contempora...
ErlizaRosete
 
Lesson 1 : Science and the Art of Geography Ecosystem
marvinnbustamante1
 
VCE Literature Section A Exam Response Guide
jpinnuck
 
Wikinomics How Mass Collaboration Changes Everything Don Tapscott
wcsqyzf5909
 
Andreas Schleicher_Teaching Compass_Education 2040.pdf
EduSkills OECD
 
ESP 10 Edukasyon sa Pagpapakatao PowerPoint Lessons Quarter 1.pptx
Sir J.
 
Iván Bornacelly - Presentation of the report - Empowering the workforce in th...
EduSkills OECD
 
A Case of Identity A Sociological Approach Fix.pptx
Ismail868386
 
2025 Completing the Pre-SET Plan Form.pptx
mansk2
 
COM and NET Component Services 1st Edition Juval Löwy
kboqcyuw976
 
Project 4 PART 1 AI Assistant Vocational Education
barmanjit380
 
CAD25 Gbadago and Fafa Presentation Revised-Aston Business School, UK.pdf
Kweku Zurek
 
THE PSYCHOANALYTIC OF THE BLACK CAT BY EDGAR ALLAN POE (1).pdf
nabilahk908
 
How to Configure Refusal of Applicants in Odoo 18 Recruitment
Celine George
 
How to Create & Manage Stages in Odoo 18 Helpdesk
Celine George
 
Photo chemistry Power Point Presentation
mprpgcwa2024
 
DLL english grade five goof for one week
FlordelynGonzales1
 
Supply Chain Security A Comprehensive Approach 1st Edition Arthur G. Arway
rxgnika452
 
How to Add New Item in CogMenu in Odoo 18
Celine George
 
Urban Hierarchy and Service Provisions.pptx
Islamic University of Bangladesh
 
M&A5 Q1 1 differentiate evolving early Philippine conventional and contempora...
ErlizaRosete
 
Ad

+ 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