CS计算机代考程序代写 FTP cache data structure Java algorithm dns 2.1

2.1
. Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
Chapter 2
Application Layer

Chapter 2: Outline
2.2
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
2.1 INTRODUCTION
2.2 CLIENT-SERVER PARADIGM
2.3 STANDARDAPPLICATIONS
2.4 PEER-TO-PEER PARADIGM
2.5 SOCKET-INTERFACE PROGRAMMING

Chapter 2: Objective
2.3
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
We introduce the nature of services provided by the Internet: the client-server paradigm and the peer-to-peer paradigm.
We discuss the concept of the client-server paradigm.
We discuss some predefined or standard applications based on the client-server paradigm such as surfing the Web, file transfer, e-mail, and so on.
We show how a new application process can be created in the client-server paradigm by writing two programs in the C language.

2

1 INTRODUCTION
• The application layer provides services to the user.
• Communication is provided using a logical
connection.
• Which means that the two application layers assume that there is an imaginary direct connection through which they can send and receive messages.
• Figure 2.1 in the next slide shows the idea behind this logical connection.
2.4
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.

Figure 2.1: Logical connection at the application layer
2.5
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.

2.1.1 Providing Services
• The Internet to provide service to users.
• Application layer as interface and platform to new application & application protocols to be easily added to the Internet
• Growth of Internet and application protocols.
• today Internet and application protocols are being added constantly.
2.6
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.

2.1.1 Providing Services (Cont.)
2.7
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
Standard and Nonstandard Protocols Standard Application-Layer Protocols
Nonstandard Application-Layer Protocols

2.1.2 Application-Layer Paradigm
• Internet requires two processes on two application programs to interact with each other.
• Source and destination interaction.
• Two programs need to send messages to each
other through the Internet infrastructure.
• Require to discussed relationship between these
programs.
• Should both application programs be able to
request services and provide services, or should the application programs just do one or the other?
2.8
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.

2.1.2 Application-Layer Paradigm (cont)
2.9
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
Traditional Paradigm: Client-Server New Paradigm: Peer-to-Peer Mixed Paradigm

Figure 2.2: Example of a client-server paradigm
2.10
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.

Figure 2.3: Example of a peer-to-peer paradigm
2.11
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.

2

2 CLIENT

SERVER PARADIGM
In this paradigm:
• communication at the application layer is between two running application programs called processes: a client and a server.
• A client is a running program that initializes the communication by sending a request;
• A server is another application program that waits for a request from a client.
2.12
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.

2.2.1 Application Programming Interface
• A computer language has
• a set of instructions for mathematical operations,
• a set of instructions for string manipulation,
• a set of instructions for input/ output access, and so
on.
• If we need a process to be able to communicate
with another process, we need
• a new set of instructions to tell the lowest four layers
of the TCP/IP suite to open the connection.
• Send and Receive data from the other end, and
• Close the connection.
• A set of instructions of this kind is normally referred
to as Application Programming Interface (API).
2.13
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.

2.2.1 Application Programming Interface (continued)
 Sockets
 Socket Addresses: combination of an IP address and a port number
 Finding Socket Addresses Server Site
2.14
Client Site
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.

Figure 2.4: Position of the socket interface
2.15
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.

Figure 2.5: A Sockets used like any other sources and sinks
2.16
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.

Figure 2.6: Use of sockets in process-to-process communication
2.17
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.

Figure 2.7: A socket address
2.18
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
• A socket is one endpoint of a two-way communication link between two programs running on the network
• A socket is bound to a port number so that the TCP layer can identify the application that data is destined to be sent to
• An endpoint = Socket is a combination of an IP address and a port number

Example 2.1 Analogy – telephone & socket
• We can find a two-level address in telephone communication.
• A telephone number can define an organization, and an extension can define a specific connection in that organization.
• The telephone number in this case is similar to the IP address, which defines the whole organization;
• the extension is similar to the port number, which defines the particular connection.
2.19
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.

2.2.2 Using Services of Transport Layer
2.20
 UDP Protocol
 TCP Protocol
 SCTP Protocol
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
• A pair of processes provide services to the users of the Internet, human or programs.
• A pair of processes, however, need to use the services provided by the transport layer for communication because there is no physical communication at the application layer.
• There are three common transport layer protocols in the TCP/IP suite: UDP, TCP, and SCTP.



2
APPLICATIONS
3 STANDARD CLIENT
SERVER
2.21
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
• During the lifetime of the Internet, several application programs have been developed.
• We do not have to redefine them, but we need to understand what they do.
• For each application, we also need to know the options available to us.
• The study of these applications can help us to create customized applications in the future.

2.3.1 World Wide Web and HTTP
• In this section, we first introduce the World Wide Web (abbreviated WWW or Web).
• We then discuss the Hyper Text Transfer Protocol (HTTP), the most common client-server application program used in relation to the Web.
2.22
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.

2.23
2.3.1 (continued)
 World Wide Web  Architecture
 Uniform Resource Locator (URL)  Web Documents
 HyperText Transfer Protocol (HTTP)  Nonpersistent versus Persistent Connections
 Message Formats  Conditional Request  Cookies
 Web Caching: Proxy Server  Proxy Server Location
 Cache Update
 HTTP Security
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.

Example 2.2
Assume we need to retrieve a scientific document that contains one reference to another text file and one reference to a large image. Figure 2.8 shows the situation.
• The main document and the image are stored in two separate files in the same site (file A and file B);
• The referenced text file is stored in another site (file C).
• So we are dealing with three different files, we need three
transactions if we want to see/get the whole document.
2.24
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.

Figure 2.8: HTTP Example 2.2 (Retrieving two files and one image)
2.25
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.

Figure 2.9: Browser
2.26
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.

Example 2.3
2.27
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
The URL http://www.mhhe.com/engcs/compsci/forouzan/ defines the web page related to one of the computer in the McGraw-Hill company (the three letters www are part of the host name and are added to the commercial host).
The path is compsci/forouzan/, which defines Forouzan’s web page under the directory compsci (computer science).

Example 2.4
Figure 2.10 shows an example of a nonpersistent connection
• The client needs to access a file that contains one link to an image. The text file and image are located on the same server.
• Here we need two connections.
• For each connection, TCP requires at least three handshake
messages( SYN, SYN-ACK, ACK) to establish the connection, but the request can be sent with the third one. After the connection is established, the object can be transferred.
• After receiving an object, another three handshake messages( FIN, FIN-ACK, ACK) are needed to terminate the connection.
2.28
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.

Figure 2.10: Example 2.4 (HTTP non-persistent connection)
2.29
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.

Example 2.5
2.30
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
Figure 2.11 shows the same scenario as in Example 2.4, but using a persistent connection.
• Only one connection establishment and connection termination is used, but the request for the image is sent separately.

Figure 2.11: Example 2.5 (HTTP persistent connection)
2.31
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.

Figure 2.12: Formats of the request and response messages
2.32
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.

Table 2.1: Methods
2.33
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.

Table 2.2: Request Header Names
2.34
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.

Table 2.3: Response Header Names
2.35

Figure 2.13: Example 2.6
2.36
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.

Figure 2.14: Post on the server. We use the PUT method. Example 2.7
2.37
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.

Example 2.8
The following shows how a client imposes/forces the modification data and time condition on a request.
The status line in the response shows the file was not modified after the defined point in time. The body of the response message is also empty.
2.38
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.

Example 2.9: Use of cookies
2.39
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
Figure 2.15 shows a scenario in which an electronic store
can benefit from the use of cookies.
• Assume a shopper wants to buy a toy from an electronic store
named BestToys.
• The shopper browser (client) sends a request to the BestToys
server.
• The server creates an empty shopping cart (a list) for the client and
assigns an ID to the cart (for example, 12343).
• The server then sends a response message, which contains the
images of all toys available, with a link under each toy that selects
the toy if it is being clicked.
• This response message also includes the Set-Cookie header line
whose value is 12343.
• The client displays the images and stores the cookie value in a file
named BestToys.

Figure 2.15: Example 2.9
2.40
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.

Example 2.10: Use of Proxy Server
2.41
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
Figure 2.16 shows an example of a use of a proxy server in a local network, such as the network on a campus or in a
company.
• The proxy server is installed in the local network.
• When an HTTP request is created by any of the clients
(browsers), the request is first directed to the proxy server.
• If the proxy server already has the corresponding web page, it
sends the response to the client.
• Otherwise, the proxy server acts as a client and sends the
request to the web server in the Internet.
• When the response is returned, the proxy server makes a copy
and stores it in its cache before sending it to the requesting client.

Figure 2.16: Example of a proxy server
2.42
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.

2.3.2 FTP
File Transfer Protocol (FTP) is the standard protocol provided by TCP/IP for copying a file from one host to another. Although transferring files from one system to another seems simple and straightforward, some problems must be dealt with first. For example, two systems may use different file name conventions. Two systems may have different ways to represent data. All of these problems have been solved by FTP in a very simple and elegant approach.
2.43
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.

2.3.2 (continued)
2.44
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
Lifetimes of Two Connections  Control Connection
 Data Connection
 Communication over Data Connection
 File Transfer

Figure 2.17: FTP
Control connection
Data connection
2.45
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.

2.3.4 TELNET
2.46
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
• A server program can provide a specific service to its corresponding client program. However, it is impossible to have a client/server pair for each type of service we need.
• Another solution is to have a specific client/server program for a set of common scenarios, but to have some generic client/server programs that allow a user on the client site to log into the computer at the server site and use the services available there.
• We refer to these generic client/server pairs as remote logging applications. One of the original remote logging protocols is TELNET.
• However, because of serious security concerns when using Telnet over an open network such as the Internet, its use for this purpose has been waned against & significantly in favour of SSH2.

2.3.5 Secure Shell (SSH)
2.47
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
• Secure Shell (SSH) is a cryptographic network protocol for operating network services securely over an unsecured network.
• SSH provides a secure channel over an unsecured network in a client- server architecture, connecting an SSH client application with an SSH server.
• The most visible application of the protocol is for access to shell accounts on Unix, Linux, windows and other O.S
• SSH was designed as a replacement for TELNET and for unsecured remote shell protocols such as the Berkeley rlogin, rsh, and rexec protocols.
• There are two versions of SSH: SSH-1 and SSH-2, which are totally incompatible. The first version, SSH-1, is now deprecated because of security flaws in it. In this section, we discuss only SSH-2.

Figure 2.33: Port Forwarding
2.48
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.

2.3.6 Domain Name System (DNS)
• To identify an entity, TCP/IP protocols use the IP address, which uniquely identifies the connection of a host to the Internet.
• However, people prefer to use names instead of numeric addresses.
• Therefore, the Internet needs to have a directory system that can map a name to an address.
• This is analogous to the telephone network.
• A telephone network is designed to use telephone numbers, not names. People can either keep a private file to map a name to the corresponding telephone number or can call the telephone directory to do so.
2.49
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.

2.3.6 (continued)
2.50
 Name Space
 Domain Name Space
 Domain
 Distribution of Name Space  Zone
 Root Server
 DNS in the Internet  Generic Domains
 Country Domains
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.

2.3.1 (continued)
 Resolution
 Recursive Resolution  Iterative Resolution  Caching
Resource Records DNS Messages
 Encapsulation
 Registrars
 DDNS Security of DNS
2.51
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.

Figure 2.35: Purpose of DNS Host-name resolution
2.52
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.

Figure 2.36: Domain name space
2.53
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.

Figure 2.37: Domain names and labels
2.54
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.

Figure 2.39: Hierarchy of name servers
2.55
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.

Figure 2.41: Generic domains
Table 2.12: Generic domain labels
2.56
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.

Figure 2.42: Country domains
2.57
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.

Figure 2.43: Recursive Host-name resolution
7
1
8
6
4
6
9
170
2.58
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.

3
1
2
4
8
67
5
2.59
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
6
9
170
Figure 2.44: Iterative Host-name resolution

Table 2.13: DNS types
2.60
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.

Figure 2.45: DNS message
Note: Both queries and responses have the same general format, containing up to five individual sections carrying information.
The query message:
• contains only the Question section !
While the response messages:
• include the Question, Answer section, Authoritative and Additional sections.
2.61
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.

Example 2.14
In UNIX and Windows, the nslookup utility can be used to retrieve address/name mapping. The following shows how we can retrieve an address when the domain name is given.
2.62
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.

2

5 SOCKET

INTERFACE PROGRAMMING
2.63
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
In this part of lecture, we will see some examples how to write some simple client-server programs using C, a procedural programming language.
• Here in this lecture we have chose the C language;
• In the last lecture (chapter 11), we expand this idea in Java, which provides a more compact version.

2.5.1 Socket Interface in C
• First we shall see how this interface in C language.
• Then understand socket interface and the role of a
socket in communication.
• The socket has no buffer to store data to be sent or received. It is capable of neither sending nor receiving data.
• The socket just acts as a reference or a label. The buffers and necessary variables are created inside the operating system.
2.64
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.

2.65
2.5.1 (continued)
 Data Structure for Socket  Header Files
 Communication Using UDP
 Sockets Used for UDP
 Communication Flow Diagram  Programming Examples
 Communication Using TCP  Sockets Used in TCP
 Communication Flow Diagram
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.

Figure 2.58: Socket data structure
2.66
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.

Figure 2.59: Sockets for UDP communication
2.67
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.

Figure 2.60: Flow diagram for iterative UDP communication
2.68
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.

UDP ECHO CLIENT SERVER
AIM: To write a program for UDP echo client server.
SERVER ALGORITHM:
STEP 1: Start
STEP 2: Declare the variables for the socket
STEP 3: Specify the family, protocol, IP address and port number STEP 4: Create a socket using socket() function
STEP 5: Bind the IP address and Port number
STEP 6: Listen and accept the client’s request for the connection STEP 7: Read and Display the client’s message
STEP 8: Stop
CLIENT ALGORITHM:
STEP 1: Start
STEP 2: Declare the variables for the socket
STEP 3: Specify the family, protocol, IP address and port number STEP 4: Create a socket using socket() function
STEP 5: Call the connect() function
STEP 6: Read the input message
STEP 7: Send the input message to the server
STEP 8: Display the server’s echo
STEP 9: Close the socket
STEP 10: Stop
2.69
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.

SOURCE SERVER CODE:
#include #include #include #include #include #include #include #include #define MAXLINE 1024
int main(int argc,char **argv)
{int sockfd;
int n;
socklen_t len; char msg[1024];
2.70
Reference: http://www.sourcecodesolutions.in/2010/09/udp-echo-client-server.html
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
Echo server program using UDP
struct sockaddr_in servaddr,cliaddr; sockfd=socket(AF_INET,SOCK_DGRAM,0); bzero(&servaddr,sizeof(servaddr)); servaddr.sin_family=AF_INET; servaddr.sin_addr.s_addr=INADDR_ANY; servaddr.sin_port=htons(5035);
printf(“\n\n Binded”);
bind(sockfd,(struct sockaddr*)&servaddr,sizeof(servaddr)); printf(“\n\n Listening…”);
for(;;)
{
printf(“\n “);
len=sizeof(cliaddr); n=recvfrom(sockfd,msg,MAXLINE,0,(struct
sockaddr*)&cliaddr,&len);
printf(“\n Client’s Message : %s\n”,msg); if(n<6) perror("send error"); } sendto(sockfd,msg,n,0,(struct sockaddr*)&cliaddr,len); return 0; } Echo client program using UDP SOURCE CLIENT CODE: #include #include #include #include #include #include #include #include
#define MAXLINE 1024
int main(int argc,char* argv[])
{int sockfd;
int n;
socklen_t len;
char sendline[1024],recvline[1024]; struct sockaddr_in servaddr; strcpy(sendline,””);
printf(“\n Enter the message : “); scanf(“%s”,sendline);
sockfd=socket(AF_INET,SOCK_DGRAM,0); bzero(&servaddr,sizeof(servaddr)); servaddr.sin_family=AF_INET; servaddr.sin_addr.s_addr=inet_addr(“127.0.0.1”); servaddr.sin_port=htons(5035);
connect(sockfd,(struct sockaddr*)&servaddr,sizeof(servaddr)); len=sizeof(servaddr); sendto(sockfd,sendline,MAXLINE,0,(struct sockaddr*)&servaddr,len); n=recvfrom(sockfd,recvline,MAXLINE,0,NULL,NULL );
recvline[n]=0;
printf(“\n Server’s Echo : %s\n\n”,recvline); return 0;
}
2.71
Reference: http://www.sourcecodesolutions.in/2010/09/udp-echo-client-server.html
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.

Figure 2.61: Sockets used in TCP communication
2
Create
5
Create
2.72
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.

Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.

Please check out the other program: Socket program written in C using TCP transport protocol to chat between tcpchatserver.c & tcpchatclient.c
2.74
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
http://mcalabprogram.blogspot.com.au/2012/01/tcp-sockets-chat-applicationserver.html

Please check out the other program: Socket program written in C using TCP transport protocol to get Date & Timestamp from server TStmpserver.c & TStmpclient.c
2.75
Reference: https://www.thegeekstuff.com/2011/12/c-socket-programming/
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.

Chapter 2: Summary
 Applications in the Internet are designed using either a client-server paradigm or a peer-to-peer paradigm.
 The World Wide Web (WWW) is a repository of information linked together from points all over the world. Hypertext and hypermedia documents are linked to one another through pointers. The HyperText Transfer Protocol (HTTP) is the main protocol used to access data on the World Wide Web (WWW).
 File Transfer Protocol (FTP) is a TCP/IP client-server application for copying files from one host to another. FTP requires two connections for data transfer: a control connection and a data connection.
 TELNET is a client-server application that allows a user to log into a remote machine, giving the user access to the remote system.
 The Domain Name System (DNS) is a client-server application that identifies each host on the Internet with a unique name. DNS organizes the name space in a hierarchical structure to decentralize the responsibilities involved in naming.
2.76
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.