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 <winsock.h> 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 <stdlib.h> #include <winsock.h> #include <time.h> 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 <stdio.h> #include <winsock.h> #include <process.h> #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"); }