TDC 561 Network Programming Week 4 ClientServer Design
- Slides: 68
TDC 561 Network Programming Week 4: § Client/Server Design Approaches § Case Study Using Sockets – A Client Server Application of Network Performance Management presentation prepared with the help of Dale Knudson Ph. D, Lucent Technologies Distributed Systems Frameworks (DS 520 -95 -103) DISC’ 99
References § Douglas Comer, David Stevens, Internetworking with TCP/IP : Client-Server Programming, Volume III (BSD Unix and ANSI C), 2 nd edition, 1996 (ISBN 0 -13 -260969 -X) – Chap. 2, 8 § W. Richard Stevens, Network Programming : Networking API: Sockets and XTI, Volume 1, 2 nd edition, 1998 (ISBN 013 -490012 -X) – Chap. 7 (partial), 27 (partial), 16. 1 -16. 3 (partial) Network Programming (TDC 561) Winter 2003 Page 2
Server Design Iterative Connectionless Iterative Connection-Oriented Concurrent Connectionless Concurrent Connection-Oriented Network Programming (TDC 561) Winter 2003 Page 3
Concurrent vs. Iterative Concurrent • Large or variable size requests • Harder to program • Typically uses more system resources Iterative • Small, fixed size requests • Easy to program Network Programming (TDC 561) Winter 2003 Page 4
Connectionless vs. Connection-Oriented • Easy to program • Transport protocol handles the tough stuff. • Requires separate socket for each connection. Connectionless • Less overhead • No limitation on number of clients Network Programming (TDC 561) Winter 2003 Page 5
Statelessness § State: Information that a server maintains about the status of ongoing client interactions. § Issues with Statefullness – – Clients can go down at any time. Client hosts can reboot many times. The network can lose messages. The network can duplicate messages. § Example – Connectionless servers that keep state information must be designed carefully » Messages can be duplicated Network Programming (TDC 561) Winter 2003 Page 6
Concurrent Server Design Alternatives § One child per client § Single Process Concurrency (AKA Pseudo-Concurrency or Apparent Concurrency) § Pre-forking multiple processes § Spawn one thread per client will be discussed in a future lecture § Pre-threaded Server Network Programming (TDC 561) Winter 2003 Page 7
One child per client § Traditional Unix server: – TCP: after call to accept(), call fork(). – UDP: after recvfrom(), call fork(). – Each process needs only a few sockets. – Small requests can be serviced in a small amount of time. § Parent process needs to clean up after children (call wait() ). Network Programming (TDC 561) Winter 2003 Page 8
Discussion Stevens Example: Concurrency using fork() 1/3 /* Code fragment that uses fork() and signal() to implement concurrency */ /* include and define statements section */ void signal_handler(int sig) { int status; wait(&status); /* awaits child process to exit therefore allows child to terminate, and to transit from ZOMBIE to NORMAL TEMINATION (END) state */ signal(SIGCHLD, &signal_handler); /* restarts signal handler */ } Network Programming (TDC 561) Winter 2003 Page 9
Discussion Stevens Example: Concurrency using fork() 2/3 main(int argc, char *argv[]) { /* Variable declaration section */ /* The calls socket(), bind(), and listen() */ See previous slide signal(SIGCHLD, &signal_handler); while(1) { /* infinite accept() loop */ newfd = accept(sockfd, (struct sockaddr *)&theiraddr, &sinsize); if (newfd < 0) { /* error in accept() */ if (errno = EINTR) continue; else { perror("accept"); exit(-1); } } Network Programming (TDC 561) Winter 2003 Page 10
Discussion Stevens Example: Concurrency using fork() 3/3 /* successfully accepted a new client connection newfd >=0 */ switch (fork()) { case -1: /* fork() error */ perror("fork"); exit(-1); case 0: /* child handles request */ close(sockfd); /* read msg and form a response */ /* send response back to the client */ close(newfd); exit(-1); /* exit() sends by default SIGCHLD to parent */ default: /* parent returns to wait for another request */ close(newfd); } /* end switch */ } /* end while(1) */ } Network Programming (TDC 561) Winter 2003 Page 11
Discussion: Single Process Concurrency 1/2 main(int argc, char *argv[]) { /* various declarations */ int result; fd_set readfds, testfds; /* socket(), bind(), listen() … */ /* inititialization of the fd set FD_ZERO(&readfds); /* add listening socket sockfd to the reading set */ FD_SET(sockfd, &readfds); while(1) { int fd; testfds = readfds; result = select(FD_SETSIZE, &testfds, NULL, NULL); /* error check for select */ Network Programming (TDC 561) Winter 2003 Page 12
Discussion: Single Process Concurrency 2/2 for (fd = 0; fd < FD_SETSIZE; fd++) { if (FD_ISSET(fd, &testfds)) { /* find activated socket fd */ if (fd == sockfd) { /* ==> New connection */ newfd = accept(sockfd, (struct sockaddr *)&theiraddr, &sinsize); FD_SET(newfd, &readfds); /* update file descriptor set with newfd */ } else { /* ==> request from ``old'' connection */ /* call read() and handle request from old connection using fd */ close(fd); FD_CLR(fd, &readfds); /* remove fd when connection finished */ } /* else */ } /* end if (FD_ISSET() */ } / * end for (; ; ) */ } /* end while(1) */ return 1; } Network Programming (TDC 561) Winter 2003 Page 13
OAM&P - Operations, Administration, Maintenance and Provisioning ITU Categories (International Telecommunications Union) 1. Configuration Management (CM) 2. Accounting Management (AM) 3. Fault Management (FM) 4. Performance Management (PM) 5. Security Management (SM) Network Programming (TDC 561) Winter 2003 Page 14
Performance Management Custom Reports PM report (one per hour) Wireless Switch OSS NM ftp What if you want the data more frequently? OSS NM – Operation Support System - Network Manager Network Programming (TDC 561) Winter 2003 Page 15
Every 5 minutes OMP Server (but how much data? ) Collection Specification Part A – every 1 hour OMP Part B – every 30 minutes Part C – every 5 minutes OMP Server (Operations and Maintenance Platform Server) Network Programming (TDC 561) Winter 2003 Page 16
OMP Master Clock PM Poll for data PM PM PM Network Programming (TDC 561) Winter 2003 Page 17
OMP Web browser Ethernet Network Programming (TDC 561) Winter 2003 Page 18
OMP Web Server Web browser Ethernet Network Programming (TDC 561) Winter 2003 Page 19
OMP Web Server HTML pages Web browser Ethernet Network Programming (TDC 561) Winter 2003 Page 20
OMP Web Server HTML pages Generated HTML CGI Script Web browser PM hourly data Ethernet Network Programming (TDC 561) Winter 2003 Page 21
OMP Web Server HTML pages Generated HTML CGI Script PM hourly PM On data Demand data Web browser Ethernet Network Programming (TDC 561) Winter 2003 Page 22
OMP Web Server HTML pages Generated HTML CGI Script PM hourly data On Demand data PM PM Hourly On Demand Collection Network Programming (TDC 561) Web browser Ethernet Winter 2003 Page 23
OMP Web Server HTML pages Generated Web Browser HTML Java applet CGI Script PM hourly data On Demand data PM PM Hourly On Demand Collection Network Programming (TDC 561) User Terminal Ethernet Winter 2003 Page 24
OMP Web Server HTML pages Generated Web Browser HTML Java applet Client CGI Script PM hourly data On Demand data PM PM Hourly On Demand Collection Socket User Terminal Ethernet Server Network Programming (TDC 561) Winter 2003 Page 25
CREATE A SERVER SOCKET AF_INET = use TCP/IP internet protocols SOCK_STREAM = connection oriented socket (not datagrams) On Demand s_sock = socket( AF_INET, SOCK_STREAM, 0) Collection (server socket) Note that this socket is not yet associated with a port number. Network Programming (TDC 561) Winter 2003 Page 26
CREATE A SERVER SOCKET s_sock = socket( AF_INET, SOCK_STREAM, 0) bind will associate s_sock with a local name or address/port pair. memset(&serv_adr, 0, sizeof(serv_adr)); On Demand Collection (server socket) serv_adr. sin_family = AF_INET; serv_adr. sin_addr. s_addr = INADDR_ANY; serv_adr. sin_port = htons(PORT); bind (s_sock, (struct sockaddr *)(&serv_adr), sizeof(serv_adr)); Note that this port number will be known to all the clients. Network Programming (TDC 561) Winter 2003 Page 27
CREATE A SERVER SOCKET s_sock = socket( AF_INET, SOCK_STREAM, 0) bind (s_sock, (struct sockaddr *)(&serv_adr), sizeof(serv_adr)); Set up a queue for incoming connection requests. On Demand In this case set up for a maximum of 5 clients. Collection (server socket) Network Programming (TDC 561) listen (s_sock, 5); Winter 2003 Page 28
CREATE A CLIENT SOCKET AF_INET = use TCP/IP internet protocols SOCK_STREAM = connection oriented socket (not datagrams) c_sock = socket( AF_INET, SOCK_STREAM, 0) Java applet (socket client) Note that this socket is not yet associated with a local or destination address. Network Programming (TDC 561) Winter 2003 Page 29
CREATE A CLIENT SOCKET c_sock = socket( AF_INET, SOCK_STREAM, 0); Connect binds a permanent destination to a socket, placing it in the connected state. Java applet Internet address interpretation routine – translates from dotted decimal format (e. g. , a. b. c. d) to a 32 bit internet address. serv_adr. sin_family (socket client) = AF_INET; serv_adr. sin_addr. s_addr = inet_addr(serv_address); serv_adr. sin_port = htons(PORT); connect (c_sock, (*sockaddr *)&server, sizeof(server)); Network Programming (TDC 561) Winter 2003 Page 30
CREATE A CLIENT SOCKET c_sock = socket( AF_INET, SOCK_STREAM, 0); connect (c_sock, (*sockaddr *)&server, sizeof(server)); Java applet The client can now start reading and writing on the socket. (socket client) Do { buf = message to send to the server write (c_sock, buf, len); read (c_sock, buf, len); process response message received from the server } while (1); /* or until complete */ Network Programming (TDC 561) Winter 2003 Page 31
Known port number On Demand Java applet Collection (socket client) (socket server) Network Programming (TDC 561) Winter 2003 Page 32
CREATE A SERVER SOCKET s_sock = socket( AF_INET, SOCK_STREAM, 0) bind (s_sock, (struct sockaddr *)(&serv_adr), sizeof(serv_adr)); listen (s_sock, 5); On Demand Collection (server socket) Once a socket has been set up, the server needs to wait for a connection. To do so, it uses system call accept. A call to accept blocks until a connection request arrives. do { new_sock = accept (s_sock, (struct sockaddr *) &client_adr, &client_len); if (fork() == 0) { /* in CHILD process */ while ((len=read(new_sock, buf, size)){ /* process message */ } close (new_sock); exit(0); } else close (new_sock); /* in parent */ } while (1); /* process messages forever */ Network Programming (TDC 561) Winter 2003 Page 33
CREATE A SERVER SOCKET s_sock = socket( AF_INET, SOCK_STREAM, 0) bind (s_sock, (struct sockaddr *)(&serv_adr), sizeof(serv_adr)); listen (s_sock, 5); On Demand Collection (server socket) new_sock = accept (s_sock, (struct sockaddr *) &client_adr, &client_len); When a request arrives, the system fills in client_adr with the address of the client that placed the request. The system creates a new socket file descriptor, new_sock, that has its destination connected to the requesting client. Network Programming (TDC 561) Winter 2003 Page 34
Known port Number Listening/ Passive socket On Demand Java applet Collection (socket client) (socket server) Active socket (returned by accept) When the server accepts a connection from a client, a new file descriptor is assigned. Network Programming (TDC 561) Winter 2003 Page 35
Second Client Known port On Demand Collection number Java applet (socket client) (socket server) Network Programming (TDC 561) Winter 2003 Page 36
Network Programming (TDC 561) Winter 2003 Page 37
Network Programming (TDC 561) Winter 2003 Page 38
Network Programming (TDC 561) Winter 2003 Page 39
Network Programming (TDC 561) Winter 2003 Page 40
Network Programming (TDC 561) Winter 2003 Page 41
Network Programming (TDC 561) Winter 2003 Page 42
Question: How can we have a single process handle all the client interfaces and at the same time do all PM count retrieval from the cells? Why would we want to do this? Network Programming (TDC 561) Winter 2003 Page 43
void ODinit() { /* Call the signal handler if we get one a SIGPOLL signal. This indicates that ** a socket needs servicing. */ (void) sigset (SIGPOLL, odsig_handler); --- continued later --} static void ODsig_handler (int sig) { switch (sig) { case SIGPOLL: sigpoll_flag = TRUE; default: break; } } Network Programming (TDC 561) Winter 2003 Page 44
main ( ) { /* On Demand collection process */ ODinit(); /* initialize the socket * while (1) { while (ODwork_to_do()){ ODdo_work(); } ODwait_for_work(); } } Network Programming (TDC 561) Winter 2003 Page 45
Void ODwait_for_work ( void ) { /* Define how to react to Asynchronous Events - messages */ uxretval = UXWGETMSG ( &uxmbuf, UXFOREVER ); switch ( uxretval) { case UXMSG: /* Process the UXMSG received from a cell - Network Element */ ODuxmsg(&uxmbuf ); break; case UXEINTR: /* Got an interrupt. Handled on return from this function */ break; case UXENOPORT: /* Somehow the port we are doing the UXWGETMSG on got dropped */ /* Restart the Unix port */ uxretval = UXCONNPORT(ITPT_OD); break; default: break; } } Network Programming (TDC 561) Winter 2003 Page 46
int ODwork_to_do (void) { if (cl 1 timerflag || cl 2 timerflag || … ) return (TRUE); if (rsp 1 timerflag || rsp 2 timerflag || … ) return (TRUE); if (sigpoll_flag) return (TRUE); return (FALSE); } Network Programming (TDC 561) Winter 2003 Page 47
Void ODdo_work ( void ) { if (sigpoll_flag) { /* Process client request */ sigpoll_flag = FALSE; ODclient_req(); remember this one } if (cl 1 timerflag) { /* Process periodic request to Network elements (cells, . . )*/ ODmsgdest (ODCL 1 TIMERTAG); odcl 1 timerflag = FALSE; } if (rsp 1 timerflag) { /* Timed out waiting for response from Network elements (cells, . . )*/ ODerr(ODRSP 1 TIMERTAG); ODRSP 1 TIMERTAG = FALSE; } if (audit_timer_flag) { /* Time for a socket audit */ ODsock_audit(); } } Network Programming (TDC 561) Winter 2003 Page 48
ODinit continued Void ODinit () { /* Initialize the server structure */ server. sin_addr. s_addr = INADDR_ANY; server. sin_family = AF_INET; /* Get the service port associated with this process usually in some /etc/ config file such as /etc/services */ servent_p = getservbyname ( PMSERVICE, “tcp” ); /* NOTE: API is not IPv 6 compliant!*/ server. sin_port = servent_p->s_port; /* Now create the connection socket */ ODsock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); /* Allow local address reuse */ on = 1; setsockopt (ODsock, SOL_SOCKET, SO_REUSEADDR, (char *)&on, sizeof(on)); Network Programming (TDC 561) Winter 2003 Page 49
ODinit continued Void ODinit () { /* Bind the socket to the local address specified on the server. This establishes a local address for the socket. */ bind (ODsock, (struct sockaddr *) (&server), sizeof(server)); /* Set up the server to listen for connection requests from the client. This simply marks the socket as ready to receive a connection. It does not wait for the client to connect. */ (void) listen (ODsock, 10); /* allow 10 connections pending */ Network Programming (TDC 561) Winter 2003 Page 50
Question: How do we know how many client to allow? Network Programming (TDC 561) Winter 2003 Page 51
select() says these file descriptors from the ODreadmask have work pending X X X ODreadmask X X Set by FD_SET X X X FD_ISSET – tells if a particular file descriptor returned by select() has pending work Network Programming (TDC 561) Winter 2003 Page 52
ODinit continued Void ODinit () { /* Include the socket descriptor in the list of sockets to poll. Set up the descriptor so that we get a SIGPOLL when there’s something to read from the socket. */ FD_ZERO (&ODreadmask); FD_SET (ODsock, &ODreadmask); if ( (rc -= ioctl (ODsock, I_SETSIG, S_INPUT )) == -1 ) { error } /* At this point, if we get any input requests on the connection server socket, we’ll get a SIGPOLL, call our sigpoll handler, and set a flag indicating that we have input on one of our sockets. */ Network Programming (TDC 561) Winter 2003 Page 53
Void ODclient_req ( void ) { /* ODreadmask is a global file descriptor set. Initially it includes only the bound (listening) socket file descriptor. New file descriptors are added to the ODreadmask after accepting a connection request. Since select() modifies the read mask, we’ll have to make a copy. */ memcpy (&readfdset, &ODreadmask, sizeof(readfdset)); /* select() is used to determine if there are outstanding requests at any of the socket fds */ numrequests = select (nfds, &readfdset, (fd_set *)NULL, &timeout); Network Programming (TDC 561) Winter 2003 Page 54
ODclient continued /* Loop through all the client socket file descriptors to determine the next socket with an outstanding request. */ for (i = 0; i < nfds; i++) { if (!FD_ISSET (socknum, &readfdset) { /* nothing for this fd */ return; } /* If the request is from the listening socket, do an accept and add the new socket file descriptor to the global readmask. otherwise we have work for a client already connected. */ if (socknum == ODsock) { ODconnect_client(); /* New client */ } else { ODapi (socknum); } } Network Programming (TDC 561) Winter 2003 Page 55
Void ODconnect_client ( void ) { /* accept() will extract the first connection on the queue of pending connections, create a new socket with the properties of ODsock, and allocate a new file descriptor (clientsock) for the socket. */ clientsock = accept (ODsock, (struct sockaddr *)0, &i ); /* set up this socket so that it generates a SIGPOLL when it has input */ rc = ioctl (clientsock, I_SETSIG, S_INPUT); /* Add the socket to the global read mask */ FD_SET ( clientsock, &ODreadmask ); /* We have a valid client. Send a challenge message to the client. */ ODsendchallenge ( clientsock, challenge_type ); } Network Programming (TDC 561) Winter 2003 Page 56
Void ODsock_audit () { /* Loop through connection table - list of all active socket file desriptors*/ /* If fd is open, but no activity for two intervals, close the socket */ ODclose_sock ( ODconnect[i], sockfd ); /* Reset the audit timer */ UXTIMER(intvl, AUDIT_TAG, ODtimer_exp); } Network Programming (TDC 561) Winter 2003 Page 57
Java applet – client side public void run() { // open a socket to the server sock. open(host. Name, port. No); } class PMod. SW { public int open(String host. Name, int port. No) { sock = new Socket(host. Name, port. No) // get. Input. Stream is the input stream for reading bytes from the // Data. Input. Stream sock. In = new Data. Input. Stream(sock. get. Input. Stream()); //get. Output. Stream writes to the Data. Outputstream sock. Out = new Data. Output. Stream (sock. get. Output. Stream()); } Network Programming (TDC 561) Winter 2003 Page 58
Java applet – client side continued public String get. Line() { // read response message from the OMP, // waits until a message is received line = sock. In. read. Line(); } public int send. Req (byte[] msg, int offset, int length) { // send a message to the server on the OMP sock. Out. write (msg, offset, length) } } // end of PMod. SW class Network Programming (TDC 561) Winter 2003 Page 59
Additional Socket APIs - will be discussed in a future lecture Distributed Systems Frameworks (DS 520 -95 -103) DISC’ 99
Socket Options § Various attributes that are used to determine the behavior of sockets. § Setting options tells the OS/Protocol Stack the behavior we want. § Support for generic options (apply to all sockets) and protocol specific options. Network Programming (TDC 561) Winter 2003 Page 61
Setting and Getting option values getsockopt() gets the current value of a socket option. setsockopt() is used to set the value of a socket option. #include <sys/socket. h> Network Programming (TDC 561) Winter 2003 Page 62
getsockopt() int getsockopt( int sockfd, int level, int optname, void *opval, socklen_t *optlen); level specifies whether the option is a general option or a protocol specific option (what level of code should interpret the option). Network Programming (TDC 561) Winter 2003 Page 63
setsockopt() int setsockopt( int sockfd, int level, int optname, const void *opval, socklen_t optlen); Network Programming (TDC 561) Winter 2003 Page 64
SO_REUSEADDR § Boolean option: enables binding to an address (port) that is already in use. § Used by servers that are transient - allows binding a passive socket to a port currently in use (with active sockets) by other processes. § Can be used to establish separate servers for the same service on different interfaces (or different IP addresses on the same interface). § Virtual Web Servers can work this way. Network Programming (TDC 561) Winter 2003 Page 65
Signal Driven I/O (R. Stevens, text, chap. 22 ) § A signal handler can be installed to instruct the kernel to generate a SIGIO or SIGPOLL signal whenever something happens to a socket descriptor. § The signal handler must determine what conditions caused the signal and take appropriate action. § See also Stevens, sect. 7. 10 – fcntl (SVR 4) and ioctl(BSDx) and fcntl (POSIX) Network Programming (TDC 561) Winter 2003 Page 66
Signal Driven UDP § SIGIO occurs whenever: – an incoming datagram arrives. – an asynchronous error occurs. » such as unreachable or invalid address § Could allow process to handle other tasks and still watch for incoming UDP messages. § Example of signal-driven UDP example described in the book ( R. Stevens, text, chap. 22): – NTP Network Time Protocol. – Used to record timestamp of arrival of UDP datagram. Network Programming (TDC 561) Winter 2003 Page 67
Signal Driven TCP § SIGIO occurs whenever: – – – – an incoming connection has completed. Disconnect request initiated. Disconnect request completed. Half a connection shutdown. Data has arrived. Data has been sent (indicating there is buffer space) Asynchronous error Network Programming (TDC 561) Winter 2003 Page 68
- Clientserver network
- Clientserver network
- Basic concept of client server architecture
- Clientserver model
- Clientserver model
- Se 561
- Ece 561
- Regulamentul 561 timpi de conducere
- Usp 561
- Ece 561
- Reglament 561
- Comp 561
- Week by week plans for documenting children's development
- Ece 526
- Codigos miscelaneos cnc
- Location of drc
- Tdc systems integration
- Tdc significato pedagogico
- Fpga tdc
- Honeywell tdc 2000
- Tdc switch
- Honeywell tdc 3000
- Wilson formel
- Distributionsstyring
- Clocksync
- Tdc
- Tdc cardiologie
- Tdc seminar
- Tdc 2
- Tdc module
- Tdc feriehuse
- Tdc systems integration
- Tcc tdc
- Lidar tdc
- Perbedaan linear programming dan integer programming
- Greedy algorithm vs dynamic programming
- System programming definition
- Linear vs integer programming
- Definisi integer
- Tcp echo server
- Value result arguments in network programming
- Android network programming
- Network model linear programming
- Stevens unix network programming
- Java interprocess communication example
- Weicc
- File and record locking in network programming
- Stevens unix
- Design of two pass assembler
- Modular design programming
- Interior design programming
- Modular design programming
- Dynamic programming general method
- Virtual circuit network uses
- Topology in computer
- Features of peer to peer network and client server network
- Network centric computing and network centric content
- Disadvantages of circuit switching
- 101 computing network
- Supply chain network design decisions
- Stealth network design
- Designing a secure local area network
- Resilient network design
- Network design and construction
- Network security design
- Network layer design
- Design issues of network layer
- Network layer design issues
- Supply chain network design decisions