当前位置:高等教育资讯网  >  中国高校课件下载中心  >  大学文库  >  浏览文档

哈尔滨工业大学:《网络应用开发》英文版 NAD-chapter4-6

资源类别:文库,文档格式:PDF,文档页数:19,文件大小:282.99KB,团购合买
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 each Buffer for file x
点击下载完整版文档(PDF)

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 well￾known 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 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 #include #include 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 #include 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

Thread Structure for Iterative passiveTcp Connection-oriented server pass TCP. cpp-passiveTCP SOCKET passivesock(const char * corst char *. int): application thread passive TCP-create a passive socket for use in a TaP server SOCKET ssive TCP(const char *service, int qlen) Operating return passivesock(service, "tcp", qlen) An example DAyTIME server An example DAyTIME server I/ TOPdtd, cpp-main, TcPdayti nsock. hp struct sin: / the from address of a client service="dayi or port rumber from-address length char*. int): #define QLEN 5 #define WsVERS MAKEWORD(2, 0) rrexit(usage: TaPdaytimed [port]\n): main(nt argc, char *argil An example DAyTIME server An example DAyTIME server (WSAStartuP(WSVERS, &wsadata)l=O TCPdaytimed-do TOP DAyTIME protocol mock= passive TCP(service, QLEN) while(1)( TOPdaytimed(soCKET fd rexitcaccept failed: error number %d\n (void) closesocket(sock) d, pts, strlen(pts), O)

6 哈工大计算机学院 李全龙 Network Application Development Server Software Design 31 passiveTCP /* passTCP.cpp - passiveTCP */ #include SOCKET passivesock(const char *, const char *, int); /*---------------------------------------------------------------------- * passiveTCP - create a passive socket for use in a TCP server *----------------------------------------------------------------------- */ SOCKET passiveTCP(const char *service, int qlen) { return passivesock(service, "tcp", qlen); } 哈工大计算机学院 李全龙 Network Application Development Server Software Design 32 Thread Structure for Iterative Connection-oriented Server server Server application thread Operating system Socket used for Connection requests Socket used for an individual connection 哈工大计算机学院 李全龙 Network Application Development Server Software Design 33 An example DAYTIME server /* TCPdtd.cpp - main, TCPdaytimed */ #include #include #include void errexit(const char *, ...); void TCPdaytimed(SOCKET); SOCKET passiveTCP(const char *, int); #define QLEN 5 #define WSVERS MAKEWORD(2, 0) /*------------------------------------------------------------------------ * main - Iterative TCP server for DAYTIME service *------------------------------------------------------------------------ */ void main(int argc, char *argv[]) 哈工大计算机学院 李全龙 Network Application Development Server Software Design 34 An example DAYTIME server { struct sockaddr_in fsin;/* the from address of a client */ char *service = "daytime"; /* service name or port number */ SOCKET msock, ssock; /* master & slave sockets */ int alen; /* from-address length */ WSADATA wsadata; switch (argc) { case 1: break; case 2: service = argv[1]; break; default: errexit("usage: TCPdaytimed [port]\n"); } 哈工大计算机学院 李全龙 Network Application Development Server Software Design 35 An example DAYTIME server if (WSAStartup(WSVERS, &wsadata) != 0) errexit("WSAStartup failed\n"); msock = passiveTCP(service, QLEN); while (1) { alen = sizeof(struct sockaddr); ssock = accept(msock, (struct sockaddr *)&fsin, &alen); if (ssock == INVALID_SOCKET) errexit("accept failed: error number %d\n", GetLastError()); TCPdaytimed(ssock); (void) closesocket(ssock); } } 哈工大计算机学院 李全龙 Network Application Development Server Software Design 36 An example DAYTIME server /*---------------------------------------------------------------------- * TCPdaytimed - do TCP DAYTIME protocol *----------------------------------------------------------------------- */ void TCPdaytimed(SOCKET fd) { char *pts; /* pointer to time string */ time_t now; /* current time */ (void) time(&now); pts = ctime(&now); (void) send(fd, pts, strlen(pts), 0); }

Closing connections Chapter 4: Server Software Design Calling closesocket requests a graceful Algorithm and issues Does not worry about data lost Iterative servers s Concurrent connection-oriented The application protocol determines how a server manages TCP connections a Singly-threaded, concurrent The client must signal completion a Allowing a client to control connection Multiprotocol servers duration can be dangerous Multiservice servers Uniform, efficient management of server concurrency Concurrent Connection-oriented Thread Structure for Concurrent Servers Connection-oriented server Consider concurrent ECHO Iterative VS Concurrent implementation application thread Concurrent avoids long delay Concurrent offers better observed response time An example concurrent ECHO server An example concurrent ECHO server TCPechod cpp-main, TOPechod*/ main-Concurrent TCP server for ECHO service #define QLEN 5/ maximum connection queue length * i shrue erice soch di n sin he adres renumber "y STKSIZE FSIZE #define WSVERS switch(arge)( SOCKET sock, sock: / master slave TCPechod(SOCKET) service= argil] errexit(const char *,.: SoCKET passive TCP(const char * int) errexit("usage: TCPechod [port]\m")

7 哈工大计算机学院 李全龙 Network Application Development Server Software Design 37 Closing connections „ Calling closesocket requests a graceful shutdown „ Does not worry about data lost „ The application protocol determines how a server manages TCP connections „ The client must signal completion „ Allowing a client to control connection duration can be dangerous 哈工大计算机学院 李全龙 Network Application Development Server Software Design 38 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 39 Concurrent Connection-oriented Servers „ Consider concurrent ECHO „ Iterative VS Concurrent implementation „ Iterative performs poorly „ Concurrent avoids long delay „ Concurrent offers better observed response time 哈工大计算机学院 李全龙 Network Application Development Server Software Design 40 Thread Structure for Concurrent Connection-oriented Server server Server application thread Operating system Socket for Connection requests Socket for individual connection slave1 slave2 slaven … 哈工大计算机学院 李全龙 Network Application Development Server Software Design 41 An example concurrent ECHO server /* TCPechod.cpp - main, TCPechod */ #include #include #include #define QLEN 5 /* maximum connection queue length */ #define STKSIZE 16536 #define BUFSIZE 4096 #define WSVERS MAKEWORD(2, 0) SOCKET msock, ssock; /* master & slave server sockets */ int TCPechod(SOCKET); void errexit(const char *, ...); SOCKET passiveTCP(const char *, int); 哈工大计算机学院 李全龙 Network Application Development Server Software Design 42 An example concurrent ECHO server /*------------------------------------------------------------------------ * main - Concurrent TCP server for ECHO service *------------------------------------------------------------------------ */ int main(int argc, char *argv[]) { char *service = "echo"; /* service name or port number */ struct sockaddr_in fsin; /* the address of a client */ int alen; /* length of client's address */ WSADATA wsadata; switch (argc) { case 1: break; case 2: service = argv[1]; break; default: errexit("usage: TCPechod [port]\n"); }

An example concurrent ECHO server An example concurrent ECHO server CPechod-echo data until end of fileI while (I)( char buf[BUFSIZE] en= sizeof(fsin) c:recv(fd, buf, sizeof buf, O): if (sock =s INVALID SOCKET number \n", GetLastError OE if Cbeginthread(void (")(void"))TCPechod, STKSIZE. (void*)sock)< o)[ rexitc-beginthread: %s\n, strerror(errno) ce= recv(fd, buf, sizeof buf, O:) if (cc = SOCKET_ ERROR) fprintf(stderr. "echo recv error: Zd\n" GetLo return 1: /not reached n Singly-Threaded, Concurrent Chapter 4: Server Software Design Servers (TCP) Chapter goal: a Data-driven processing in a serve Algorithm and issues s Iterative servers Concurrent connection-oriented Arrange for a single tread to keep TCP connections servers Singly-threaded, concurrent The server uses the arrival of data to trigger processing a Multiprotocol servers Consider previous concurrent ECHO Slave thread spends most time blocked in a call to recv s Multiservice servers uIre time Uniform, efficient management er request often b server concurrency Data-driven processing with a single thread A single thread can perform the same task Thread Structure for A Singly- & Thread Structure for A Singly- hreaded Server threaded server A single thread server must perform the duties of both the master and slave thread Server The server passes the set of socket descriptors as argument to select select returns bit mask that specifies which of the descriptors in the set is ready s The server uses the order descriptors become ready to decide how to The server uses the descriptors to distinguish 8

8 哈工大计算机学院 李全龙 Network Application Development Server Software Design 43 An example concurrent ECHO server if (WSAStartup(WSVERS, &wsadata) != 0) errexit("WSAStartup failed\n"); msock = passiveTCP(service, QLEN); while (1) { alen = sizeof(fsin); ssock = accept(msock, (struct sockaddr *)&fsin, &alen); if (ssock == INVALID_SOCKET) errexit("accept: error number\n", GetLastError()); if (_beginthread((void (*)(void *))TCPechod, STKSIZE, (void *)ssock) 0) { if (send(fd, buf, cc, 0) == SOCKET_ERROR) { fprintf(stderr, "echo send error: %d\n", GetLastError()); break;} cc = recv(fd, buf, sizeof buf, 0);} if (cc == SOCKET_ERROR) fprintf(stderr, "echo recv error: %d\n", GetLastError()); closesocket(fd); return 0;} 哈工大计算机学院 李全龙 Network Application Development Server Software Design 45 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 46 Singly-Threaded, Concurrent Servers (TCP) „ Data-driven processing in a server „ For applications where I/O dominates the cost of preparing a response to a request, a server can use asynchronous I/O to provide apparent concurrency „ Arrange for a single server thread to keep TCP connections open to multiple clients, and have the server handle a given connection when data arrives „ The server uses the arrival of data to trigger processing „ Consider previous concurrent ECHO server „ Slave thread spends most time blocked in a call to recv „ Concurrent servers that require little processing time per request often behave in a sequential manner „ Data-driven processing with a single thread „ A single thread can perform the same task 哈工大计算机学院 李全龙 Network Application Development Server Software Design 47 Thread Structure for A Singly￾threaded Server server Server application thread Operating system Socket for Connection requests Socket for individual connection … 哈工大计算机学院 李全龙 Network Application Development Server Software Design 48 Thread Structure for A Singly￾threaded Server „ A single thread server must perform the duties of both the master and slave threads „ The server passes the set of socket descriptors as argument to select „ select returns bit mask that specifies which of the descriptors in the set is ready „ The server uses the order in which descriptors become ready to decide how to proceed „ The server uses the descriptors to distinguish between master and slave operations

An example singly-threaded An example Singly-threaded ECHO server ECHO server TCPmechd, cpp-main, echo*/ #include winsock ha main-Concurrent TCP server for ECHO service #include <string. h pid main(int argc, char *argv[D #define QLEN 5 maximum connection queue length * har *service ="echo" / service name or port number * struct ockaddr in fsin / the from address of a client BUFSIZE SOCKET mock: / #define AKEWORD(2. 0) rfds: /*read file descriptor set fo_set afds: /active file descriptor set * void errexit(const char*..E: from-address length SOCKET ssive TCP(const char * int): WSADATA wsdata int echo(soCKET) unsigned int fdndx An example Singly-threaded An example singly -threaded ECHO server ECHO server switch(argc)t while (I)i emcp(&rfds, dads, sizeof(rfds)): (select(FD_ SETSIZE, &rfds, (fd set *)o, (fd set")o (struct timeval *)0)== SOCKET ERROR) service= argv[1B: if(FD_ ISSET(mock, &rfds))( errexitc"usage: TCPmechod [port]\n") clen s sizeof(fsin): sock z accept(sock, (struct sockaddr *fsin. if (wsAStartup(WSVERS, &wsdata)E O) t(WSAStartup failed\"E if(sock == INVALID_ SOCKET errexit("accept: error Ad\n FD- ZERO(afds): tLastError o: FD_SET(sock, &afds): FD_ SET(sock, &afds): 1 An example Singly-threaded .I An example Singly-threaded ECHO server ECHO server for (fandx=0: fandxerfds, fd count:++fdndx( echo-echo one buffer of data, returning byte count if (fd l mock & FD_IsSET(fd, &rfds)) char buf[BUFSIZE] FD CLR(fd, &afds recv(fd, buf, sizeof buf, O): if (cc = SOCKET ERROR) if(cc &d send(fd, buf, cc, O)== 5OCKET-ERROR) errexit(echo send error Zd\n

9 哈工大计算机学院 李全龙 Network Application Development Server Software Design 49 An example Singly-threaded ECHO server /* TCPmechd.cpp - main, echo */ #include #include #define QLEN 5 /* maximum connection queue length */ #define BUFSIZE 4096 #define WSVERS MAKEWORD(2, 0) void errexit(const char *, ...); SOCKET passiveTCP(const char *, int); int echo(SOCKET); 哈工大计算机学院 李全龙 Network Application Development Server Software Design 50 An example Singly-threaded ECHO server /*------------------------------------------------------------------------ * main - Concurrent TCP server for ECHO service *------------------------------------------------------------------------ */ void main(int argc, char *argv[]) { char *service = "echo";/* service name or port number */ struct sockaddr_in fsin;/* the from address of a client*/ SOCKET msock; /* master server socket */ fd_set rfds; /* read file descriptor set */ fd_set afds; /* active file descriptor set */ int alen; /* from-address length */ WSADATA wsdata; unsigned int fdndx; 哈工大计算机学院 李全龙 Network Application Development Server Software Design 51 An example Singly-threaded ECHO server switch (argc) { case 1: break; case 2: service = argv[1]; break; default: errexit("usage: TCPmechod [port]\n"); } if (WSAStartup(WSVERS, &wsdata) != 0) errexit("WSAStartup failed\n"); msock = passiveTCP(service, QLEN); FD_ZERO(&afds); FD_SET(msock, &afds); 哈工大计算机学院 李全龙 Network Application Development Server Software Design 52 An example Singly-threaded ECHO server while (1) { memcpy(&rfds, &afds, sizeof(rfds)); if (select(FD_SETSIZE, &rfds, (fd_set *)0, (fd_set *)0, (struct timeval *)0) == SOCKET_ERROR) errexit("select error: %d\n", GetLastError()); if (FD_ISSET(msock, &rfds)) { SOCKET ssock; alen = sizeof(fsin); ssock = accept(msock, (struct sockaddr *)&fsin, &alen); if (ssock == INVALID_SOCKET) errexit("accept: error %d\n", GetLastError()); FD_SET(ssock, &afds);} 哈工大计算机学院 李全龙 Network Application Development Server Software Design 53 An example Singly-threaded ECHO server for (fdndx=0; fdndx<rfds.fd_count; ++fdndx){ SOCKET fd = rfds.fd_array[fdndx]; if (fd != msock && FD_ISSET(fd, &rfds)) if (echo(fd) == 0) { (void) closesocket(fd); FD_CLR(fd, &afds); } } } } 哈工大计算机学院 李全龙 Network Application Development Server Software Design 54 An example Singly-threaded ECHO server /*------------------------------------------------------------------------ * echo - echo one buffer of data, returning byte count *------------------------------------------------------------------------ */ int echo(SOCKET fd) { char buf[BUFSIZE]; int cc; cc = recv(fd, buf, sizeof buf, 0); if (cc == SOCKET_ERROR) errexit("echo recv error %d\n", GetLastError()); if (cc && send(fd, buf, cc, 0) == SOCKET_ERROR) errexit("echo send error %d\n", GetLastError()); return cc; }

Chapter 4: Server Software Design Multiprotocol Servers Chapter gae Algorithm and issues Motivation for reducing the number of Iterative servers n Concurrent connection-oriented In most cases, a given server handles requests for ne particu servers a Singly-threaded, concurrent particular transport protocol servers The chief advantage of using separate server for a Multiprotocol servers ach protocol lies in contro Multiservice servers The chief disadvantage lies in replication and use Uniform, efficient management of server concurrency Multiprotocol server design A single thread that uses asynchronous I/O Thread Structure for An example multiprotocol multiprotocol Server DAYTIME server A daytime. cpp- main, daytime% Server SOCKE e TCP(const char * int): #define QLEN LINELEN An example multiprotocol An example multiprotocol DAyTIME server DAyTIME server switch(argc)( s main- Iterative server for DAYTIME service service=argv[l] main(int arge, char *argv[D default: ar service ="daytime". service rame or port number xtc"usage: daytime [port]\n") sockaddr in fsin / the request from address % if (wSAStartup(WSVERS, &wsadata)l= O) errexit(WSAStartup failed\n ive TCP(service, QLEN: A isodata _ZERO(&rids

10 哈工大计算机学院 李全龙 Network Application Development Server Software Design 55 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 56 Multiprotocol Servers „ Motivation for reducing the number of servers „ In most cases, a given server handles requests for one particular service accessed through one particular transport protocol „ The chief advantage of using separate server for each protocol lies in control „ The chief disadvantage lies in replication and use of resources „ Multiprotocol server design „ A single thread that uses asynchronous I/O 哈工大计算机学院 李全龙 Network Application Development Server Software Design 57 Thread Structure for multiprotocol Server server Server application thread Operating system Socket for UDP requests Socket for TCP conn. requests … Socket for a TCP connection 哈工大计算机学院 李全龙 Network Application Development Server Software Design 58 An example multiprotocol DAYTIME server /* daytimed.cpp - main, daytime */ #include #include #include void daytime(char buf[]); void errexit(const char *, ...); SOCKET passiveTCP(const char *, int); SOCKET passiveUDP(const char *); #define WSVERS MAKEWORD(2, 0) #define QLEN 5 #define LINELEN 128 哈工大计算机学院 李全龙 Network Application Development Server Software Design 59 An example multiprotocol DAYTIME server /*------------------------------------------------------------------------ * main - Iterative server for DAYTIME service *------------------------------------------------------------------------ */ void main(int argc, char *argv[]) { char *service = "daytime"; /* service name or port number */ char buf[LINELEN+1]; /* buffer for one line of text */ struct sockaddr_in fsin;/* the request from address */ int alen; /* from-address length */ SOCKET tsock; /* TCP master socket */ SOCKET usock; /* UDP socket */ fd_set rfds; /* readable file descriptors */ int rv; WSADATA wsadata; 哈工大计算机学院 李全龙 Network Application Development Server Software Design 60 An example multiprotocol DAYTIME server switch (argc) { case 1: break; case 2: service = argv[1]; break; default: errexit("usage: daytimed [port]\n"); } if (WSAStartup(WSVERS, &wsadata) != 0) errexit("WSAStartup failed\n"); tsock = passiveTCP(service, QLEN); usock = passiveUDP(service); FD_ZERO(&rfds);

点击下载完整版文档(PDF)VIP每日下载上限内不扣除下载券和下载次数;
按次数下载不扣除下载券;
24小时内重复下载只扣除一次;
顺序:VIP每日次数-->可用次数-->下载券;
共19页,试读已结束,阅读完整版请下载
相关文档

关于我们|帮助中心|下载说明|相关软件|意见反馈|联系我们

Copyright © 2008-现在 cucdc.com 高等教育资讯网 版权所有