Chapter 4: Server Software Design Chapter 4: Server Software Design Algorithm and issues Algorithm and issues Iterative Iterative servers a Concurrent connection-oriented Concurrent connection-oriented servers servers Singly-threaded,concurrent a Singly-threaded, concurrent servers servers a Multiprotocol servers Multiprotocol servers a Multiservice servers Multiservice servers Uniform, efficient management Uniform, efficient management of server concurrency of server concurrency Concurrent vs. Iterative The conceptual server algorithm Servers Each server follows a simple algorithm a Iterative server Process one request at a time Enters infinite lo Easier to build Accepts request Poor performance Processes request a Concurrent Handles multiple requests at one time But only suffices for only the most trivial More difficult to design and build services Better performance Connection-oriented vs Connection-oriented vs Connectionless Servers Connectionless servers Depend on the application protocol Connectionless servers n Connection-oriented servers Advantages Advantages Do not suffer from the problem of resource Ease of programming TCP provides reliability Can supply broadcast or multicast communication Disadvantages Disadvantages: Require a separate socket for each connection Can not depend on underlying transport for Overhead of 3-way handshaking TCP does not send any packets over an idle Application protocols respond for reliability connection- run out of resources
1 哈工大计算机学院 李全龙 Network Application Development Server Software Design 1 Chapter 4: Server Software Design Chapter goal: Algorithm and issues Iterative servers Concurrent, connection-oriented servers Singly-threaded, concurrent servers Multiprotocol servers Multiservice servers Uniform, efficient management of server concurrency 哈工大计算机学院 李全龙 Network Application Development Server Software Design 2 Chapter 4: Server Software Design Chapter goal: Algorithm and issues Iterative servers Concurrent, connection-oriented servers Singly-threaded, concurrent servers Multiprotocol servers Multiservice servers Uniform, efficient management of server concurrency 哈工大计算机学院 李全龙 Network Application Development Server Software Design 3 The conceptual server algorithm Each server follows a simple algorithm: Creates a socket Binds port Enters infinite loop Accepts request Processes request Responses But only suffices for only the most trivial services 哈工大计算机学院 李全龙 Network Application Development Server Software Design 4 Concurrent vs. Iterative Servers Iterative server Process one request at a time Unnecessary delays Easier to build Poor performance Concurrent server Handles multiple requests at one time More difficult to design and build Better performance 哈工大计算机学院 李全龙 Network Application Development Server Software Design 5 Connection-oriented vs. Connectionless Servers Depend on the application protocol Connection-oriented servers Advantages: Ease of programming TCP provides reliability Disadvantages: Require a separate socket for each connection Overhead of 3-way handshaking TCP does not send any packets over an idle connection- run out of resources 哈工大计算机学院 李全龙 Network Application Development Server Software Design 6 Connection-oriented vs. Connectionless Servers Connectionless servers Advantages: Do not suffer from the problem of resource depletion Can supply broadcast or multicast communication Disadvantages: Can not depend on underlying transport for reliable delivery Application protocols respond for reliability
Optimizing stateless servers Optimizing stateless servers Consider a connectionless server for file Table of information about files clients are using We find hash(IP addr, port) Overhead of opening and closing file Clients may read only a dozen bytes in each Buffer for file x Clients tend to read files sequentially starting at byte 512 To optimize the performance Offset: 1024 Maintain a small table of file information Buffer for file y State information starting a Optimizing stateless servers Four basic types of servers Improve performance without any penalty? Iterative connectionless Even a small amount of information can cause a a Iterative connection-oriented server to perform badly when machines, client a Concurrent connectionless programs, or networks fail Frequent crashes can cause one client to dominate a Concurrent connection-oriented table by filling it with entries that will never b Iterative servers suffice only for the trivial application protocols A programmer must be extremely careful when Iterative servers work best with simple optimizing a stateless server services accessed by a connectionless access protocol Iterative connection-oriented Binding to a well-known address server algorithm by using INADDR_ANy Creating a socket and bind to the well-know s binduses structure sockaddr in address for the service being offered Place the socket in passive mode, making it ready Selecting a specific IP address at which a for use by a server server will accept connections can cause Accept the next connection request from the difficult ocket, and obtain a new socket for the connection. repeatedly receive a request from the clien especially for gateways(routers) ormulate a response, and send a reply back to the a Special constant: INADDR_ANy client according to application-protocol. Wildcard address When finished with a particular client, close the a Servers use INADDR ANy instead a connection and return to step 3 to accept a new onnection specific IP address
2 哈工大计算机学院 李全龙 Network Application Development Server Software Design 7 Optimizing stateless servers Consider a connectionless server for file reading We find: Overhead of opening and closing file Clients may read only a dozen bytes in each request Clients tend to read files sequentially To optimize the performance Maintain a small table of file information State information 哈工大计算机学院 李全龙 Network Application Development Server Software Design 8 Optimizing stateless servers Filename:X Offset:512 Buffer pointer: Filename:Y Offset:1024 Buffer pointer: Table of information about files clients are using hash(IP addr,port) Buffer for file X starting at byte 512 Buffer for file Y starting at byte 1024 哈工大计算机学院 李全龙 Network Application Development Server Software Design 9 Optimizing stateless servers Improve performance without any penalty? Even a small amount of information can cause a server to perform badly when machines, client programs, or networks fail Frequent crashes can cause one client to dominate the table by filling it with entries that will never be used A programmer must be extremely careful when optimizing a stateless server 哈工大计算机学院 李全龙 Network Application Development Server Software Design 10 Four basic types of servers Iterative connectionless Iterative connection-oriented Concurrent connectionless Concurrent connection-oriented Iterative servers suffice only for the trivial application protocols Iterative servers work best with simple services accessed by a connectionless access protocol 哈工大计算机学院 李全龙 Network Application Development Server Software Design 11 Iterative connection-oriented server algorithm 1. Creating a socket and bind to the well-known address for the service being offered. 2. Place the socket in passive mode, making it ready for use by a server. 3. Accept the next connection request from the socket, and obtain a new socket for the connection. 4. Repeatedly receive a request from the client, formulate a response, and send a reply back to the client according to application-protocol. 5. When finished with a particular client, close the connection and return to step 3 to accept a new connection. 哈工大计算机学院 李全龙 Network Application Development Server Software Design 12 Binding to a well-known address by using INADDR_ANY bind uses structure sockaddr_in Selecting a specific IP address at which a server will accept connections can cause difficulty especially for gateways (routers) Special constant: INADDR_ANY Wildcard address Servers use INADDR_ANY instead a specific IP address
Iterative connectionless server Forming a reply address in a algorithm connectionless server Creating a socket and bind to the well a Can not use connect known address for the service being Uses an unconnected socket Uses sendto to send datagram 2. Repeatedly receive the next request from the client, formulate a response and send a reply back to the clien mber of by according to application-protocol contains data contains the control options ( endpoint addr Forming a reply address in a Concurrent connectionless server connectionless server algorithm Easy way to obtain address of client Master 1: Creating g socket and bind to the well- From source address found in request next d to handle the response. t and create a new slave place next datagram Slave 3: Exit(i. e. a slave thread terminates after control options Few connectionless servers have concurrent implementation Concurrent connection-oriented Apparent Concurrency using a server algorithm gle thread Ma known Cdertes for e service beia ofed Creating a socket and bind to the well-known Leaving the socket unconnected. Add socket to the list Master 2: Place the socket in passive mode, making it se select to wait for I/O on existing sockets tedly call accept to receive the next reate a new slave Id the new socket to hread to handle the response hich I/O is possible Slave 1: Receive a connection request (i. e, socket for receive request(s) k to the clie and exit. The slave Continue processing with step 2 above. read exits after ng all requests from one
3 哈工大计算机学院 李全龙 Network Application Development Server Software Design 13 Iterative connectionless server algorithm 1. Creating a socket and bind to the wellknown address for the service being offered. 2. Repeatedly receive the next request from the client, formulate a response, and send a reply back to the client according to application-protocol. 哈工大计算机学院 李全龙 Network Application Development Server Software Design 14 Forming a reply address in a connectionless server Can not use connect Uses an unconnected socket Uses sendto to send datagram retcode=sendto(socket,data,length,flags,destaddr,addrlen); unconnected socket address of a buffer that contains data to be sent number of bytes in the buffer Debugging or control options Pointer to a sockaddr_in structure that contains the endpoint address Length of the address structure 哈工大计算机学院 李全龙 Network Application Development Server Software Design 15 Forming a reply address in a connectionless server Easy way to obtain address of client From source address found in request Use recvfrom retcode=recvfrom(socket,buf,length,flags,from,fromlen); a socket to use address of a buffer place next datagram Space available in the buffer Debugging or control options Address of the second buffer place the source address Length of the source address 哈工大计算机学院 李全龙 Network Application Development Server Software Design 16 Concurrent connectionless server algorithm Master 1: Creating a socket and bind to the well- known address for the service being offered. Leaving the socket unconnected. Master 2: Repeatedly call recvfrom to receive the next request from a client, and create a new slave thread to handle the response. Slave 1: Receive a specific request upon creation as well as access to the socket. Slave 2: Form a reply according to the app- protocol and send it back to the client using sendto. Slave 3: Exit(i.e., a slave thread terminates after handling one request) Few connectionless servers have concurrent implementations. 哈工大计算机学院 李全龙 Network Application Development Server Software Design 17 Concurrent connection-oriented server algorithm Master 1: Creating a socket and bind to the well- known address for the service being offered. Leaving the socket unconnected. Master 2: Place the socket in passive mode, making it ready for use by a server. Master 3: Repeatedly call accept to receive the next request from a client, and create a new slave thread to handle the response. Slave 1: Receive a connection request (i.e., socket for the connection) upon creation. Slave 2: Interact with the client using the connection: receive request(s) and send back response(s). Slave 3: Close the connection and exit. The slave thread exits after handling all requests from one client. 哈工大计算机学院 李全龙 Network Application Development Server Software Design 18 Apparent Concurrency using a single thread 1. Creating a socket and bind to the well-known address for the service. Add socket to the list of those on which I/O is possible. 2. Use select to wait for I/O on existing sockets. 3. If original socket is ready, use accept to obtain the next connection, and add the new socket to the list of those on which I/O is possible. 4. If some socket other than the original is ready, use recv to obtain the next request, form response, and use send to send the response back to the client. 5. Continue processing with step 2 above
Chapter 4: Server Software Design Creating a Passive Socket Chapter gae a Algorithm and issues Use procedures to hide the details a Iterative servers Two high-level procedures a Concurrent connection-oriented passrveUDP servers a Singly-threaded, concurrent The low-level procedure: passivesock servers Multiprotocol servers Multiservice servers Uniform, efficient management of server concurrency Iterative Connectionless Servers passiveUDP A connectionless server calls function passiveUDPto passUDP. cpp-passiveUDP*/ create a socke CossivelDP calls passivesock to create a #include <winsock. hp connectionless socket SOCKET passivesock(const char*, const char * int): To maks it keaeyototst c bent and s rver software c passiveUDP-create a passive socket for use in a UDP The importance soffware cannot be tested while the production passiveUDP(const char * service continues to execute Using portbase makes it possible to test multiple turn passivesock(service, "udp", o versions passivesock passiveUDP void errexit(const char*,: soler sockaddr_n sinc /. an Internet endpoint address entry u_short portbase =0:/ port base, for test servers type: /socket type(SOCK-STREAM, SOCK_DGRAM)- passivesock-allocate bind a server socket using ToP or UDP nsin port hons(ntohs((u.)pse-s_port portbase: else if ((sin, inport Name of a service errexitccan't get \7s\"service entry\n, service):
4 哈工大计算机学院 李全龙 Network Application Development Server Software Design 19 Chapter 4: Server Software Design Chapter goal: Algorithm and issues Iterative servers Concurrent, connection-oriented servers Singly-threaded, concurrent servers Multiprotocol servers Multiservice servers Uniform, efficient management of server concurrency 哈工大计算机学院 李全龙 Network Application Development Server Software Design 20 Creating a Passive Socket Use procedures to hide the details Two high-level procedures: passiveUDP passiveTCP The low-level procedure: passivesock 哈工大计算机学院 李全龙 Network Application Development Server Software Design 21 Iterative Connectionless Servers A connectionless server calls function passiveUDP to create a socket passiveUDP calls passivesock to create a connectionless socket To make it easy to test client and server software, passivesock relocates all port values by adding the contents of global integer portbase The importance: If a new version of a client-server application uses the same protocol port numbers as an existing, production version, the new software cannot be tested while the production version continues to execute. Using portbase makes it possible to test multiple versions 哈工大计算机学院 李全龙 Network Application Development Server Software Design 22 passiveUDP /* passUDP.cpp - passiveUDP */ #include <winsock.h> SOCKET passivesock(const char *, const char *, int); /*------------------------------------------------------------------------ * passiveUDP - create a passive socket for use in a UDP server *------------------------------------------------------------------------ */ SOCKET passiveUDP(const char *service) { return passivesock(service, "udp", 0); } 哈工大计算机学院 李全龙 Network Application Development Server Software Design 23 passivesock /* passsock.cpp - passivesock */ #include <stdlib.h> #include <string.h> #include <winsock.h> void errexit(const char *, ...); u_short portbase = 0; /* port base, for test servers */ /*----------------------------------------------------------------------- * passivesock - allocate & bind a server socket using TCP or UDP *------------------------------------------------------------------------ */ SOCKET passivesock(const char *service, const char *transport, int qlen) Name of a service Name of protocol Length of the connection request queue (only for TCP) 哈工大计算机学院 李全龙 Network Application Development Server Software Design 24 passiveUDP { struct servent *pse; /* pointer to service information entry */ struct protoent *ppe; /* pointer to protocol information entry */ struct sockaddr_in sin;/* an Internet endpoint address */ SOCKET s; /* socket descriptor */ int type; /* socket type (SOCK_STREAM, SOCK_DGRAM)*/ memset(&sin, 0, sizeof(sin)); sin.sin_family = AF_INET; sin.sin_addr.s_addr = INADDR_ANY; /* Map service name to port number */ if ( pse = getservbyname(service, transport) ) sin.sin_port = htons(ntohs((u_short)pse->s_port) + portbase); else if ( (sin.sin_port = htons((u_short)atoi(service))) == 0 ) errexit("can't get \"%s\" service entry\n", service);
Thread Structure for Iterative pass/veUDP Connectionless Server A Use protocol to choose a socket type"/ Server type SOcK-DGRAM plication thread GetLastErrorOk port: Zd\n. service. GetLastErrorO) An example TIME server An example TIME server /UDPtimed, cpp-main #include ctime. k struct sockaddr in fsic #include < hp 20485 /"input"buffer: any size I packet. * passiveUDP(const char *E from-address length #define WINEPOCH 2208988800/ Windows epoch, in UCT WSADATA sadat #define wavERS MA∈ EWORD(2.0) main-Iterative UDP server for TIME service 如2些 rvice =argv(lE air(int argc, char *argvD rexit("usage: UDPtimed [port]\n"):] Iterative Connection-oriented An example TIME server Servers arup failed\n") aA connection-oriented server calls function rvice) passive / to create a stream socket while( 1)( passive TCP takes two arguments buf, sizeof(buf), o A character string Nname or number of service ddr * )fsin, dalen)== SOCKET_ERROR Length of incoming connection request queue ecvfrom: error %d\n", GetLastError) (void) sendto(sock (char *)now struct sockaddr & fsin, si return 1: / not reached *
5 哈工大计算机学院 李全龙 Network Application Development Server Software Design 25 passiveUDP /* Map protocol name to protocol number */ if ( (ppe = getprotobyname(transport)) == 0) errexit("can't get \"%s\" protocol entry\n", transport); /* Use protocol to choose a socket type */ if (strcmp(transport, "udp") == 0) type = SOCK_DGRAM; else type = SOCK_STREAM; /* Allocate a socket */ s = socket(PF_INET, type, ppe->p_proto); if (s == INVALID_SOCKET) errexit("can't create socket: %d\n", GetLastError()); /* Bind the socket */ if (bind(s, (struct sockaddr *)&sin, sizeof(sin)) == SOCKET_ERROR) errexit("can't bind to %s port: %d\n", service, GetLastError()); if (type == SOCK_STREAM && listen(s, qlen) == SOCKET_ERROR) errexit("can't listen on %s port: %d\n", service, GetLastError()); return s;} 哈工大计算机学院 李全龙 Network Application Development Server Software Design 26 Thread Structure for Iterative Connectionless Server server Socket at well-known port used for all communication Server application thread Operating system 哈工大计算机学院 李全龙 Network Application Development Server Software Design 27 An example TIME server /* UDPtimed.cpp - main */ #include <time.h> #include <winsock.h> SOCKET passiveUDP(const char *); void errexit(const char *, ...); #define WINEPOCH 2208988800/* Windows epoch, in UCT secs */ #define WSVERS MAKEWORD(2, 0) /*------------------------------------------------------------------------ * main - Iterative UDP server for TIME service *------------------------------------------------------------------------ */ int main(int argc, char *argv[]) 哈工大计算机学院 李全龙 Network Application Development Server Software Design 28 An example TIME server { struct sockaddr_in fsin; /* the from address of a client */ char *service = "time"; /* service name or port number */ char buf[2048]; /* "input" buffer; any size > 1 packet */ SOCKET sock; /* server socket */ time_t now; /* current time */ int alen; /* from-address length */ WSADATA wsadata; switch (argc) { case 1: break; case 2: service = argv[1]; break; default: errexit("usage: UDPtimed [port]\n");} 哈工大计算机学院 李全龙 Network Application Development Server Software Design 29 An example TIME server if (WSAStartup(WSVERS, &wsadata)) errexit("WSAStartup failed\n"); sock = passiveUDP(service); while (1) { alen = sizeof(fsin); if (recvfrom(sock, buf, sizeof(buf), 0, (struct sockaddr *)&fsin, &alen) == SOCKET_ERROR) errexit("recvfrom: error %d\n", GetLastError()); (void) time(&now); now = htonl((u_long)(now + WINEPOCH)); (void) sendto(sock, (char *)&now, sizeof(now), 0, (struct sockaddr *)&fsin, sizeof(fsin)); } return 1; /* not reached */ } 哈工大计算机学院 李全龙 Network Application Development Server Software Design 30 Iterative Connection-oriented Servers A connection-oriented server calls function passiveTCP to create a stream socket passiveTCP takes two arguments: A character string-Nname or number of service Length of incoming connection request queue