Client Server Interac ons Client Server (C/S) Paradigm
• Conceptual basis for virtually all distributed applica ons • One program ini ates interac on to which another program responds • Note: “peer-to-peer” applica ons use client- server paradigm internally !
!
C/S Paradigm
! Defini on of C/S
• Client – Any applica on program – Contacts a server – Forms and sends a request – Awaits a response • Server – Usually a specialized program that offers a service – Awaits a request – Computes an answer – Issues a response Server Persistence (Always present)
A server starts execu on before interac on begins and (usually) con nues to accept requests and send responses without ever termina ng. A client is any program that makes a request and awaits a response; it (usually) terminates a er using a server a finite number of mes. Use of Ports
A server waits for requests at a well-known port that has been reserved for the service it offers. A client allocates an arbitrary, unused, nonreserved port for its communica on. C/S Model Example Client Side
• Any applica on program can become a client • Must know how to reach the server – Server’s Internet address – Server’s protocol port number d Usually easy to build Server Side
• Finds client’s location from incoming request • Can be implemented with application program or in operating system • Starts execution before requests arrive • Must ensure client is authorized • Must uphold protection rules • Must handle multiple, concurrent requests • Usually complex to design and build Server Algorithm
• Open well-known port • Wait for next client request • Create a new socket for the client • Create thread / process to handle request • Con nue with wait step Server Complexity
• Servers are usually more difficult to build than clients because: – servers must enforce all the access and protec on policies of the computer system on which they run – must protect themselves against all possible errors – Are generally concurrent (handle mul ple requests) Socket Programming Using the TCP/IP model Using Protocols
• Protocol so ware usually embedded in OS • Applica ons run outside OS • Need an Applica on Program Interface (API) to allow applica on to access protocols API
• TCP/IP standards – Describe general func onality needed – Do not give details such as func on names and arguments • Each OS free to define its own API • In prac ce: socket interface has become the de facto standard API Socket API
• Defined by U.C. Berkeley as part of BSD Unix • Adopted (with minor changes) by Microso as Windows Sockets C/Cs of Socket API
• Follows Unix’s open-read-write-close paradigm • Uses Unix’s descriptor abstrac on – First, create a socket and receive an integer descriptor – Second, call a set of func ons that specify all the details for the socket (descriptor is argument to each func on) • Once socket has been established, use read and write or equivalent func ons to transfer data • When finished, close the socket Steps to Socket Programming
¨ Crea ng a Socket:
¨ The analogy of crea ng a socket is that of reques ng a telephone line from the phone company. ¨ Iden fying/Naming a Socket: ¤ The analogy is that of assigning a phone number to the line that you requested from the phone company in step 1 or that of assigning an address to a mailbox. ¨ Connec ng from a Client to a Server
¨ Accep ng Connec ons on a Server q Communicate – Exchange Data q Close the Connec on Crea ng a Socket ¨ A socket, s, is created with the socket system call: int s = socket(domain, type, protocol)
¨ All the parameters as well as the return value are integers: ¤ domain, or address family — communica on domain in which the socket should be created. Example of address families are AF_INET (IP), AF_INET6 (IPv6), AF_UNIX (local channel, similar to pipes), AF_ISO (ISO protocols). ¤ type — type of service. This is selected according to the proper es required by the applica on: SOCK_STREAM (virtual circuit service), SOCK_DGRAM (datagram service), SOCK_RAW (direct IP service). Check with your address family to see whether a par cular service is available. ¤ protocol — indicates a specific protocol to use for the sockets opera on. This is useful in cases where some families may have more than one protocol to support a given type of service.
¨ The return value is a file descriptor (s is a small integer). Demo Code: Crea ng a Socket
/* demo-01: create a TCP/IP socket usage: demo-01 create a socket. Doesn't do much */ #include
Demo Contd. Returns fd: a file descriptor
main(int argc, char **argv) { int fd;
/* create a tcp/ip socket */ /* request the Internet address protocol */ /* and a reliable 2-way byte stream (TCP/IP) */
if ((fd = socket(AF_INET, SOCK_STREAM, 0)) < 0) { perror("cannot create socket"); return 0; }
prin ("created socket: descriptor=%d\n", fd); exit(0); } Iden fy/Name a Socket
¨ When we talk about naming a socket, we are talking about assigning a transport address to the socket (a port number in IP networking). In sockets, this opera on is called binding an address and the bind system call is used for this. Assigning an Address
• You can explicitly assign an address or allow the system to assign one. • The address is defined in a socket address structure. • Applica ons find addresses of well-known services by looking up their names in a database (e.g., the file /etc/services. E.g., p 23). • The system call for binding is:
int fx = bind(s, addr, addrlen)
• s is the socket descriptor obtained in step 1, • addr is the address structure (struct sockaddr *) • addrlen is an integer containing the address length Socket Address (struct sockaddr *)
• The address family determines what variant of the sockaddr struct * to use that contains elements that make sense for that specific communica on type. • For IP networking, we use struct sockaddr_in, which is defined in the header ne net/in.h. • This structure defines:
struct sockaddr_in { len sin_len; sa_family sin_family; in_port sin_port; struct in_add r sin_addr; Char sin_zero[8]; };
Before calling bind, we need to fill out this structure. The struct sockaddr_in The three key parts we need to set are: • sin_family The address family we used when we set up the socket. In our case, it's AF_INET.
• sin_port The port number (the transport address). • You can explicitly assign a transport address (port) or allow the opera ng system to assign one. • A client that won't be receiving incoming connec ons, lets the opera ng system pick any available port number by specifying port 0. • A server generally picks a specific number since clients will need to know a port number to connect to. struct sockaddr_in contd. sin_addr The address for this socket - machine's IP address. • For IP, your machine will have one IP address for each network interface. • E.g., if your machine has both Wi-Fi and Ethernet connec ons, machine will have two addresses, one for each interface. • Generally not important to specify a specific interface • Let the opera ng system use whatever it wants. • The special address for this is 0.0.0.0, defined by the symbolic constant INADDR_ANY. sin_zero[8] Specifies the length of that structure. • This is simply sizeof(struct sockaddr_in). • The address structure may differ based on the type of transport used
Format of a Sockaddr Structure
! Sockaddr Structure when used with TCP/Ip Demo Code: Binding a Socket /* demo-01: create a TCP/IP socket
usage: demo-01
Create a socket. Bind it to any available port. Then use getsockname to print the port. */ #include