Concurrent Server Design Alternatives



Similar documents
Elementary Name and Address. Conversions

Implementing Network Software

Domain Name System (1)! gethostbyname (2)! gethostbyaddr (2)!

Socket Programming. Kameswari Chebrolu Dept. of Electrical Engineering, IIT Kanpur

CSE 333 SECTION 6. Networking and sockets

Name and Address Conversions

Porting applications & DNS issues. socket interface extensions for IPv6. Eva M. Castro. ecastro@dit.upm.es. dit. Porting applications & DNS issues UPM

Introduction to Socket Programming Part I : TCP Clients, Servers; Host information

Socket Programming. Srinidhi Varadarajan

Socket Programming in C/C++

IPv6 Enabling CIFS/SMB Applications

Introduction to Socket programming using C

Tutorial on Socket Programming

BSD Sockets Interface Programmer s Guide

UNIX Sockets. COS 461 Precept 1

ICT SEcurity BASICS. Course: Software Defined Radio. Angelo Liguori. SP4TE lab.

Network Programming with Sockets. Process Management in UNIX

Socket Programming. Request. Reply. Figure 1. Client-Server paradigm

IT304 Experiment 2 To understand the concept of IPC, Pipes, Signals, Multi-Threading and Multiprocessing in the context of networking.

INTRODUCTION UNIX NETWORK PROGRAMMING Vol 1, Third Edition by Richard Stevens

Network Programming with Sockets. Anatomy of an Internet Connection

Elementary Name and Address Conversions

The POSIX Socket API

TCP/IP - Socket Programming

ELEN 602: Computer Communications and Networking. Socket Programming Basics

Hostnames. HOSTS.TXT was a bottleneck. Once there was HOSTS.TXT. CSCE515 Computer Network Programming. Hierarchical Organization of DNS

UNIX. Sockets. mgr inż. Marcin Borkowski

NS3 Lab 1 TCP/IP Network Programming in C

Best practices in IPv6 enabled networking software development.

Unix Network Programming

Programmation Systèmes Cours 9 UNIX Domain Sockets

Overview. Socket Programming. Using Ports to Identify Services. UNIX Socket API. Knowing What Port Number To Use. Socket: End Point of Communication

Programming with TCP/IP Best Practices

Writing a C-based Client/Server

Lab 4: Socket Programming: netcat part

Operating Systems Design 16. Networking: Sockets

Networks. Inter-process Communication. Pipes. Inter-process Communication

Session NM059. TCP/IP Programming on VMS. Geoff Bryant Process Software

TFTP Usage and Design. Diskless Workstation Booting 1. TFTP Usage and Design (cont.) CSCE 515: Computer Network Programming TFTP + Errors

IP Network Layer. Datagram ID FLAG Fragment Offset. IP Datagrams. IP Addresses. IP Addresses. CSCE 515: Computer Network Programming TCP/IP

sys socketcall: Network systems calls on Linux

Windows Socket Programming & IPv6 Translation Middleware

Lecture 17. Process Management. Process Management. Process Management. Inter-Process Communication. Inter-Process Communication

Client / Server Programming with TCP/IP Sockets

Application Architecture

VMCI Sockets Programming Guide VMware ESX/ESXi 4.x VMware Workstation 7.x VMware Server 2.0

Warning for programmers. Network programming: sockets. ISO/OSI, TCP/IP, network programming. Exercise copying data. Error functions.

Lecture 7: Introduction to Sockets

Communication Networks. Introduction & Socket Programming Yuval Rochman

416 Distributed Systems. Feb 24, 2016 DNS and CDNs

Computer Networks Network architecture

IPv6 Applications. Formation IPv6 RENATER -Rouen, FR

What is CSG150 about? Fundamentals of Computer Networking. Course Outline. Lecture 1 Outline. Guevara Noubir noubir@ccs.neu.

DNS Domain Name System

Writing Client/Server Programs in C Using Sockets (A Tutorial) Part I. Session Greg Granger grgran@sas. sas.com. SAS/C & C++ Support

A Client-Server Transaction. Systemprogrammering 2006 Föreläsning 8 Network Programming. Hardware Org of a Network Host.

Java Network. Slides prepared by : Farzana Rahman

CSI 402 Lecture 13 (Unix Process Related System Calls) 13 1 / 17

Transport Layer Protocols

Network-Oriented Software Development. Course: CSc4360/CSc6360 Instructor: Dr. Beyah Sessions: M-W, 3:00 4:40pm Lecture 2

CS 213, Fall 2000 Lab Assignment L5: Logging Web Proxy Assigned: Nov. 28, Due: Mon. Dec. 11, 11:59PM

TCP/IP Support Enhancements

Network Programming. Chapter The Client-Server Programming Model

A Client Server Transaction. Introduction to Computer Systems /18 243, fall th Lecture, Nov. 3 rd

Limi Kalita / (IJCSIT) International Journal of Computer Science and Information Technologies, Vol. 5 (3), 2014, Socket Programming

Computer Networks. Chapter 5 Transport Protocols

Generalised Socket Addresses for Unix Squeak

Networks class CS144 Introduction to Computer Networking Goal: Teach the concepts underlying networks Prerequisites:

Implementing and testing tftp

Command Manual - Network Protocol Quidway S3000 Series Ethernet Switches. Table of Contents

Software changes for Website and Application IPv6 Readiness

TCP/IP Fundamentals. OSI Seven Layer Model & Seminar Outline

Network Programming. Writing network and internet applications.

Computer Networks UDP and TCP

System calls. Problem: How to access resources other than CPU

Internet Control Protocols Reading: Chapter 3

Chapter 3. Internet Applications and Network Programming

Java Programming: Sockets in Java

MODBUS MESSAGING ON TCP/IP IMPLEMENTATION GUIDE V1.0b CONTENTS

ICOM : Computer Networks Chapter 6: The Transport Layer. By Dr Yi Qian Department of Electronic and Computer Engineering Fall 2006 UPRM

CS640: Computer Networks. Naming /ETC/HOSTS

An Advanced 4.4BSD Interprocess Communication Tutorial

Overview. Securing TCP/IP. Introduction to TCP/IP (cont d) Introduction to TCP/IP

Beej's Guide to Network Programming

q Connection establishment (if connection-oriented) q Data transfer q Connection release (if conn-oriented) q Addressing the transport user

DESIGN AND IMPLEMENT AND ONLINE EXERCISE FOR TEACHING AND DEVELOPMENT OF A SERVER USING SOCKET PROGRAMMING IN C

Network Programming TDC 561

Transport Layer. Chapter 3.4. Think about

CS335 Sample Questions for Exam #2

Packet Sniffing and Spoofing Lab

1 Posix API vs Windows API

TCP Flow Control. TCP Receiver Window. Sliding Window. Computer Networks. Lecture 30: Flow Control, Reliable Delivery

IBM i Version 7.2. Programming Socket programming IBM

CS3250 Distributed Systems

Libmonitor: A Tool for First-Party Monitoring

Transcription:

CSCE 515: Computer Network Programming ------ Advanced Socket Programming Wenyuan Xu Concurrent Server Design Alternatives Department of Computer Science and Engineering University of South Carolina Ref: Dave Hollinger Ref: UNP Chapter 7, 11, 30 Concurrent Server Design Alternatives One child process per client Spawn one thread per client Preforking multiple processes Prethreaded Server 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() ). One thread per client Almost like using fork - call pthread_create instead. Using threads makes it easier (less overhead) to have sibling processes share information. Sharing information must be done carefully (use pthread_mutex) Example main(int argc, char **argv) { listenfd=socket( ) Bind(listenfd ) Listen(listenfd,LISTENQ); Signal(SIGGHLD, sig_chld); Signal(SIGINT,sig_int); For( ; ;) { connfd = Accept(listenfd, ); if ( (pid = Fork())==0) { Close(listendf); doit(connfd); Close(connfd); exit(0); Close(connfd); Process version main(int argc, char **argv) { pthread_t tid; listenfd=socket( ) Bind(listenfd ) Listen(listenfd,LISTENQ); For( ; ;) { connfd = Accept(listenfd, ); Pthread_creat(&tid, NULL, &doit, (void *) connfd); static void doit (void *arg) { Close( (int) arg); return NULL; Thread version

Prefork() d Server Creating a new process for each client is expensive. We can create a bunch of processes, each of which can take care of a client. Each child process is an iterative server. Prefork() d TCP Server Initial process creates socket and binds to well known address. Process now calls fork() a bunch of times. All children call accept(). The next incoming connection will be handed to one child. Example listen() main(int argc, char **argv) { listenfd=socket( ) Bind(listenfd ) Listen(listenfd,LISTENQ); Signal(SIGGHLD, sig_chld); Signal(SIGINT,sig_int); For( ; ;) { connfd = Accept(listenfd, ); if ( (pid = Fork())==0) { Close(listendf); doit(connfd); Close(connfd); exit(0); Close(connfd); main(int argc, char **argv) { listenfd=socket( ) Bind(listenfd ) Listen(listenfd,LISTENQ); Signal(SIGGHLD, sig_chld); Signal(SIGINT,sig_int); for(i=0;i<nchildren;i++) { if ((pid = Fork())==0) { for(;;) { connfd = Accept(listenfd, ); doit(connfd); Close(connfd); \\for \\if & for Server TCP accept Sum of both queues cannot exceed backlog 3-way handshake complete arriving SN Completed connection queue Incomplete connection queue Process version Prefork version Preforking As the book shows, having too many preforked children can be bad. Using dynamic process allocation instead of a hard-coded number of children can avoid problems. The parent process just manages the children, doesn t worry about clients. Sockets library vs. system call A preforked TCP server won t usually work the way we want if sockets is not part of the kernel: calling accept() is a library call, not an atomic operation. We can get around this by making sure only one child calls accept() at a time using some locking scheme.

Prethreaded Server Same benefits as preforking. Can also have the main thread do all the calls to accept() and hand off each client to an existing thread. What s the best server design for my application? Many factors: expected number of simultaneous clients. Transaction size (time to compute or lookup the answer) Variability in transaction size. Available system resources (perhaps what resources can be required in order to run the service). Server Design It is important to understand the issues and options. Knowledge of queuing theory can be a big help. ou might need to test a few alternatives to determine the best design. Socket Options Posix name/address conversion It's important to know about some of these topics, although it might not be apparent how and when to use them. Details are in the book - we are just trying to get some idea of what can be done. Socket Options 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.

Option types Many socket options are Boolean flags indicating whether some feature is enabled (1) or disabled (0). Read-Only Socket Options Some options are readable only (we can t set the value). Other options are associated with more complex types including int, timeval, in_addr, sockaddr, etc. 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> 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). Socket and IP-layer socket options Level SOL_SOCKET IPPRORO_IP Optname SO_ERROR SO_LINGER SO_KEEPALIVE IP_HDRINCL Get Set N Flag Data type int linger int Int setsockopt() int setsockopt( int sockfd, int level, int optname, const void *opval, socklen_t optlen); IP_TOS int IPPROTO_TCP TCP_MAXSEG int TCP_NODELA int

Example: SO_LINGER Specifies how the close function operates for a connection-oriented protocol. #include <unistd.h> int close(int socketfd); Decrease the reference count for the descriptor If the reference count is 0: send any data that is already queued to be sent to the other end Normal TCP Connection termination sequence SO_LINGER Value is of type: struct linger { int l_onoff; /* 0 = off */ int l_linger; /* time in seconds */ ; Used to control whether and how long a call to close will wait for pending ACKS. connection-oriented sockets only. SO_LINGER usage By default, calling close() on a TCP socket will return immediately. The closing process has no way of knowing whether or not the peer received all data. Setting SO_LINGER means the closing process can determine that the peer machine has received the data (but not that the data has been read()!). SO_LINGER l_onoff = 1 & l_linger =0 TCP aborts the connections when it is closed l_onoff = 1 & l_linger!= 0 close return if either: all the data is sent and acked the linger time has expired. Check an example shutdown Starts TCP s normal connection termination sequence, regardless of the reference count #include <sys/socket.h> int shutdown(int sockfd, int howto); howto SHUT_RD: the read half of the connection is closed SHUT_WR: the write half of the connection is closed SHUT_RDWR: the read half and the write half of the connection are both closed shutdown() vs SO_LINGER Summary close returns immediately without waiting at all close lingers until the ACK of our FIN is received shutdown followed by a read waits until we receive the peer s FIN

General Options Protocol independent options. Handled by the generic socket system code. Some general options are supported only by specific types of sockets (SOCK_DGRAM, SOCK_STREAM). Some Generic Options SO_BROADCAST SO_DONTROUTE SO_ERROR SO_KEEPALIVE SO_LINGER SO_RCVBUF,SO_SNDBUF SO_REUSEADDR SO_BROADCAST Boolean option: enables/disables sending of broadcast messages. Underlying DL layer must support broadcasting! Applies only to SOCK_DGRAM sockets. Prevents applications from inadvertently sending broadcasts (OS looks for this flag when broadcast address is specified). SO_DONTROUTE Boolean option: enables bypassing of normal routing. Used by routing daemons. SO_ERROR Integer value option. The value is an error indicator value (similar to errno). Readable (get able) only! Reading (by calling getsockopt()) clears any pending error. SO_KEEPALIVE Boolean option: enabled means that STREAM sockets should send a probe to peer if no data flow for a long time. Used by TCP - allows a process to determine whether peer process/host has crashed. Consider what would happen to an open telnet connection without keepalive. Detect half-open connections and terminate them

SO_RCVBUF SO_SNDBUF Integer values options - change the receive and send buffer sizes. Can be used with STREAM and DGRAM sockets. With TCP, When should this option be set? this option effects the window size used for flow control - must be established before connection is made. SO_REUSEADDR Boolean option: enables binding to an address (port) that is already in use. By default, bind fails when the listening server is trying to bind a port that is part of an existing connection. How? SO_REUSEADDR A listening server is started. A connection request arrives and a child process is spawned to handle that client. SO_REUSEADDR Used by servers that are transient - allows binding a passive socket to a port currently in use (with active sockets) by other processes. The listening server terminates, but the child continues to service the client on the existing connections. The listening server is restarted. Can be used to establish separate servers for the same service on different interfaces (or different IP addresses on the same interface). IP Options (IPv4): IPPROTO_IP IP_HDRINCL: used on raw IP sockets when we want to build the IP header ourselves. IP_TOS: allows us to set the Type-ofservice field in an IP header. IP_TTL: allows us to set the Time-to-live field in an IP header. TCP socket options (IPPROTO_TCP) TCP_MAXSEG: set the maximum segment size sent by a TCP socket.

another TCP socket option TCP_NODELA: can disable TCP s Nagle algorithm that delays sending small packets if there is unack d data pending. TCP_NODELA also disables delayed ACKS (TCP ACKs are cumulative). Socket Options Summary This was just an overview there are many details associated with the options described. There are many options that haven t been described. Our text is one of the best sources of information about socket options. Let s see an example: getsockopt(fd, IPPROTO_TCP, TCP_MAXSEG, &val, &len); Posix Name/Adress Conversion Posix name/address conversion We've seen gethostbyname and gethostbyaddr - these are protocol dependent. Not part of sockets library. Posix includes protocol independent functions: getaddrinfo() getnameinfo() gethostbyname struct hostent *gethostbyname( const char *hostname); struct hostent is defined in netdb.h: #include <netdb.h> struct hostent h_name h_aliases h_addrtype h_length h_addr_list struct hostent { char *h_name; char **h_aliases; int h_addrtype; //AF_INET int h_length;//4 char **h_addr_list; ; null null Official Name alias 1 alias 2 IP address 1 IP address 2

getaddrinfo, getnameinfo These functions provide name/address conversions as part of the sockets library. In the future it will be important to write code that can run on many protocols (IPV4, IPV6). Why getaddrinfo()? Puts protocol dependence in library (where it belongs). Same code can be used for many protocols (IPV4, IPV6) re-entrant function - gethostbyname is not! Important to threaded applications. getaddrinfo() int getaddrinfo( const char *hostname, const char *service, const struct addrinfo* hints, struct addrinfo **result); getaddrinfo() parameters hostname is a hostname or an address string (dotted decimal string for IP). service is a service name or a decimal port number string. getaddrinfo() replaces both gethostbyname() and getservbyname() struct addrinfo struct addrinfo { int ai_flags; int ai_family; int ai_socktype; int ai_protocol; size_t ai_addrlen; char *canonname; struct sockaddr *ai_addr; struct addrinfo *ai_next; ; Linked list! getaddrinfo() hints hints is an addrinfo * (can be NULL) that can contain: ai_flags (AI_PASSIVE, AI_CANONNAME ) ai_family (AF_XXX ) ai_socktype (SOCK_XXX ) ai_protocol (IPPROTO_TCP, etc.)

getaddrinfo() result result is returned with the address of a pointer to an addrinfo structure that is the head of a linked list. It is possible to get multiple structures: multiple addresses associated with the hostname. The service is provided for multiple socket types. addrinfo usage ai_flags ai_family ai_socktype ai_protocol ai_addrlen ai_canonname ai_addr ai_next int socket(int family,int type,int proto); Used in call to socket() Used in call to bind(), connect() or sendto() int bind( int sockfd, const struct sockaddr *myaddr, int addrlen); ai_flags ai_family ai_socktype ai_protocol ai_addrlen ai_canonname ai_addr ai_next getnameinfo() int getnameinfo( const struct sockaddr *sockaddr, socklen_t addrlen char *host, size_t hostlen, char *serv, size_t servlen, int flags); getnameinfo() looks up a hostname and a service name given a sockaddr Assignment & Next time Reading: UNP 7.1-7.6, 11.3, 11.4, 11.6, 11.17, 30 Next Lecture: Daemons and inetd