CS计算机代考程序代写 FTP dns DHCP 1/21/2021

1/21/2021
1
CS 118 Discussion Week 3: The Application Layer and BSD Sockets
Slides by Eric Newberry January 22, 2021

Application
Transport
Network
Link
HTTP SMTP DNS
TCP UDP
1/21/2021
2

Content Distribution Networks
• Store content at geographically distributed locations
• Through the magic of DNS, clients will find the geographically closest
location
• Idea is to reduce both latency and load on a small set of servers
• By using closer content and more servers
• Content providers pay CDN companies to serve their data
1/21/2021 3

Content Distribution Networks
1/21/2021
4
https://commons.wikimedia.org/wiki/File:NCDN_-_CDN.png

Video Streaming with DASH
• Split video file into chunks and encode at different bit rates • Smaller bit ratesless bandwidth required to transfer
• Replicate various bit rate chunks on CDN
• Client will use a manifest file to identify different chunks at different bit
rates
• Client with periodically estimate available bandwidth to reach server
• In this case CDN nodes are the server
• Retrieve video at maximum bit rate that estimate says have enough
• Can also change which CDN node retrieving from based upon geographical and available bandwidth considerations
bandwidth for
1/21/2021
5

UDP
• User Datagram Protocol
• The simpler of the two, provides less “features” than TCP
• Provides the bare minimum needed to get packets to the receiver • Packets *not* guaranteed to be received by remote app in order • Packets *not* guaranteed to get to remote host at all
• No connectionjust send data
1/21/2021 6

TCP
• Transmission Control Protocol
• Data guaranteed to be delivered and delivered *in order*
• Provides “congestion control” to allow it to “play nice” with other communication streams on the network
• Need to establish connection with a specific remote host
1/21/2021 7

Should I use TCP or UDP?
• Still no guarantee of delay or throughput in either TCP or UDP
• Discuss: What types of applications would benefit the most from
using each transport protocol?
1/21/2021 8

Which applications use which protocols?
• TCP
• HTTP (current versions)
• SMTP, IMAP
• FTP (file transfer) • SSH (remote shell)
• UDP
• DNS (can also use TCP), DHCP (network configuration)
• Streaming audio and video (including voice-over-IP/“VoIP”)
• HTTP/3 and QUIC
• Use congestion control and reliability at application layer (if needed)!
1/21/2021
9

1/21/2021
10
Socket Programming in C

“Where” are we programming?
• “Clients” and “servers” are programs at the
application layer
• The transport layer is responsible for providing communication services for the application layer
• Basic transport protocols • TCP
• UDP
Client
Socket API
Server
Socket API
Transport
Transport
Network
Network
Data Link
Data Link
Physical
Physical
Slide by Seungbae Kim, UCLA

What is socket programming?
• From Wikipedia: “A network socket is an endpoint of an inter-process communication flow across a computer network”
• In other words, sockets are APIs between applications and transport/network services
Slide by Seungbae Kim, UCLA

TCP Socket Programming
• Create service
• Establish a TCP connection • Send and receive data
• Close TCP connection
Slide by Seungbae Kim, UCLA

TCP Socket: Service Setup
TCP Client TCP Server
Slide by Seungbae Kim, UCLA

TCP Socket: Service Setup
TCP Client TCP Server
socket( )
Slide by Seungbae Kim, UCLA

TCP Socket: Service Setup
TCP Client TCP Server
socket( )
bind( )
Slide by Seungbae Kim, UCLA

TCP Socket: Service Setup
TCP Client
TCP Server
socket( )
bind( )
listen( )
Slide by Seungbae Kim, UCLA

TCP Socket: Service Setup
TCP Client
TCP Server
socket( )
bind( )
listen( )
accept( )
Slide by Seungbae Kim, UCLA

TCP Socket: Service Setup
TCP Client
TCP Server
socket( )
bind( )
listen( )
accept( )
Slide by Seungbae Kim, UCLA
blocked until connection from client

TCP Socket: Establish Connection
TCP Client
socket( )
TCP Server
socket( )
bind( )
listen( )
accept( )
Slide by Seungbae Kim, UCLA
blocked until connection from client

TCP Socket: Establish Connection
TCP Client
socket( )
connect( )
TCP Server
socket( )
bind( )
listen( )
accept( )
Slide by Seungbae Kim, UCLA
blocked until connection from client

TCP Socket: Send/Receive Data
TCP Client
socket( )
connect( )
write( )
TCP Server
socket( )
bind( )
listen( )
accept( )
blocked until connection from client
Slide by Seungbae Kim, UCLA

TCP Socket: Send/Receive Data
TCP Client
socket( )
connect( )
write( )
TCP Server
socket( )
bind( )
listen( )
accept( )
read( )
blocked until connection from client
Data
Slide by Seungbae Kim, UCLA

TCP Socket: Send/Receive Data
TCP Client
socket( )
connect( )
write( )
TCP Server
socket( )
bind( )
listen( )
accept( )
read( )
process data
write( )
blocked until connection from client
Data
Slide by Seungbae Kim, UCLA

TCP Socket: Send/Receive Data
TCP Client
socket( )
connect( )
write( )
TCP Server
socket( )
bind( )
listen( )
accept( )
read( )
process data
write( )
blocked until connection from client
Data
Slide by Seungbae Kim, UCLA
read( )
Data

TCP Socket: Close Connection
TCP Client
socket( )
connect( )
write( )
TCP Server
socket( )
bind( )
listen( )
accept( )
read( )
process data
write( )
blocked until connection from client
Data
Data
read( )
close( ) Slide by Seungbae Kim, UCLA

TCP Socket: Close Connection
TCP Client
socket( )
connect( )
write( )
TCP Server
socket( )
bind( )
listen( )
accept( )
read( )
process data
write( )
read( )
close( )
blocked until connection from client
Data
read( )
close( ) Slide by Seungbae Kim, UCLA
Data

Functions
• int socket(int domain, int type, int protocol);
• Creates a socket
• Returns the socket descriptor or -1 (failure). Also sets errno on failure • domain: protocol family
• PF_INET for IPv4, PF_INET6 for IPv6, PF_UNIX or PF_LOCAL for Unix socket, PF_ROUTE for routing • type: communication paradigm
• SOCK_STREAM for TCP (with PF_INET or PF_INET6)
• SOCK_DGRAM for UDP (with PF_INET or PF_INET6)
• protocol: protocol within family, which is typically set to 0
Slide by Seungbae Kim, UCLA

Functions
• int bind(int sockfd, struct sockaddr* myaddr, int addrlen); • Bind a socket to a local IP address and port number
• Returns 0 on success, returns -1 and sets errno on failure • sockfd: socket file descriptor returned by socket ( )
• myaddr: includes IP address and port number
• NOTE: sockaddr and sockaddr_in are of the same size, use sockaddr_in and convert it to sockaddr • sin_family: protocol family, e.g. AF_INET
• sin_port: port number assigned by caller
• sin_addr: IP address (0.0.0.0 binds to all)
• sin_zero: used for keeping same size as sockaddr
• addrlen: sizeof(struct sockaddr_in) Slide by Seungbae Kim, UCLA
struct sockaddr {
short sa_family;
};char sa_data[14];
struct sockaddr_in {
short sin_family;
ushort sin_port;
struct in_addr sin_addr;
};unsigned char sin_zero[8];

Functions
• int listen(int sockfd, int backlog);
• Put socket into passive state (wait for connections rather than initiating a connection)
• Returns 0 on success, returns -1 and sets errno on failure
• sockfd: socket file descriptor returned by socket( )
• backlog: the maximum number of connections this program can serve simultaneously
Slide by Seungbae Kim, UCLA

Functions
• int accept(int sockfd, struct sockaddr* client_addr, int* addrlen);
• Accepts a new connection
• Returns client socket file descriptor, returns -1 and sets errno on failure
• sockfd: socket file descriptor for server, returned by socket( )
• client_addr: IP address and port number of a client (returned from call)
• addrlen: length of address structure = pointer to int set to sizeof(struct sockaddr_in) • NOTE: client_addr and addrlen are result arguments
• i.e. The program passes empty client_addr and addrlen into the function, and the kernel will fill in these arguments with client’s information
Slide by Seungbae Kim, UCLA

More Information about accept( )
• A new socket is cloned from the listening socket • If there are no incoming connections to accept:
• Blocking mode (default): accept( ) call will block until a client connects
Slide by Seungbae Kim, UCLA

Functions
• int connect (int sockfd, struct sockaddr* server_addr, int addrlen); • Connects to another socket (server)
• Returns 0 on success, returns -1 and sets errno on failure
• sockfd: socket file descriptor — returned from socket( )
• server_addr: IP address and port number of server
• Server’s IP address and port number should be known in advance
• addrlen: sizeof(struct sockaddr_in)
Slide by Seungbae Kim, UCLA

Functions
• int write(int sockfd, char* buf, size_t nbytes);
• Writes data to a socket
• Return the number of sent bytes or -1 on failure
• sockfd: socket file descriptor from socket ( )
• buf: data buffer to send
• nbytes: the number of bytes that caller wants to send from buf
Slide by Seungbae Kim, UCLA

Functions
• int read(int sockfd, char* buf, size_t nbytes);
• Reads data from a socket
• Returns the number of bytes read or -1 on failure
• Returns 0 if socket is closed
• sockfd: socket file descriptor returned from socket ( )
• buf: data buffer to store read data in
• nbytes: the number of bytes that caller can read (usually set to buffer size)
Slide by Seungbae Kim, UCLA

Functions
• int close(int sockfd);
• Closes a socket
• Returns 0 on success, returns -1 on failure • After being closed, sockfd is no longer valid
Slide by Seungbae Kim, UCLA