350 likes | 619 Views
Server Design. Discuss Design issues for Servers Review Server Creation in Windows. Server Software Design. Concurrent Vs. Iterative Concurrent servers support multiple clients concurrently (may or may not use concurrent processes)
E N D
Server Design Discuss Design issues for Servers Review Server Creation in Windows
Server Software Design • Concurrent Vs. Iterative • Concurrent servers support multiple clients concurrently (may or may not use concurrent processes) • Iterative servers support a single client at a time. (Much easier to build, but usually much less efficient)
Server Software Design • Concurrent Vs. Iterative • Concurrent servers support multiple clients concurrently (may or may not use concurrent processes) • Iterative servers support a single client at a time. (Much easier to build, but usually much less efficient) • Connection-Oriented Vs. Connectionless • Connection Oriented access is provided through TCP which provides all of the reliability for the access. • Connectionless access is provided through UDP, and requires the application to provide reliability.
Server Software Design • Stateless Vs. Stateful Servers • Stateful servers maintain status of ongoing client communications. Use of state information can allow more efficient communication (less information needed in each message), but it does open up the possibility of state dependencies if not carefully designed into the server. • Stateless servers rely on the client to provide all of the information needed for each request. Easier to design, but generally less efficient.
Stateless Vs. Stateful ServersExample • Client queries server for file information (ftp, database search, etc.) • Stateless model: • query includes filename, offset, # of bytes to read
Stateless Vs. Stateful ServersExample • Client queries server for file information (ftp, database search, etc.) • Stateless model: • query includes filename, offset, # of bytes to read • Stateful model: • server maintains table of client info current queries containing filename, current offset • client sends request to read # of bytes • server accesses buffer for info, if available,or disk file.
Filename: X Offset: 512 Buffer Pointer: Filename: Y Offset: 1024 Buffer Pointer: Stateless Vs. Stateful Servers hash(IP, port) Buffer for file X , pointer at 512 Buffer for file Y , pointer at 1024
Basic Server Types Iterative connectionless Iterative connection-oriented Concurrent connectionless Concurrent connection-oriented
Basic Server Types Iterative connectionless Iterative connection-oriented Concurrent connectionless Concurrent connection-oriented
Iterative Connection-Oriented Server • Create a socket & bind to the port for the service being offered. • Place the socket in passive mode (listen) • Accept the next connection request from the socket and obtain a new socket for the connection. • Repeatedly send a request from client, formulate a response, and send response back to client. • When finished with a client, close the connection & return to accept mode, waiting for a new connection.
Iterative Connectionless Server • Create a socket and bind to the well-known address for the service being offered. • Repeatedly read the next request from a client, formulate a response, and send a reply back to the client according to the application protocol.
Concurrent Connectionless Server • (P) Create a socket & bind to the port for the service being offered. • (P) Repeatedly call recvfrom to receive the next request from client, & create a new child process • (c) Recieve a specific request upon creation as well as access to the socket. • (c) Form a reply according to application protocol and send it back to client using sendto. • (c) Terminate child process upon completion of task
Concurrent Connection-Oriented Server • (P) Create a socket & bind to the port for the service offered. Leave the socket unconnected • (P) Place the socket in passive mode (listen) • (P) Repeatedly call accept to receive the next request from client, & create new child process • (c) Receive a connection request upon creation • (c) Interact with client (read request(s) & send(s)) • (c) Terminate child process upon completion of task
Server Issues • Request processing time Vs. Observed response time
Server Issues • Request processing time Vs. Observed response time • Use of INADDR_ANY to receive datagrams from any IP address.
Server Issues • Request processing time Vs. Observed response time • Use of INADDR_ANY to receive datagrams from any IP address. • Connectionless communications: • ans = sendto(s, buf, buflen, flags, toaddr, toaddrlen) • ans = recvfrom(s, buf, buflen, flags, toaddr, toaddrlen)
Server Issues • Request processing time Vs. Observed response time • Use of INADDR_ANY to receive datagrams from any IP address. • Connectionless communications: • ans = sendto(s, buf, buflen, flags, toaddr, toaddrlen) • ans = recvfrom(s, buf, buflen, flags, toaddr, toaddrlen) • Use of exec for child processes
Apparent vs Real Concurrency • Create a socket and bind to the well-known address for the service being offered. Add socket to the list of those on which I/O is possible
Apparent vs Real Concurrency • Create a socket and bind to the well-known address for the service being offered. Add socket to the list of those on which I/O is possible • Use select to wait for I/O on existing sockets
Apparent vs Real Concurrency • Create a socket and bind to the well-known address for the service being offered. Add socket to the list of those on which I/O is possible • Use select to wait for I/O on existing sockets • If original responds, use accept for new connection & add socket to the list • If other socket responds, use read / write to communicate • Return to select
Apparent Concurrency • Select (fdsize, &fd_set_in, &fd_set_out, &fd_set_err, time) • Fdsize: Not used in Windows, but used in UNIX or Linux to indicate the number of sockets that should be scanned. • &fd_set_in:address of the file descriptor set that is monitoring sockets for pending input work • &fd_set_out: Address of the file descriptor set that is moniroting sockets for pending outgoing work. • &fd_set_err: File descriptor set that monitors for exceptions to normal work (special error messages, or urgent messages) • Time: set to NULL to wait until there is some activity. Otherwise may be set to the maximum time to wait until select function returns.
Server Creation in Windows • Formal service creation and installation requires administrative privilege.
Server Creation in Windows • Formal service creation and installation requires administrative privilege. • Normal console applications maintain access to a console window. Can’t delete window without terminating process.
Server Creation in Windows • Formal service creation and installation requires administrative privilege. • Normal console applications maintain access to a console window. Can’t delete window without terminating process. • Would like to create a new process that is detached from the console (runs in “background” mode.
CreateProcess BOOL CreateProcess ( LPCTSTR IpApplicationName, LPTSTR IpCommandLine, LPSECURITY_ATTRIBUTES IpProcessAttributes, LPSECURITY_ATTRIBUTES IpthreadAttributes, DWORD fdwCreate, BOOL bInheritHandles, LPVOID IpEnvironment, LPCTSTR IpCurrentDirectory, LPSTARTINFO IpStartInfo, LPPROCESS_INFORMATION IpProcessInformation );
struct STARTUPINFO typedef struct _STARTUPINFO { DWORD cb; DWORD dwFillAttribute; LPSTR lpReserved; DWORD dwFlags; LPSTR lpDesktop; WORD wShowWindow; LPSTR lpTitle; WORD cbReserved2; DWORD dwX; LPBYTE lpReserved2; DWORD dwY; HANDLE hStdInput; DWORD dwXSize; HANDLE hStdOutput; DWORD dwYSize; HANDLE hStdError; DWORD dwXCountChars; DWORD dwYCountChars; } STARTUPINFO, *LPSTARTUPINFO;
struct PROCESS_INFORMATION typedef struct _PROCESS_INFORMATION HANDLE hProcess; HANDLE hThread; DWORD dwProcessId; DWORD dwThreadID; } PROCESS_INFORMATION, *LPPROCESS_INFORMATION;
Server Creation in WindowsWinProcess.cpp #include <stdio.h> #include <stdlib.h> #include <windows.h> #define NAMESIZE 50 void main (int argc, char *argv[]) { char AppName[NAMESIZE] = "timetest.exe"; char answer[NAMESIZE]; STARTUPINFO si; PROCESS_INFORMATION ProcessInformation;
Server Creation in WindowsWinProcess.cpp // Get the desired background process name... if (argc != 2) { printf("Usage: Winprocess background_prog\n"); exit(1); } strcpy(AppName, argv[1]); printf("Starting %s in background mode. OK?(yes/no) ", AppName); gets (answer); if (strcmp(answer, "yes") != 0) { printf("Aborting background program startup...\n"); printf("Select any key to continue....."); gets(answer); exit(1); }
Server Creation in WindowsWinProcess.cpp // Initialize the STARTINFO buffer to the // parameters needed for a detached process si.cb = sizeof(STARTUPINFO); si.lpReserved = NULL; si.lpTitle = NULL; si.lpDesktop = ""; si.dwX = 0L; si.dwY = 0L; si.dwXSize = 0L; si.dwYSize = 0L; si.dwFlags = 0; si.wShowWindow = SW_SHOW; si.lpReserved2 = NULL; si.cbReserved2 = 0;
Server Creation in WindowsWinProcess.cpp if (!CreateProcess(NULL, AppName, NULL, NULL, FALSE, DETACHED_PROCESS, NULL, NULL, &si, &ProcessInformation)) { printf("ERROR: Cannot launch child process\n"); printf("Select any key to continue....."); gets(answer); exit(1); } printf("New process id: %ld, new thread id: %ld\n", ProcessInformation.dwProcessId, ProcessInformation.dwThreadId); printf("Select any key to close WinProcess..."); gets(answer); }
timetest.cpp #include <stdio.h> #include <stdlib.h> #include <time.h> #include <windows.h> #define MYBUFFER 80 #define ENTRYCOUNT 15 void main () { char *timeptr; int entries; FILE *fp; time_t newtime;
timetest.cpp if ((fp = fopen( "timetest.dat", "w" )) == NULL ) exit(1); fputs( "This is my time test file.\n\n", fp); fclose (fp); for (entries = 0; entries < ENTRYCOUNT; entries++) { (void) time (&newtime); timeptr = ctime(&newtime); if ((fp = fopen( "timetest.dat", "a" )) == NULL ) exit(1); fputs (timeptr, fp); fputs ("Now we sleep... \n\n\n", fp); fclose (fp); Sleep (1000L); } }
timetest.cpp (output) C:\cotter\cs490d_ws98\WinProcess>winprocess timetest This will start timetest in background mode. Proceed?(yes/no) yes The new process id is 68, and the new thread id is 46 Select any key to close WinProcess... C:\cotter\cs490d_ws98\WinProcess>
Summary • Several different factors involved in server design • 4 basic types of servers • Server concurrency can be real or apparent • Server creation in Windows allows user to configure operating environment for the server program