Preview Introduction to Client-Server Model Motivation of Client-Server Model Terminologies and Concepts in Client-Server Model Connectionless vs. Connection-Oriented Stateless vs. Stateful Server Identify a client by Stateful Server Server as a Client COSC350, System Software, Fall 2015 1
Motivation of Client-Server Model The fundamental motivation for the client-server paradigm arises from the problem rendezvous. A sender will send a message when it is ready to send. But receiver does not know when sender send a message to the receiver. The client-server model solves the rendezvous problem in a straightforward way. In any pair of communicating applications, one side must start execution and wait (indefinitely) for the other side to contact it. COSC350, System Software, Fall 2015 2
Terminologies in Client-Server Model Client an application that initiates peerto peer communication. Each time client executes, it contacts a server, send a request, and awaits a response. Server any program that waits for incoming communication requests from a client. The server receives a client s request, performs the necessary computation, and returns the result to the client. COSC350, System Software, Fall 2015 3
Terminologies in Client-Server Model A server contains code that handles many issues that can be grouped under security: Authentication verifying the identity of the client Authorization check the permission for the client to access the service Data security guarantee that data is not unintentionally revealed Privacy keep information from unauthorized access Protection guarantee that network application cannot abuse system resource COSC350, System Software, Fall 2015 4
Connectionless vs. Connection-Oriented When programmer designs client-server application software, they must choose types of interaction. Connectionless use User Datagram Protocol (UDP) Connection-Oriented use Transmission Control Protocol (TCP) or use Stream Control Transmission Protocol (SCTP) COSC350, System Software, Fall 2015 5
Connectionless vs. Connection-Oriented Are you getting all of this? Who Cares? Just Send it faster UDP: Connectionless COSC350, System Software, Fall 2015 6
Connectionless vs. Connection-Oriented Are you getting all of this? OK! I am sending again I got it! Can you send again? Slow down! TCP: Connection- Oriented COSC350, System Software, Fall 2015 7
Connectionless vs. Connection-Oriented TCP provide full reliability by taking care of all transmission problems: Verifies data arrives- if not, retransmit segment. Error checking by checking checksum, guarantee error free transmission. TCP uses sequence number to ensure the data arrives in order and eliminate duplicate copy. TCP provides flow control to ensure that the sender does not transmit data faster than receiver can consume it. COSC350, System Software, Fall 2015 8
Connectionless vs. Connection-Oriented UDP do not have any guarantees about reliable delivery When a client sends a request, the request may be lost, duplicated, delayed or delivered out of order. A response from the server to client also might be lost, duplicated, delayed or delivered out of order. If a programmer use UDP for client-server application, application software itself must contain code to take care of such errors. COSC350, System Software, Fall 2015 9
Connectionless vs. Connection-Oriented Program only use UDP if the application Protocol handles reliability, application requires hardware broadcast or multicast, or the overhead for reliability is not needed. UDP is commonly used for streaming audio and video. Streaming media such as Windows Media audio files (.WMA), Real Player (.RM), and others use UDP because it offers speed! The reason UDP is faster than TCP is because there is no form of flow control or error correction. COSC350, System Software, Fall 2015 10
Stateless Vs. Stateful Server A stateless server is a server that treats each request as an independent transaction that is unrelated to any previous request. The motivation of a stateless server lies in protocol reliability. State information in a server can become incorrect if messages are lost, duplicated, or delivered out of order. It cause server might respond incorrectly. COSC350, System Software, Fall 2015 11
Stateless Vs. Stateful Server Ex) Stateless file server Since server does not save any information regarding a client request, each request from a client must specify complete information: operation (read/write), name of file, position in the file, number of bytes to transfer, and data (for writing). COSC350, System Software, Fall 2015 12
Stateless Vs. Stateful Server A stateful server is a server which keeps data about previous requests for a service which can then be used for subsequent requests. Keeping a small amount of information can reduce the size of messages that the client and server exchange, and can allow the server to respond to requests quickly. If the client state change by error (ex. Client site support multiple IP, Client site change IP address), stateful server might send wrong response. COSC350, System Software, Fall 2015 13
Stateless Vs. Stateful Server Ex Stateful file server) A stateful file server maintains a table that holds information about each client and the file currently being accessed. Once a client begins accessing a file (read or write), the server retain information: operation, name of file, and position in the file. After initial request to read from a file, subsequent read request messages only need to contain one field: the number of bytes to read. Once client begins writing data, each subsequent write request messages only need to contain two fields: the number of bytes to write and data. COSC350, System Software, Fall 2015 14
Stateless Vs. Stateful Server Ex Stateful file server Cont.) Consider what will happen if the network duplicates a read request from a client to sateful file server. Server updates its notion of file position each time the client extract data from a file. Server will receive two copies of request since the read request is duplicated. When the first copy arrives, server extracts data from the file, updates the file position and returns the result to client. When the second copy arrives, server extracts data again from the file, updates the file position again and returns the result to the client. Client will receive two different results for one request. Client might discard it or it may reports an error since it received two different results for one request. COSC350, System Software, Fall 2015 15
Stateless Vs. Stateful Server Client A client request read a file to a file server Send read request with client s full information Return a result + a handle to the Client Stateful file Server Create an entry in the state table for the client and save information. Create a handle for the client A client receive two data for one request. Send subsequent requests with the handle. This request is duplicated in the network. Return a result to the Client Return a result to the Client Server receive the first copy of duplicated request. Server extracts data from the file, updates the file position Server receive the second copy of duplicated request. Server extracts data from the file, updates the file position COSC350, System Software, Fall 2015 16
Identify a Client by Stateful Server Two general approaches to identify clients: Endpoint, Handle Endpoints The sever ask the underlying transport protocol to provide identifying information when a request arrives (IP address and Port number used by client). Then, the server uses the endpoint information to identify a particular slot in its state table. Unfortunately, endpoint information can change (ex. A client need to open a new TCP connection due to a network failure). COSC350, System Software, Fall 2015 17
Identify a Client by Stateful Server Two general approaches to identify clients: Endpoint, Handle Handle When the client first sends a request, the client must specify complete information. The server allocates an entry in its state table, and generates a short identifier for the entry called handle which is small integer value. The sever send the handle to the client for use in subsequent request. COSC350, System Software, Fall 2015 18
Identify a Client by Stateful Server Client send request with client s full information Server Send the handle to Client Create an entry in the state table for the client and save information. Create a handle for the client Send subsequent requests with the handle COSC350, System Software, Fall 2015 19
Server as a Client Internet Time Server File Server A Client COSC350, System Software, Fall 2015 20
Server as a Client Programs do not always fit exactly into the definition of client or server. A server can be a client of other server. For example, a file server is currently running on a system without time-of-day clock. Server need to obtain the time of day so it can stamp files with the time of access. The server can get the time of day information from time server. COSC350, System Software, Fall 2015 21
Client/Server Program #include <stdio.h> #include <stdlib.h> #include <strings.h> #include <sys/types.h> #include <sys/socket.h> #include <sys/time.h> #include <netinet/in.h> #include <netdb.h> #include <time.h> #define SA struct sockaddr /* generic socket address */ #define LISTENQ 1024 /* 2nd argument to listen() */ #define MAXLINE 4096 /* max text line length */ COSC 350 System Software, Fall2015 22
int main() { int len, listenfd, connfd; /* for server and client address */ struct sockaddr_in servaddr, cliaddr; char buff[maxline]; /* for sending message to client */ time_t CurrTime; /*create a socket for server using IPv4 with TCP */ listenfd = socket(af_inet, SOCK_STREAM, 0); /*clear struct sokaddr_in for server */ bzero(&servaddr, sizeof(servaddr)); /* use IPv4 */ servaddr.sin_family = AF_INET; /* allow clients to connect using any one of the host's IP addresses */ servaddr.sin_addr.s_addr = htonl(inaddr_any); /* set daytime server port as 1313. htons function format port number to short*/ servaddr.sin_port = htons(1313); } /* bind a space for client request */ bind(listenfd, (SA *) &servaddr, sizeof(servaddr)); /* waiting for client request */ listen(listenfd, LISTENQ); /* seqential server need queue */ while(1) { /* make a space for client address */ len = sizeof(cliaddr); /* accept connection from a client */ connfd = accept(listenfd, (SA *) &cliaddr, &len); /* issue current time as Unix time */ CurrTime = time(null); /* convert Unix time to formated output and a save in buffer */ snprintf(buff, sizeof(buff), "%.24s\r\n", ctime(&currtime)); /* send message to client */ write(connfd, buff, strlen(buff)); close(connfd); } COSC 350 System Software, Fall2015 23
void err_sys(char *); int main(int argc, char **argv) { int sockfd, n; /*socket file descriptor */ char recvline[maxline + 1]; struct sockaddr_in servaddr;/*server address */ if (argc!= 2) err_sys("argument error"); /*create a socket to receive data from server */ if ( (sockfd = socket(af_inet, SOCK_STREAM, 0)) < 0) err_sys("socket error"); /* shall place n zero-valued bytes in the area pointed to by serveraddr */ bzero(&servaddr, sizeof(servaddr)); /* server using IPv4 */ servaddr.sin_family = AF_INET; /* port number used by server. htons function format port number to short*/ servaddr.sin_port = htons(1313); /* converts the character string argv[i] into a network address structure /* in the IPv4 address family, then copies to servaddr.sin_addr. */ if (inet_pton(af_inet, argv[1], &servaddr.sin_addr) <= 0) err_sys("inet_pton error"); /*try to connect to server */ if (connect(sockfd, (SA *) &servaddr, sizeof(servaddr)) < 0) err_sys("connect error"); /* receive information from the daytime server*/ while ( (n = read(sockfd, recvline, MAXLINE)) > 0) { recvline[n] = 0; /* null terminate */ if (fputs(recvline, stdout) == EOF) err_sys("fputs error"); } if (n < 0) err_sys("read error"); exit(0); } void err_sys(char *str) { printf ("%s \n",str); exit (1); } COSC 350 System Software, Fall2015 24
client server socket() bind() listen() socket() connect() Connection established by TCP three-way handshake accept() No Read() write() Ye Read() s Welcome message + ask for game Yes or No write() read() Shuffle Card Display Five Card write() Send second chance cards info write() read() Read() What is on hand Display Final five cards + what is on hand write() close() close() COSC 350 System Software, Fall2015 25