SlideShare a Scribd company logo
Net Programming
Topics
 Programmer’s view of the Internet (review)
 Sockets interface
 Writing clients and servers
net2.ppt
“Tour of the Black Holes of Computing!”
– 2 – CS 105
Client-Server Transactions
Client
process
Server
process
1. Client sends request
2. Server
handles
request
3. Server sends response
4. Client
handles
response
Resource
Every network application is based on client-server
model:
 Server process and one or more client processes
 Server manages some resource.
 Server provides service by manipulating resource for clients.
Note: clients and servers are processes running on hosts
(can be the same or different hosts).
– 3 – CS 105
Programmer’s View of Internet
1. Hosts are mapped to set of 32-bit or 128-bit IP
addresses
 134.173.42.2
 2001:1878:301:902:214:22ff:fe7c:883f
2. IP addresses are mapped to identifiers called Internet
domain names
 134.173.42.2 maps to www.cs.hmc.edu
 In general, mapping is many-to-many
3. Process on one Internet host can communicate with
process on another over a connection
– 4 – CS 105
1. IP Addresses
Computers are identified by IP addresses
Two flavors: IPv4 (old) and IPv6 (new)
Both are stored in an IP address struct of appropriate
type
 in_addr for IPv4
 in6_addr for IPv6
Details don’t matter; library functions usually hide them
– 5 – CS 105
2. Domain Naming System (DNS)
Internet maintains mapping between IP addresses and
domain names in huge worldwide distributed database
called DNS
 Conceptually, programmers can view DNS database as collection
of millions of host entry structures:
Functions for retrieving host entries from DNS:
 getaddrinfo: query key is a DNS domain name
 getnameinfo: query key is an IP address
/* DNS host entry structure */
struct hostent {
char *h_name; /* official domain name of host */
char **h_aliases; /* null-terminated array of domain names */
int h_addrtype; /* host address type (AF_INET or AF_INET6) */
int h_length; /* length of an address, in bytes */
char **h_addr_list; /* null-terminated array of in*_addr structs
*/
};
– 6 – CS 105
3. Internet Connections
Connection socket pair
(128.2.194.242:51213, 134.173.42.2:80)
Server
(port 80)
Client
Client socket address
128.2.194.242:51213
Server socket address
134.173.42.2:80
Client host address
128.2.194.242
Server host address
134.173.42.2
Clients and servers communicate by sending streams
of bytes over connections
Connections are point-to-point, full-duplex (2-way
communication), and reliable
Note: 51213 is an
ephemeral port allocated
by the kernel
Note: 80 is a well-known port
associated with Web servers
– 7 – CS 105
Clients
Examples of client programs
 Web browsers, ftp, telnet, ssh
How does a client find the server?
 IP address in server socket address identifies host (more
precisely, an adapter on the host)
 (Well-known) port in server socket address identifies service,
and thus implicitly identifies server process that provides it
 Examples of well-known ports
 Port 7: Echo server
 Port 23: Telnet server
 Port 25: Mail server
 Port 80: Web server
– 8 – CS 105
Using Ports to Identify
Services
Web server
(port 80)
Client host
Server host 134.173.42.2
Echo server
(port 7)
Service request for
134.173.42.2:80
(i.e., Web server)
Web server
(port 80)
Echo server
(port 7)
Service request for
134.173.42.2:7
(i.e., echo server)
Kernel
Kernel
Client
Client
– 9 – CS 105
Servers
Servers are long-running processes (daemons).
 Created at boot time (typically) by init process (process 1)
 Run continuously until machine is turned off
 Or spawned by inetd in response to connection to port
Each server waits for requests to arrive on well-known
port associated with that particular service
 Port 7: echo server
 Port 23: telnet server
 Port 25: mail server
 Port 80: HTTP server
Machine that runs a server process is also often referred
to as a “server”
– 10 – CS 105
Server Examples
Web server (port 80)
 Resource: files/compute cycles (CGI programs)
 Service: retrieves files and runs CGI programs on behalf of
the client
FTP server (20, 21)
 Resource: files
 Service: stores and retrieve files
Ssh server (22)
 Resource: terminal
 Service: proxies a terminal on the server machine
Mail server (25)
 Resource: email “spool” file
 Service: stores mail messages in spool file
See /etc/services for a
comprehensive list of the
services (potentially)
available on a Linux
machine.
– 11 – CS 105
Sockets Interface
Created in the early 80’s as part of original Berkeley
distribution of Unix that contained an early version of
the Internet protocols
Designed for “any” network protocol
Provides a user-level interface to the network
Underlying basis for all Internet applications
Based on client/server programming model
– 12 – CS 105
Overview of Sockets Interface
Client Server
socket socket
bind
listen
accept
read
read
write
close
read
connect
write
close
Connection
request
EOF
Await connection
request from
next client
open_listenfd
open_clientfd
– 13 – CS 105
Sockets
What is a socket?
 To kernel, socket is endpoint of communication
 To application, socket is file descriptor that lets application
read from or write to network
 Remember: 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
Main distinction between regular file I/O and socket I/O
is how application “opens” socket descriptors
– 14 – CS 105
Socket Address Structures
Generic socket address (struct sockaddr):
 For address arguments to connect, bind, and accept
 Necessary because other networks (non-TCP/IP) have
different address formats
 But doesn’t work for IPv6!
Internet-specific socket addresses:
 IPv4 uses sockaddr_in
 IPv6 uses sockaddr_in6
 Must use union to be sure of having enough space, sigh
– 15 – CS 105
Echo Client Main Routine
/* #include lots of stuff */
/* usage: ./echoclient host port */
int main(int argc, char **argv)
{
int clientfd;
size_t n;
char *host, *port, buf[MAXLINE];
host = argv[1];
port = argv[2];
if ((clientfd = open_clientfd(host, port)) == -1)
exit(1);
while (fgets(buf, sizeof buf - 1, stdin) != NULL) {
write(clientfd, buf, strlen(buf));
n = read(clientfd, buf, sizeof buf - 1);
if (n >= 0) {
buf[n] = '0';
fputs(buf, stdout);
}
}
close(clientfd);
exit(0);
}
– 16 – CS 105
Echo Client: open_clientfd
int open_clientfd(char *hostname, char *port)
{
int clientfd, error;
struct addrinfo hints, *hostaddresses = NULL;
/* Find out the server's IP address and port */
memset(&hints, 0, sizeof hints);
hints.ai_flags = AI_ADDRCONFIG | AI_V4MAPPED;
hints.ai_family = AF_INET6;
hints.ai_socktype = SOCK_STREAM;
if (getaddrinfo(hostname, port, &hints, &hostaddresses) != 0)
return -2;
}
/* We take advantage of the fact that AF_* and PF_* are identical */
clientfd = socket(hostaddresses->ai_family,
hostaddresses->ai_socktype, hostaddresses->ai_protocol);
if (clientfd == -1)
return -1; /* check errno for cause of error */
/* Establish a connection with the server */
if (connect(clientfd, hostaddresses->ai_addr, hostaddresses->ai_addrlen)
== -1)
return -1;
freeaddrinfo(hostaddresses);
return clientfd;
}
This function opens connection
from client to server at
hostname:port
Details follow….
– 17 – CS 105
Echo Client: open_clientfd
(getaddrinfo)
hints.ai_flags = AI_ADDRCONFIG | AI_V4MAPPED;
hints.ai_family = AF_INET6;
hints.ai_socktype = SOCK_STREAM;
if (getaddrinfo(hostname, port, &hints, &hostaddresses) != 0)
... (more)
getaddrinfo finds out about an Internet host
 AI_ADDRCONFIG: only give IPv6 address if our machine can talk
IPv6; likewise for IPv4
 AI_V4MAPPED: translate IPv6 to IPv4 when needed
 AF_INET6: prefer IPv6 to IPv4
 SOCK_STREAM: selects a reliable byte-stream connection
– 18 – CS 105
Echo Client: open_clientfd
(socket)
int clientfd; /* socket descriptor */
clientfd = socket(hostaddresses->ai_family,
hostaddresses->ai_socktype, hostaddresses->ai_protocol);
... (more)
socket creates socket descriptor on client
 All details provided by getaddrinfo
 Possibility of multiple addresses (must loop & try all)
– 19 – CS 105
Echo Client: open_clientfd
(connect)
Finally, client creates connection with server
 Client process suspends (blocks) until connection is created
 After resuming, client is ready to begin exchanging messages
with server via Unix I/O calls on descriptor sockfd
 hostaddresses is linked list, must be freed
 Including on error returns (not shown for brevity)
int clientfd; /* socket descriptor */
...
/* Establish a connection with the server */
if (connect(clientfd, hostaddresses->ai_addr,
hostaddresses->ai_addrlen)
== -1)
return -1;
freeaddrinfo(hostaddresses);
– 20 – CS 105
Echo Server: Main Routine
int main(int argc, char **argv) {
int listenfd, connfd, clientlen, error;
char * port;
union {struct sockaddr_in client4; struct sockaddr_in6 client6;
} clientaddr;
char hostname[NI_MAXHOST], hostaddr[NI_MAXHOST];
listenfd = open_listenfd(argv[1]);
if (listenfd < 0)
exit(1);
while (1) {
clientlen = sizeof clientaddr;
connfd = accept(listenfd, (struct sockaddr *)&clientaddr, &clientlen);
if (connfd == -1)
continue;
error = getnameinfo((struct sockaddr*)&clientaddr, clientlen,
hostname, sizeof hostname, NULL, 0, 0);
if (error != 0)
continue;
getnameinfo((struct sockaddr*)&clientaddr, clientlen,
hostaddr, sizeof hostaddr, NULL, 0, NI_NUMERICHOST);
printf("server connected to %s (%s)n", hostname, hostaddr);
echo(connfd);
close(connfd);
}
}
This program repeatedly
waits for connections, then
calls echo(). Details will
follow after we look at
open_listenfd()…
– 21 – CS 105
int open_listenfd(char *port)
{
int listenfd, optval=1, error;
struct addrinfo hints;
struct addrinfo *hostaddresses = NULL;
/* Find out the server's IP address and port */
memset(&hints, 0, sizeof hints);
hints.ai_flags = AI_ADDRCONFIG | AI_V4MAPPED | AI_PASSIVE;
hints.ai_family = AF_INET6;
hints.ai_socktype = SOCK_STREAM;
error = getaddrinfo(NULL, port, &hints, &hostaddresses);
if (error != 0)
return -2;
if ((listenfd = socket(hostaddresses->ai_family,
hostaddresses->ai_socktype, hostaddresses->ai_protocol)) == -1)
return -1;
/* Eliminates "Address already in use" error from bind. */
if (setsockopt(listenfd, SOL_SOCKET, SO_REUSEADDR,
(const void *)&optval , sizeof optval) == -1)
return -1;
/* Listenfd will be an endpoint for all requests to port */
if (bind(listenfd, hostaddresses->ai_addr,
hostaddresses->ai_addrlen) == -1)
return -1;
/* Make it a listening socket ready to accept
connection requests */
if (listen(listenfd, LISTEN_MAX) == -1)
return -1;
return listenfd;
}
Echo Server: open_listenfd
This function opens a file
descriptor on which server
can listen for incoming
connections. Details follow…
– 22 – CS 105
Here, getaddrinfo sets up to create generic “port”
 Most options same as for open_clientfd
 AI_PASSIVE: allow any host to connect to us (because we’re a
server)
 First argument to getaddrinfo is NULL because we won’t be
connecting to a specific host
Echo Server: open_listenfd
(getaddrinfo)
hints.ai_flags = AI_ADDRCONFIG | AI_V4MAPPED | AI_PASSIVE;
hints.ai_family = AF_INET6;
hints.ai_socktype = SOCK_STREAM;
error = getaddrinfo(NULL, port, &hints, &hostaddresses);
– 23 – CS 105
socket creates socket descriptor on the server
 All important parameters provided by getaddrinfo
 Saves us from worrying about IPv4 vs. IPv6
Echo Server: open_listenfd
(socket)
int listenfd; /* listening socket descriptor */
/* Create a socket descriptor */
if ((listenfd = socket(hostaddresses->ai_family,
hostaddresses->ai_socktype, hostaddresses->ai_protocol)) == -1)
return -1;
– 24 – CS 105
Echo Server: open_listenfd
(setsockopt)
The socket can be given some attributes
Handy trick that allows us to rerun the server
immediately after we kill it
 Otherwise we would have to wait about 15 secs
 Eliminates “Address already in use” error from bind()
Strongly suggest you do this for all your servers to
simplify debugging
...
/* Eliminates "Address already in use" error from bind(). */
if (setsockopt(listenfd, SOL_SOCKET, SO_REUSEADDR,
(const void *)&optval , sizeof optval) == -1)
return -1;
– 25 – CS 105
Echo Server: open_listenfd
(bind)
bind associates socket with socket address we just
created
Again, important parameters come from getaddrinfo
int listenfd; /* listening socket */
...
/* listenfd will be an endpoint for all requests to port
on any IP address for this host */
if (bind(listenfd, hostaddresses->ai_addr,
hostaddresses->ai_addrlen) == -1)
return -1;
– 26 – CS 105
Echo Server: open_listenfd
(listen)
listen indicates that this socket will accept
connection (connect) requests from clients
We’re finally ready to enter main server loop that
accepts and processes client connection requests
int listenfd; /* listening socket */
...
/* Make it a listening socket ready to accept connection requests */
if (listen(listenfd, LISTEN_MAX) == -1)
return -1;
return listenfd;
}
– 27 – CS 105
Echo Server: Main Loop
Server loops endlessly, waiting for connection
requests, then reading input from client and echoing
it back to client
main() {
/* create and configure the listening socket */
while(1) {
/* accept(): wait for a connection request */
/* echo(): read and echo input lines from client til EOF */
/* close(): close the connection */
}
}
– 28 – CS 105
accept() blocks waiting for connection request
accept returns connected descriptor (connfd) with
same properties as listening descriptor (listenfd)
 Returns when connection between client and server is
created and ready for I/O transfers
 All I/O with client will be done via connected socket
accept also fills in client’s IP address
Echo Server: accept
int listenfd; /* listening descriptor */
int connfd; /* connected descriptor */
union { struct sockaddr_in client4; struct sockaddr_in6 client6;
} clientaddr;
int clientlen;
clientlen = sizeof(clientaddr);
connfd = accept(listenfd, (struct sockaddr *)&clientaddr,
&clientlen);
– 29 – CS 105
Echo Server: accept 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)
– 30 – CS 105
Connected vs. Listening
Descriptors
Listening descriptor
 End point for client connection requests
 Created once and exists for lifetime of server
Connected descriptor
 End point of connection between client and server
 New descriptor is created each time server accepts
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 or
spawn a thread to handle it
 Can be closed to break connection to particular client
– 31 – CS 105
Echo Server: Identifying Client
Server can determine domain name and IP address of
client
char hostname[NI_MAXHOST], hostaddr[NI_MAXHOST];
…
error = getnameinfo((struct sockaddr*)&clientaddr, clientlen,
hostname, sizeof hostname, NULL, 0, 0);
if (error != 0)
continue;
getnameinfo((struct sockaddr*)&clientaddr, clientlen,
hostaddr, sizeof hostaddr, NULL, 0, NI_NUMERICHOST);
printf("server connected to %s (%s)n", hostname, hostaddr);
– 32 – CS 105
Echo Server: echo
void echo(int connfd)
{
size_t n;
char buf[MAXLINE];
while((n = read(connfd, buf, sizeof buf)) > 0) {
printf("server received %d bytesn", n);
write(connfd, buf, n);
}
}
Server uses Unix I/O to read and echo text lines until
EOF (end-of-file) is encountered
 EOF notification caused by client calling close(clientfd)
 IMPORTANT: EOF is a condition, not a particular data byte
– 33 – CS 105
Testing Servers Using telnet
The telnet program is invaluable for testing servers
that transmit ASCII strings over Internet connections
 Our simple echo server
 Web servers
 Mail servers
Usage:
 unix> telnet <host> <portnumber>
 Creates connection with server running on <host> and
listening on port <portnumber>.
– 34 – CS 105
Testing Echo Server With
telnet
mallet> ./echoserver 5000
server connected to bow-vpn.cs.hmc.edu (::ffff:192.168.6.5)
server received 5 bytes
server connected to bow-vpn.cs.hmc.edu (::ffff:192.168.6.5)
server received 8 bytes
bow> telnet mallet-vpn 5000
Trying 192.168.6.1...
Connected to mallet-vpn.
Escape character is '^]'.
123
123
Connection closed by foreign host.
bow> telnet mallet-vpn 5000
Trying 192.168.6.1...
Connected to mallet-vpn.
Escape character is '^]'.
456789
456789
Connection closed by foreign host.
bow>
– 35 – CS 105
Running Echo Client and
Server
mallet> echoserver 5000
server connected to bow-vpn.cs.hmc.edu (::ffff:192.168.6.5)
server received 4 bytes
server connected to bow-vpn.cs.hmc.edu (::ffff:192.168.6.5)
server received 7 bytes
...
bow> echoclient mallet-vpn 5000
123
123
bow> echoclient mallet-vpn 5000
456789
456789
bow>
– 36 – CS 105
One More Important Function
Real servers often want to handle multiple clients
Problem: you have 3 clients. Only B wants service.
You can’t really write serve(A); serve(B);
serve(C); because B must wait for A to ask for
service
Solution: select system call
 Accepts set of file descriptors you’re interested in
 Tells you which ones have input waiting or are ready for
output
 Then you can read from/write to only the active ones
 For more info, see man 2 select and Chapter 13
– 37 – CS 105
For More Information
W. Richard Stevens, “Unix Network Programming:
Networking APIs: Sockets and XTI”, Volume 1,
Second Edition, Prentice Hall, 1998.
 THE network programming bible.
Complete versions of the echo client and server (for
IPV4 only) are developed in the text
 IPV6 versions from slides available from class web page
Ad

More Related Content

Similar to Net Programming.ppt (20)

Socket Programming
Socket ProgrammingSocket Programming
Socket Programming
CEC Landran
 
JavaSockets-Session10 New York university.pdf
JavaSockets-Session10 New York university.pdfJavaSockets-Session10 New York university.pdf
JavaSockets-Session10 New York university.pdf
jose19881
 
اسلاید اول جلسه یازدهم کلاس پایتون برای هکرهای قانونی
اسلاید اول جلسه یازدهم کلاس پایتون برای هکرهای قانونیاسلاید اول جلسه یازدهم کلاس پایتون برای هکرهای قانونی
اسلاید اول جلسه یازدهم کلاس پایتون برای هکرهای قانونی
Mohammad Reza Kamalifard
 
Os 2
Os 2Os 2
Os 2
university of Gujrat, pakistan
 
LECTURE-17(Socket Programming) Detailed.
LECTURE-17(Socket Programming) Detailed.LECTURE-17(Socket Programming) Detailed.
LECTURE-17(Socket Programming) Detailed.
qamarmajeed0000
 
Socket Programming
Socket ProgrammingSocket Programming
Socket Programming
Mostak Ahmed
 
Network programming using python
Network programming using pythonNetwork programming using python
Network programming using python
Ali Nezhad
 
Basic socket programming
Basic socket programmingBasic socket programming
Basic socket programming
Kristian Arjianto
 
CODE FOR echo_client.c A simple echo client using TCP #inc.pdf
CODE FOR echo_client.c A simple echo client using TCP  #inc.pdfCODE FOR echo_client.c A simple echo client using TCP  #inc.pdf
CODE FOR echo_client.c A simple echo client using TCP #inc.pdf
secunderbadtirumalgi
 
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
 
Socket programming in C
Socket programming in CSocket programming in C
Socket programming in C
Deepak Swain
 
Socket System Calls
Socket System CallsSocket System Calls
Socket System Calls
Avinash Varma Kalidindi
 
Client server
Client serverClient server
Client server
maryam1231
 
Lan chat system
Lan chat systemLan chat system
Lan chat system
Wipro
 
L5-Sockets.pptx
L5-Sockets.pptxL5-Sockets.pptx
L5-Sockets.pptx
ycelgemici1
 
Raspberry pi Part 23
Raspberry pi Part 23Raspberry pi Part 23
Raspberry pi Part 23
Techvilla
 
Socket Programming
Socket ProgrammingSocket Programming
Socket Programming
elliando dias
 
Unit 8 Java
Unit 8 JavaUnit 8 Java
Unit 8 Java
arnold 7490
 
Socket programming
Socket programming Socket programming
Socket programming
Rajivarnan (Rajiv)
 
INTRODUCTION TO SOCKETS IN COMPUTER NETWORKS DEPT OF CSE.ppt
INTRODUCTION TO SOCKETS IN COMPUTER NETWORKS DEPT OF CSE.pptINTRODUCTION TO SOCKETS IN COMPUTER NETWORKS DEPT OF CSE.ppt
INTRODUCTION TO SOCKETS IN COMPUTER NETWORKS DEPT OF CSE.ppt
senthilnathans25
 
Socket Programming
Socket ProgrammingSocket Programming
Socket Programming
CEC Landran
 
JavaSockets-Session10 New York university.pdf
JavaSockets-Session10 New York university.pdfJavaSockets-Session10 New York university.pdf
JavaSockets-Session10 New York university.pdf
jose19881
 
اسلاید اول جلسه یازدهم کلاس پایتون برای هکرهای قانونی
اسلاید اول جلسه یازدهم کلاس پایتون برای هکرهای قانونیاسلاید اول جلسه یازدهم کلاس پایتون برای هکرهای قانونی
اسلاید اول جلسه یازدهم کلاس پایتون برای هکرهای قانونی
Mohammad Reza Kamalifard
 
LECTURE-17(Socket Programming) Detailed.
LECTURE-17(Socket Programming) Detailed.LECTURE-17(Socket Programming) Detailed.
LECTURE-17(Socket Programming) Detailed.
qamarmajeed0000
 
Socket Programming
Socket ProgrammingSocket Programming
Socket Programming
Mostak Ahmed
 
Network programming using python
Network programming using pythonNetwork programming using python
Network programming using python
Ali Nezhad
 
CODE FOR echo_client.c A simple echo client using TCP #inc.pdf
CODE FOR echo_client.c A simple echo client using TCP  #inc.pdfCODE FOR echo_client.c A simple echo client using TCP  #inc.pdf
CODE FOR echo_client.c A simple echo client using TCP #inc.pdf
secunderbadtirumalgi
 
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
 
Socket programming in C
Socket programming in CSocket programming in C
Socket programming in C
Deepak Swain
 
Lan chat system
Lan chat systemLan chat system
Lan chat system
Wipro
 
Raspberry pi Part 23
Raspberry pi Part 23Raspberry pi Part 23
Raspberry pi Part 23
Techvilla
 
INTRODUCTION TO SOCKETS IN COMPUTER NETWORKS DEPT OF CSE.ppt
INTRODUCTION TO SOCKETS IN COMPUTER NETWORKS DEPT OF CSE.pptINTRODUCTION TO SOCKETS IN COMPUTER NETWORKS DEPT OF CSE.ppt
INTRODUCTION TO SOCKETS IN COMPUTER NETWORKS DEPT OF CSE.ppt
senthilnathans25
 

Recently uploaded (20)

Secure Test Infrastructure: The Backbone of Trustworthy Software Development
Secure Test Infrastructure: The Backbone of Trustworthy Software DevelopmentSecure Test Infrastructure: The Backbone of Trustworthy Software Development
Secure Test Infrastructure: The Backbone of Trustworthy Software Development
Shubham Joshi
 
Microsoft AI Nonprofit Use Cases and Live Demo_2025.04.30.pdf
Microsoft AI Nonprofit Use Cases and Live Demo_2025.04.30.pdfMicrosoft AI Nonprofit Use Cases and Live Demo_2025.04.30.pdf
Microsoft AI Nonprofit Use Cases and Live Demo_2025.04.30.pdf
TechSoup
 
Top 10 Client Portal Software Solutions for 2025.docx
Top 10 Client Portal Software Solutions for 2025.docxTop 10 Client Portal Software Solutions for 2025.docx
Top 10 Client Portal Software Solutions for 2025.docx
Portli
 
Interactive odoo dashboards for sales, CRM , Inventory, Invoice, Purchase, Pr...
Interactive odoo dashboards for sales, CRM , Inventory, Invoice, Purchase, Pr...Interactive odoo dashboards for sales, CRM , Inventory, Invoice, Purchase, Pr...
Interactive odoo dashboards for sales, CRM , Inventory, Invoice, Purchase, Pr...
AxisTechnolabs
 
Who Watches the Watchmen (SciFiDevCon 2025)
Who Watches the Watchmen (SciFiDevCon 2025)Who Watches the Watchmen (SciFiDevCon 2025)
Who Watches the Watchmen (SciFiDevCon 2025)
Allon Mureinik
 
Top 10 Data Cleansing Tools for 2025.pdf
Top 10 Data Cleansing Tools for 2025.pdfTop 10 Data Cleansing Tools for 2025.pdf
Top 10 Data Cleansing Tools for 2025.pdf
AffinityCore
 
Tools of the Trade: Linux and SQL - Google Certificate
Tools of the Trade: Linux and SQL - Google CertificateTools of the Trade: Linux and SQL - Google Certificate
Tools of the Trade: Linux and SQL - Google Certificate
VICTOR MAESTRE RAMIREZ
 
Revolutionizing Residential Wi-Fi PPT.pptx
Revolutionizing Residential Wi-Fi PPT.pptxRevolutionizing Residential Wi-Fi PPT.pptx
Revolutionizing Residential Wi-Fi PPT.pptx
nidhisingh691197
 
Avast Premium Security Crack FREE Latest Version 2025
Avast Premium Security Crack FREE Latest Version 2025Avast Premium Security Crack FREE Latest Version 2025
Avast Premium Security Crack FREE Latest Version 2025
mu394968
 
Automation Techniques in RPA - UiPath Certificate
Automation Techniques in RPA - UiPath CertificateAutomation Techniques in RPA - UiPath Certificate
Automation Techniques in RPA - UiPath Certificate
VICTOR MAESTRE RAMIREZ
 
How to Optimize Your AWS Environment for Improved Cloud Performance
How to Optimize Your AWS Environment for Improved Cloud PerformanceHow to Optimize Your AWS Environment for Improved Cloud Performance
How to Optimize Your AWS Environment for Improved Cloud Performance
ThousandEyes
 
How can one start with crypto wallet development.pptx
How can one start with crypto wallet development.pptxHow can one start with crypto wallet development.pptx
How can one start with crypto wallet development.pptx
laravinson24
 
Full Cracked Resolume Arena Latest Version
Full Cracked Resolume Arena Latest VersionFull Cracked Resolume Arena Latest Version
Full Cracked Resolume Arena Latest Version
jonesmichealj2
 
Landscape of Requirements Engineering for/by AI through Literature Review
Landscape of Requirements Engineering for/by AI through Literature ReviewLandscape of Requirements Engineering for/by AI through Literature Review
Landscape of Requirements Engineering for/by AI through Literature Review
Hironori Washizaki
 
Innovative Approaches to Software Dev no good at all
Innovative Approaches to Software Dev no good at allInnovative Approaches to Software Dev no good at all
Innovative Approaches to Software Dev no good at all
ayeshakanwal75
 
Scaling GraphRAG: Efficient Knowledge Retrieval for Enterprise AI
Scaling GraphRAG:  Efficient Knowledge Retrieval for Enterprise AIScaling GraphRAG:  Efficient Knowledge Retrieval for Enterprise AI
Scaling GraphRAG: Efficient Knowledge Retrieval for Enterprise AI
danshalev
 
Cryptocurrency Exchange Script like Binance.pptx
Cryptocurrency Exchange Script like Binance.pptxCryptocurrency Exchange Script like Binance.pptx
Cryptocurrency Exchange Script like Binance.pptx
riyageorge2024
 
The Significance of Hardware in Information Systems.pdf
The Significance of Hardware in Information Systems.pdfThe Significance of Hardware in Information Systems.pdf
The Significance of Hardware in Information Systems.pdf
drewplanas10
 
Kubernetes_101_Zero_to_Platform_Engineer.pptx
Kubernetes_101_Zero_to_Platform_Engineer.pptxKubernetes_101_Zero_to_Platform_Engineer.pptx
Kubernetes_101_Zero_to_Platform_Engineer.pptx
CloudScouts
 
Adobe Marketo Engage Champion Deep Dive - SFDC CRM Synch V2 & Usage Dashboards
Adobe Marketo Engage Champion Deep Dive - SFDC CRM Synch V2 & Usage DashboardsAdobe Marketo Engage Champion Deep Dive - SFDC CRM Synch V2 & Usage Dashboards
Adobe Marketo Engage Champion Deep Dive - SFDC CRM Synch V2 & Usage Dashboards
BradBedford3
 
Secure Test Infrastructure: The Backbone of Trustworthy Software Development
Secure Test Infrastructure: The Backbone of Trustworthy Software DevelopmentSecure Test Infrastructure: The Backbone of Trustworthy Software Development
Secure Test Infrastructure: The Backbone of Trustworthy Software Development
Shubham Joshi
 
Microsoft AI Nonprofit Use Cases and Live Demo_2025.04.30.pdf
Microsoft AI Nonprofit Use Cases and Live Demo_2025.04.30.pdfMicrosoft AI Nonprofit Use Cases and Live Demo_2025.04.30.pdf
Microsoft AI Nonprofit Use Cases and Live Demo_2025.04.30.pdf
TechSoup
 
Top 10 Client Portal Software Solutions for 2025.docx
Top 10 Client Portal Software Solutions for 2025.docxTop 10 Client Portal Software Solutions for 2025.docx
Top 10 Client Portal Software Solutions for 2025.docx
Portli
 
Interactive odoo dashboards for sales, CRM , Inventory, Invoice, Purchase, Pr...
Interactive odoo dashboards for sales, CRM , Inventory, Invoice, Purchase, Pr...Interactive odoo dashboards for sales, CRM , Inventory, Invoice, Purchase, Pr...
Interactive odoo dashboards for sales, CRM , Inventory, Invoice, Purchase, Pr...
AxisTechnolabs
 
Who Watches the Watchmen (SciFiDevCon 2025)
Who Watches the Watchmen (SciFiDevCon 2025)Who Watches the Watchmen (SciFiDevCon 2025)
Who Watches the Watchmen (SciFiDevCon 2025)
Allon Mureinik
 
Top 10 Data Cleansing Tools for 2025.pdf
Top 10 Data Cleansing Tools for 2025.pdfTop 10 Data Cleansing Tools for 2025.pdf
Top 10 Data Cleansing Tools for 2025.pdf
AffinityCore
 
Tools of the Trade: Linux and SQL - Google Certificate
Tools of the Trade: Linux and SQL - Google CertificateTools of the Trade: Linux and SQL - Google Certificate
Tools of the Trade: Linux and SQL - Google Certificate
VICTOR MAESTRE RAMIREZ
 
Revolutionizing Residential Wi-Fi PPT.pptx
Revolutionizing Residential Wi-Fi PPT.pptxRevolutionizing Residential Wi-Fi PPT.pptx
Revolutionizing Residential Wi-Fi PPT.pptx
nidhisingh691197
 
Avast Premium Security Crack FREE Latest Version 2025
Avast Premium Security Crack FREE Latest Version 2025Avast Premium Security Crack FREE Latest Version 2025
Avast Premium Security Crack FREE Latest Version 2025
mu394968
 
Automation Techniques in RPA - UiPath Certificate
Automation Techniques in RPA - UiPath CertificateAutomation Techniques in RPA - UiPath Certificate
Automation Techniques in RPA - UiPath Certificate
VICTOR MAESTRE RAMIREZ
 
How to Optimize Your AWS Environment for Improved Cloud Performance
How to Optimize Your AWS Environment for Improved Cloud PerformanceHow to Optimize Your AWS Environment for Improved Cloud Performance
How to Optimize Your AWS Environment for Improved Cloud Performance
ThousandEyes
 
How can one start with crypto wallet development.pptx
How can one start with crypto wallet development.pptxHow can one start with crypto wallet development.pptx
How can one start with crypto wallet development.pptx
laravinson24
 
Full Cracked Resolume Arena Latest Version
Full Cracked Resolume Arena Latest VersionFull Cracked Resolume Arena Latest Version
Full Cracked Resolume Arena Latest Version
jonesmichealj2
 
Landscape of Requirements Engineering for/by AI through Literature Review
Landscape of Requirements Engineering for/by AI through Literature ReviewLandscape of Requirements Engineering for/by AI through Literature Review
Landscape of Requirements Engineering for/by AI through Literature Review
Hironori Washizaki
 
Innovative Approaches to Software Dev no good at all
Innovative Approaches to Software Dev no good at allInnovative Approaches to Software Dev no good at all
Innovative Approaches to Software Dev no good at all
ayeshakanwal75
 
Scaling GraphRAG: Efficient Knowledge Retrieval for Enterprise AI
Scaling GraphRAG:  Efficient Knowledge Retrieval for Enterprise AIScaling GraphRAG:  Efficient Knowledge Retrieval for Enterprise AI
Scaling GraphRAG: Efficient Knowledge Retrieval for Enterprise AI
danshalev
 
Cryptocurrency Exchange Script like Binance.pptx
Cryptocurrency Exchange Script like Binance.pptxCryptocurrency Exchange Script like Binance.pptx
Cryptocurrency Exchange Script like Binance.pptx
riyageorge2024
 
The Significance of Hardware in Information Systems.pdf
The Significance of Hardware in Information Systems.pdfThe Significance of Hardware in Information Systems.pdf
The Significance of Hardware in Information Systems.pdf
drewplanas10
 
Kubernetes_101_Zero_to_Platform_Engineer.pptx
Kubernetes_101_Zero_to_Platform_Engineer.pptxKubernetes_101_Zero_to_Platform_Engineer.pptx
Kubernetes_101_Zero_to_Platform_Engineer.pptx
CloudScouts
 
Adobe Marketo Engage Champion Deep Dive - SFDC CRM Synch V2 & Usage Dashboards
Adobe Marketo Engage Champion Deep Dive - SFDC CRM Synch V2 & Usage DashboardsAdobe Marketo Engage Champion Deep Dive - SFDC CRM Synch V2 & Usage Dashboards
Adobe Marketo Engage Champion Deep Dive - SFDC CRM Synch V2 & Usage Dashboards
BradBedford3
 
Ad

Net Programming.ppt

  • 1. Net Programming Topics  Programmer’s view of the Internet (review)  Sockets interface  Writing clients and servers net2.ppt “Tour of the Black Holes of Computing!”
  • 2. – 2 – CS 105 Client-Server Transactions Client process Server process 1. Client sends request 2. Server handles request 3. Server sends response 4. Client handles response Resource Every network application is based on client-server model:  Server process and one or more client processes  Server manages some resource.  Server provides service by manipulating resource for clients. Note: clients and servers are processes running on hosts (can be the same or different hosts).
  • 3. – 3 – CS 105 Programmer’s View of Internet 1. Hosts are mapped to set of 32-bit or 128-bit IP addresses  134.173.42.2  2001:1878:301:902:214:22ff:fe7c:883f 2. IP addresses are mapped to identifiers called Internet domain names  134.173.42.2 maps to www.cs.hmc.edu  In general, mapping is many-to-many 3. Process on one Internet host can communicate with process on another over a connection
  • 4. – 4 – CS 105 1. IP Addresses Computers are identified by IP addresses Two flavors: IPv4 (old) and IPv6 (new) Both are stored in an IP address struct of appropriate type  in_addr for IPv4  in6_addr for IPv6 Details don’t matter; library functions usually hide them
  • 5. – 5 – CS 105 2. Domain Naming System (DNS) Internet maintains mapping between IP addresses and domain names in huge worldwide distributed database called DNS  Conceptually, programmers can view DNS database as collection of millions of host entry structures: Functions for retrieving host entries from DNS:  getaddrinfo: query key is a DNS domain name  getnameinfo: query key is an IP address /* DNS host entry structure */ struct hostent { char *h_name; /* official domain name of host */ char **h_aliases; /* null-terminated array of domain names */ int h_addrtype; /* host address type (AF_INET or AF_INET6) */ int h_length; /* length of an address, in bytes */ char **h_addr_list; /* null-terminated array of in*_addr structs */ };
  • 6. – 6 – CS 105 3. Internet Connections Connection socket pair (128.2.194.242:51213, 134.173.42.2:80) Server (port 80) Client Client socket address 128.2.194.242:51213 Server socket address 134.173.42.2:80 Client host address 128.2.194.242 Server host address 134.173.42.2 Clients and servers communicate by sending streams of bytes over connections Connections are point-to-point, full-duplex (2-way communication), and reliable Note: 51213 is an ephemeral port allocated by the kernel Note: 80 is a well-known port associated with Web servers
  • 7. – 7 – CS 105 Clients Examples of client programs  Web browsers, ftp, telnet, ssh How does a client find the server?  IP address in server socket address identifies host (more precisely, an adapter on the host)  (Well-known) port in server socket address identifies service, and thus implicitly identifies server process that provides it  Examples of well-known ports  Port 7: Echo server  Port 23: Telnet server  Port 25: Mail server  Port 80: Web server
  • 8. – 8 – CS 105 Using Ports to Identify Services Web server (port 80) Client host Server host 134.173.42.2 Echo server (port 7) Service request for 134.173.42.2:80 (i.e., Web server) Web server (port 80) Echo server (port 7) Service request for 134.173.42.2:7 (i.e., echo server) Kernel Kernel Client Client
  • 9. – 9 – CS 105 Servers Servers are long-running processes (daemons).  Created at boot time (typically) by init process (process 1)  Run continuously until machine is turned off  Or spawned by inetd in response to connection to port Each server waits for requests to arrive on well-known port associated with that particular service  Port 7: echo server  Port 23: telnet server  Port 25: mail server  Port 80: HTTP server Machine that runs a server process is also often referred to as a “server”
  • 10. – 10 – CS 105 Server Examples Web server (port 80)  Resource: files/compute cycles (CGI programs)  Service: retrieves files and runs CGI programs on behalf of the client FTP server (20, 21)  Resource: files  Service: stores and retrieve files Ssh server (22)  Resource: terminal  Service: proxies a terminal on the server machine Mail server (25)  Resource: email “spool” file  Service: stores mail messages in spool file See /etc/services for a comprehensive list of the services (potentially) available on a Linux machine.
  • 11. – 11 – CS 105 Sockets Interface Created in the early 80’s as part of original Berkeley distribution of Unix that contained an early version of the Internet protocols Designed for “any” network protocol Provides a user-level interface to the network Underlying basis for all Internet applications Based on client/server programming model
  • 12. – 12 – CS 105 Overview of Sockets Interface Client Server socket socket bind listen accept read read write close read connect write close Connection request EOF Await connection request from next client open_listenfd open_clientfd
  • 13. – 13 – CS 105 Sockets What is a socket?  To kernel, socket is endpoint of communication  To application, socket is file descriptor that lets application read from or write to network  Remember: 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 Main distinction between regular file I/O and socket I/O is how application “opens” socket descriptors
  • 14. – 14 – CS 105 Socket Address Structures Generic socket address (struct sockaddr):  For address arguments to connect, bind, and accept  Necessary because other networks (non-TCP/IP) have different address formats  But doesn’t work for IPv6! Internet-specific socket addresses:  IPv4 uses sockaddr_in  IPv6 uses sockaddr_in6  Must use union to be sure of having enough space, sigh
  • 15. – 15 – CS 105 Echo Client Main Routine /* #include lots of stuff */ /* usage: ./echoclient host port */ int main(int argc, char **argv) { int clientfd; size_t n; char *host, *port, buf[MAXLINE]; host = argv[1]; port = argv[2]; if ((clientfd = open_clientfd(host, port)) == -1) exit(1); while (fgets(buf, sizeof buf - 1, stdin) != NULL) { write(clientfd, buf, strlen(buf)); n = read(clientfd, buf, sizeof buf - 1); if (n >= 0) { buf[n] = '0'; fputs(buf, stdout); } } close(clientfd); exit(0); }
  • 16. – 16 – CS 105 Echo Client: open_clientfd int open_clientfd(char *hostname, char *port) { int clientfd, error; struct addrinfo hints, *hostaddresses = NULL; /* Find out the server's IP address and port */ memset(&hints, 0, sizeof hints); hints.ai_flags = AI_ADDRCONFIG | AI_V4MAPPED; hints.ai_family = AF_INET6; hints.ai_socktype = SOCK_STREAM; if (getaddrinfo(hostname, port, &hints, &hostaddresses) != 0) return -2; } /* We take advantage of the fact that AF_* and PF_* are identical */ clientfd = socket(hostaddresses->ai_family, hostaddresses->ai_socktype, hostaddresses->ai_protocol); if (clientfd == -1) return -1; /* check errno for cause of error */ /* Establish a connection with the server */ if (connect(clientfd, hostaddresses->ai_addr, hostaddresses->ai_addrlen) == -1) return -1; freeaddrinfo(hostaddresses); return clientfd; } This function opens connection from client to server at hostname:port Details follow….
  • 17. – 17 – CS 105 Echo Client: open_clientfd (getaddrinfo) hints.ai_flags = AI_ADDRCONFIG | AI_V4MAPPED; hints.ai_family = AF_INET6; hints.ai_socktype = SOCK_STREAM; if (getaddrinfo(hostname, port, &hints, &hostaddresses) != 0) ... (more) getaddrinfo finds out about an Internet host  AI_ADDRCONFIG: only give IPv6 address if our machine can talk IPv6; likewise for IPv4  AI_V4MAPPED: translate IPv6 to IPv4 when needed  AF_INET6: prefer IPv6 to IPv4  SOCK_STREAM: selects a reliable byte-stream connection
  • 18. – 18 – CS 105 Echo Client: open_clientfd (socket) int clientfd; /* socket descriptor */ clientfd = socket(hostaddresses->ai_family, hostaddresses->ai_socktype, hostaddresses->ai_protocol); ... (more) socket creates socket descriptor on client  All details provided by getaddrinfo  Possibility of multiple addresses (must loop & try all)
  • 19. – 19 – CS 105 Echo Client: open_clientfd (connect) Finally, client creates connection with server  Client process suspends (blocks) until connection is created  After resuming, client is ready to begin exchanging messages with server via Unix I/O calls on descriptor sockfd  hostaddresses is linked list, must be freed  Including on error returns (not shown for brevity) int clientfd; /* socket descriptor */ ... /* Establish a connection with the server */ if (connect(clientfd, hostaddresses->ai_addr, hostaddresses->ai_addrlen) == -1) return -1; freeaddrinfo(hostaddresses);
  • 20. – 20 – CS 105 Echo Server: Main Routine int main(int argc, char **argv) { int listenfd, connfd, clientlen, error; char * port; union {struct sockaddr_in client4; struct sockaddr_in6 client6; } clientaddr; char hostname[NI_MAXHOST], hostaddr[NI_MAXHOST]; listenfd = open_listenfd(argv[1]); if (listenfd < 0) exit(1); while (1) { clientlen = sizeof clientaddr; connfd = accept(listenfd, (struct sockaddr *)&clientaddr, &clientlen); if (connfd == -1) continue; error = getnameinfo((struct sockaddr*)&clientaddr, clientlen, hostname, sizeof hostname, NULL, 0, 0); if (error != 0) continue; getnameinfo((struct sockaddr*)&clientaddr, clientlen, hostaddr, sizeof hostaddr, NULL, 0, NI_NUMERICHOST); printf("server connected to %s (%s)n", hostname, hostaddr); echo(connfd); close(connfd); } } This program repeatedly waits for connections, then calls echo(). Details will follow after we look at open_listenfd()…
  • 21. – 21 – CS 105 int open_listenfd(char *port) { int listenfd, optval=1, error; struct addrinfo hints; struct addrinfo *hostaddresses = NULL; /* Find out the server's IP address and port */ memset(&hints, 0, sizeof hints); hints.ai_flags = AI_ADDRCONFIG | AI_V4MAPPED | AI_PASSIVE; hints.ai_family = AF_INET6; hints.ai_socktype = SOCK_STREAM; error = getaddrinfo(NULL, port, &hints, &hostaddresses); if (error != 0) return -2; if ((listenfd = socket(hostaddresses->ai_family, hostaddresses->ai_socktype, hostaddresses->ai_protocol)) == -1) return -1; /* Eliminates "Address already in use" error from bind. */ if (setsockopt(listenfd, SOL_SOCKET, SO_REUSEADDR, (const void *)&optval , sizeof optval) == -1) return -1; /* Listenfd will be an endpoint for all requests to port */ if (bind(listenfd, hostaddresses->ai_addr, hostaddresses->ai_addrlen) == -1) return -1; /* Make it a listening socket ready to accept connection requests */ if (listen(listenfd, LISTEN_MAX) == -1) return -1; return listenfd; } Echo Server: open_listenfd This function opens a file descriptor on which server can listen for incoming connections. Details follow…
  • 22. – 22 – CS 105 Here, getaddrinfo sets up to create generic “port”  Most options same as for open_clientfd  AI_PASSIVE: allow any host to connect to us (because we’re a server)  First argument to getaddrinfo is NULL because we won’t be connecting to a specific host Echo Server: open_listenfd (getaddrinfo) hints.ai_flags = AI_ADDRCONFIG | AI_V4MAPPED | AI_PASSIVE; hints.ai_family = AF_INET6; hints.ai_socktype = SOCK_STREAM; error = getaddrinfo(NULL, port, &hints, &hostaddresses);
  • 23. – 23 – CS 105 socket creates socket descriptor on the server  All important parameters provided by getaddrinfo  Saves us from worrying about IPv4 vs. IPv6 Echo Server: open_listenfd (socket) int listenfd; /* listening socket descriptor */ /* Create a socket descriptor */ if ((listenfd = socket(hostaddresses->ai_family, hostaddresses->ai_socktype, hostaddresses->ai_protocol)) == -1) return -1;
  • 24. – 24 – CS 105 Echo Server: open_listenfd (setsockopt) The socket can be given some attributes Handy trick that allows us to rerun the server immediately after we kill it  Otherwise we would have to wait about 15 secs  Eliminates “Address already in use” error from bind() Strongly suggest you do this for all your servers to simplify debugging ... /* Eliminates "Address already in use" error from bind(). */ if (setsockopt(listenfd, SOL_SOCKET, SO_REUSEADDR, (const void *)&optval , sizeof optval) == -1) return -1;
  • 25. – 25 – CS 105 Echo Server: open_listenfd (bind) bind associates socket with socket address we just created Again, important parameters come from getaddrinfo int listenfd; /* listening socket */ ... /* listenfd will be an endpoint for all requests to port on any IP address for this host */ if (bind(listenfd, hostaddresses->ai_addr, hostaddresses->ai_addrlen) == -1) return -1;
  • 26. – 26 – CS 105 Echo Server: open_listenfd (listen) listen indicates that this socket will accept connection (connect) requests from clients We’re finally ready to enter main server loop that accepts and processes client connection requests int listenfd; /* listening socket */ ... /* Make it a listening socket ready to accept connection requests */ if (listen(listenfd, LISTEN_MAX) == -1) return -1; return listenfd; }
  • 27. – 27 – CS 105 Echo Server: Main Loop Server loops endlessly, waiting for connection requests, then reading input from client and echoing it back to client main() { /* create and configure the listening socket */ while(1) { /* accept(): wait for a connection request */ /* echo(): read and echo input lines from client til EOF */ /* close(): close the connection */ } }
  • 28. – 28 – CS 105 accept() blocks waiting for connection request accept returns connected descriptor (connfd) with same properties as listening descriptor (listenfd)  Returns when connection between client and server is created and ready for I/O transfers  All I/O with client will be done via connected socket accept also fills in client’s IP address Echo Server: accept int listenfd; /* listening descriptor */ int connfd; /* connected descriptor */ union { struct sockaddr_in client4; struct sockaddr_in6 client6; } clientaddr; int clientlen; clientlen = sizeof(clientaddr); connfd = accept(listenfd, (struct sockaddr *)&clientaddr, &clientlen);
  • 29. – 29 – CS 105 Echo Server: accept 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)
  • 30. – 30 – CS 105 Connected vs. Listening Descriptors Listening descriptor  End point for client connection requests  Created once and exists for lifetime of server Connected descriptor  End point of connection between client and server  New descriptor is created each time server accepts 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 or spawn a thread to handle it  Can be closed to break connection to particular client
  • 31. – 31 – CS 105 Echo Server: Identifying Client Server can determine domain name and IP address of client char hostname[NI_MAXHOST], hostaddr[NI_MAXHOST]; … error = getnameinfo((struct sockaddr*)&clientaddr, clientlen, hostname, sizeof hostname, NULL, 0, 0); if (error != 0) continue; getnameinfo((struct sockaddr*)&clientaddr, clientlen, hostaddr, sizeof hostaddr, NULL, 0, NI_NUMERICHOST); printf("server connected to %s (%s)n", hostname, hostaddr);
  • 32. – 32 – CS 105 Echo Server: echo void echo(int connfd) { size_t n; char buf[MAXLINE]; while((n = read(connfd, buf, sizeof buf)) > 0) { printf("server received %d bytesn", n); write(connfd, buf, n); } } Server uses Unix I/O to read and echo text lines until EOF (end-of-file) is encountered  EOF notification caused by client calling close(clientfd)  IMPORTANT: EOF is a condition, not a particular data byte
  • 33. – 33 – CS 105 Testing Servers Using telnet The telnet program is invaluable for testing servers that transmit ASCII strings over Internet connections  Our simple echo server  Web servers  Mail servers Usage:  unix> telnet <host> <portnumber>  Creates connection with server running on <host> and listening on port <portnumber>.
  • 34. – 34 – CS 105 Testing Echo Server With telnet mallet> ./echoserver 5000 server connected to bow-vpn.cs.hmc.edu (::ffff:192.168.6.5) server received 5 bytes server connected to bow-vpn.cs.hmc.edu (::ffff:192.168.6.5) server received 8 bytes bow> telnet mallet-vpn 5000 Trying 192.168.6.1... Connected to mallet-vpn. Escape character is '^]'. 123 123 Connection closed by foreign host. bow> telnet mallet-vpn 5000 Trying 192.168.6.1... Connected to mallet-vpn. Escape character is '^]'. 456789 456789 Connection closed by foreign host. bow>
  • 35. – 35 – CS 105 Running Echo Client and Server mallet> echoserver 5000 server connected to bow-vpn.cs.hmc.edu (::ffff:192.168.6.5) server received 4 bytes server connected to bow-vpn.cs.hmc.edu (::ffff:192.168.6.5) server received 7 bytes ... bow> echoclient mallet-vpn 5000 123 123 bow> echoclient mallet-vpn 5000 456789 456789 bow>
  • 36. – 36 – CS 105 One More Important Function Real servers often want to handle multiple clients Problem: you have 3 clients. Only B wants service. You can’t really write serve(A); serve(B); serve(C); because B must wait for A to ask for service Solution: select system call  Accepts set of file descriptors you’re interested in  Tells you which ones have input waiting or are ready for output  Then you can read from/write to only the active ones  For more info, see man 2 select and Chapter 13
  • 37. – 37 – CS 105 For More Information W. Richard Stevens, “Unix Network Programming: Networking APIs: Sockets and XTI”, Volume 1, Second Edition, Prentice Hall, 1998.  THE network programming bible. Complete versions of the echo client and server (for IPV4 only) are developed in the text  IPV6 versions from slides available from class web page