100% found this document useful (2 votes)
957 views8 pages

Multiprotocol Service10

Multi-protocol servers allow a single server process to handle requests over multiple transport protocols like TCP and UDP. The server opens sockets for each protocol and uses I/O multiplexing to wait for incoming requests on any socket. When a socket is ready, the server services the corresponding request and protocol. For example, a daytime multi-protocol server opens both TCP and UDP sockets and uses select() to handle time requests over either protocol with a single process. This avoids needing separate servers for each protocol.

Uploaded by

HocLieuMo
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
100% found this document useful (2 votes)
957 views8 pages

Multiprotocol Service10

Multi-protocol servers allow a single server process to handle requests over multiple transport protocols like TCP and UDP. The server opens sockets for each protocol and uses I/O multiplexing to wait for incoming requests on any socket. When a socket is ready, the server services the corresponding request and protocol. For example, a daytime multi-protocol server opens both TCP and UDP sockets and uses select() to handle time requests over either protocol with a single process. This avoids needing separate servers for each protocol.

Uploaded by

HocLieuMo
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

12/6/2007

Contents

 Introduction
Multiprotocol/Multiservice  Multi-protocol server
Servers  Example of Multi-protocol server
 Multi-service server
 Example of multi-service server
 Super servers

INTRODUCTION One server per protocol


 Advantage: Ease of control UDP server TCP server

 Last time: Concurrent Connection-Oriented  Disadvantages:


 Overhead of replication
=
server - echo server - that supports multiple  Because many services can be
clients at the same time using one single accessed through either UDP or
process. .
TCP, each service can require
two servers.
 Today: Extension to more complicated  The cost to maintain
servers: consistency socket socket
for TCP
socket for a
TCP
 UDP and TCP servers should for UDP
 Single process server accommodating multiple both contain the same code requests requests connection

transport protocols needed to perform the


computation
 and also multiple services.

Multiprotocol Server Design Algorithm

 A multiprotocol server consists of a single process that  The server then uses I/O multiplexing to wait
uses I/O multiplexing to handle communication over for one of the sockets to become ready :
either UDP or TCP.
If the TCP socket The server uses accept to obtain the
becomes ready, a client new connection, and then
 The server initially opens two sockets has requested a TCP communicates with the client over
 one that uses a connectionless transport (UDP) connection. that connection.

 one that uses a connection-oriented transport (TCP).


If the UDP socket The server uses recvfrom to read the
becomes ready, a client request and record the sender's
has sent a request in the endpoint address
form of a UDP datagram.

1
12/6/2007

Process Structure of a sequential, Outline of a Multiprotocol Server


multiprotocol server 1.The multiprotocol 4.If the TCP socket
server consists of a becomes ready, the
single process server accepts the
new connection and
At any time, the server has at most handles requests
server using it
three sockets open: 2.The process opens
•one for UDP requests, master sockets for
both UDP and TCP
•one for TCP connection requests, 5.If the UDP socket
becomes ready, the
•a temporary one for an individual 3.It uses select to wait
server reads the
socket for socket for socket for
TCP connection request and responds
UDP TCP a TCP for either or both of
requests requests connection
them to become ready

Example – Multiprotocol DAYTIME Server


/* main- iterative server for DAYTIME service */
int main(int argc, *argv[])
/* daytimed.c – main : DAYTIME service for TCP and
{
UDP */
char *service = “daytime”; /*service name or port number */
/* include header files here */
struct sockaddr_in fsin; /* the from address of a client */
#define QLEN 5 /*max. connection queue length */
int alen; /* length of a client’s address */
#define LINELEN 128 char buf[LINELEN+1]; /* line buffer */
extern int errno; int tsock; /* TCP master socket */
int daytime(char buf[]) int usock; /* UDP socket */
int errexit (const char *format, …); fd_set rfds; /* read file descriptor set */
int passiveTCP (const char *service, int qlen);
/* check arguments - not detailed here*/
int passiveUDP (const char *service);

tsock = passiveTCP (service, QLEN); if ( FD_ISSET (tsock, &rfds)) {


usock = passiveUDP (service); int ssock;

FD_ZERO (&rfds); alen = sizeof (fsin);


ssock = accept(tsock,(struct sockaddr *)&fsin,
while(1) { &alen);
FD_SET (tsock, &rfds);
if ( ssock < 0)
FD_SET (usock, &rfds);
errexit (“accept: %s\n, strerror (errno));
if ( select (FD_SETSIZE, &rfds, (fd_set *) 0,
daytime(buf);
(fd_set *) 0, (struct timeval *) 0) < 0)
errexit (“select: %s\n”, strerror(errno)); (void) write (ssock, buf, strlen(buf));
(void) close (ssock);
}

2
12/6/2007

/* Daytime – fill the given buffer with the time of day */

if ( FD_ISSET (usock, &rfds)) { int daytime ( char buf[])


alen = sizeof (fsin); {
if ( recvfrom(usock, buf, sizeof(buf), 0, char *ctime();
(struct sockaddr *)&fsin, &alen) < 0 ) time_t now;
errexit (“recvfrom: %s\n, strerror (errno));
daytime(buf);
(void) time (&now);
(void) sendto (usock, buf, strlen(buf),0,
(struct sockaddr *)&fsin, alen); sprintf (buf, “%s”, ctime(&now));
} }
}

Concept of Shared Code Contents

 “A multi-protocol server design permits the  Introduction


designer to create a single procedure that  Multi-protocol server
responds to requests for a given service and  Example of Multi-protocol server
to call that procedure regardless of whether
requests arrive via UDP or TCP”  Multi-service server
 Example of multi-service server
 Super servers

Motivation for a Multiservice Server A Sequential Connectionless,


Multi-service Server
 Same as for multiprotocol server Mapping table

 Also reduces the total code required server Server


application
process

Master sockets Operating


(one for each service system
being offered)

3
12/6/2007

A Sequential Connectionless, Multi-service A Sequential Connection-Oriented, Multi-


Server (Continued.) service Server
Algorithm Mapping table

1. The server opens a set of UDP sockets and binds each to a server Server
well-known port. application
process
2. Keep a table to map sockets to services.
3. Call select to wait for a datagram to arrive.
4. When a datagram arrives, get the descriptor.
5. Look up the mapping table, find the right procedure.
6. compute a response and send a reply.
7. Go back to step 3.
Master sockets Socket for Operating
(one for each one individual system
service offered) connection

A Sequential Connection-Oriented, Multi- A Concurrent, Connection-Oriented,


service Server (Continued.) Multi-service Server
Algorithm Implementations
1. Create a socket for each service
2. Keep a table to map sockets to services.  The Master-slave Implementation
3. Call select to wait for an incoming connection request.
4. Call accept to create a slave socket.  A Single-Process Implementation.
5. Through the slave socket, interact with a client, and then close
it.
 Multi-thread Implementation
6. Go back to step 3.

A Concurrent, Connection-Oriented, A Concurrent, Connection-Oriented, Multi-


Multi-service Server (Continued.) service Server (Continued.)
Mapping table
A Master-Slave Implementation
server Server
application
process
1. Create a socket for each service.
2. Call select to wait for any connection request.
3. If one of the master sockets becomes ready, call accept to slave1 slaven
open a connection.
4. The master process closes the connection as soon as it has
created a slave process; The connection remains open in the … …
slave process.
5. The slave process communicates with the client over the
connection. Master sockets Socket for Operating
(one for each each individual system
service offered) connection

4
12/6/2007

A Single-Process Implementation A Single-Process Implementation


(Continued.)
Algorithm
1. Create a socket and bind to the well-known port for the service. Mapping table
Add this socket to a list. server Server
2. Call select to wait for I/O on existing sockets. application
process
3. If original socket is ready, call accept to obtain the next
connection, and add the new socket to the list.
4. If a socket other than the original is ready, call read to obtain
the next request, compute a response, and then send a reply.
5. Go back to step 2.

Master sockets Socket for Operating


(one for each each individual system
service offered) connection

Multiservice server - superd.c int TCPechod(int), TCPchargend(int), TCPdaytimed(int),


TCPtimed(int);
/* superd.c – main, doTCP*/ int errexit (const char *format, …);
/* include header files here */ int passiveTCP (const char *service, int qlen);
#define UDP_SERV 0 int passiveUDP (const char *service);
#define TCP_SERV 1 void doTCP( struct service *psv);
#define NOSOCK -1 struct service svent[ ] = {
extern int errno; {“echo”, TCP_SERV, NOSOCK, TCPechod},
struct service { {“chargen”, TCP_SERV, NOSOCK, TCPchargend},
char *sv_name; {“daytime”, TCP_SERV, NOSOCK, TCPdaytimed},
char sv_useTCP; {“time”, TCP_SERV, NOSOCK, TCPtimed},
int sv_sock; {0,0,0,0},
void (*sv_func) (int); };
}

#define QLEN 5 switch (argc) {


#define LINELEN 128 case 1: break;
extern u_short portbase; /*from passivesock() */
case 2: portbase = (u_short) atoi(argv[1]);
break;
/* main - Super-server main program */
int main(int argc, *argv[]) default: errexit(“usage: superd [portbase]\n”);
{ }
struct service *psv; /* service table pointer */
fd_set afds, rfds; /* read file descriptors */

5
12/6/2007

FD_ZERO (&afds); while (1) {


for (psv = &svent[0]; psv->sv_name; ++psv) memcpy(&rfds, &afds, sizeof(rfds));
{
if (psv->sv_useTCP)
if ( select (FD_SETSIZE, &rfds, (fd_set *) 0,
psv->sv_sock = passiveTCP(psv->sv_name, QLEN); (fd_set *) 0, (struct timeval *) 0) < 0)
else errexit (“select: %s\n”, strerror(errno));
psv->sv_sock = passiveUDP(psv->sv_name);
FD_SET(psv->sv_sock, &afds);
}

for (psv=&svent[0];psv->sv_name; ++psv){


/* doTCP - handle a TCP service connection request */
void doTCP ( struct service *psv) {
if (FD_ISSET(psv->sv_sock, &rfds)){
struct sockaddr_in fsin; /* request from addr. */
if (psv->sv_useTCP) int alen; /* from-address length */
doTCP(psv); int ssock;
else alen = sizeof( fsin);
psv->sv_func(psv->sv_sock); ssock = accept(psv->sv_sock, (struct sockaddr *) &fsin, &alen);
} if (ssock == NOSOCK)
} errexit (“accept: %s\n”, strerror(errno));
if (_beginthread((void (*)(void *)) psv->sv_func, 0,
}
(void *) ssock) ==(unsigned long) -1)
errexit (“_beginthread: %s\n”, strerror(errno));
}

Contents A Multiservice, Multiprotocol Server


-- A Super Server
 Introduction  Motivation:
 Multi-protocol server
 Offer many services without using
 Example of Multi-protocol server excessive resources (entries in the process
 Multi-service server table and swap space)
 Example of multi-service server  Reduces overhead without eliminating the
 Super servers functionality

6
12/6/2007

A Multiservice, Multiprotocol Server Calling separate programs from a


multiservice server
 Operates much the same as a conventional multiservice  Disadvantage of discussed designs is the
server.
lack of flexibility
 Initiates both UDP and TCP sockets or either one for the
same service.
 Changing the code for any single service requires
recompilation of the entire multiservice server
 Calls select to wait for any socket to become ready.
 We must terminate the server and recompile sources
 If a UDP socket becomes ready, the server reads the
datagram, computes a response, and sends a reply.  We should break a large multiservice server
 If a TCP socket becomes ready, the server calls accept to
into independent components
create a new connection to do communication by making  Using independently compiled program to handle
it either iterative or concurrent. each service
 Process call execve can help

Calling separate programs from a Server Configuration


multiservice server (Continued.)
Mapping table
 Super servers are configurable – the set of
fork used
server Server
services that the server handles can be
application
process changed without recompiling source code.
slave1 slaven
 Static configuration:
execve used
 Specified by a configuration file when server process
prog1 progn
begins execution
… …  Dynamic configuration:
 Occurs while super server is running. The server reads the
modified configuration file and changed the services it
Master sockets Socket for Operating
(one for each each individual system offers.
service offered) connection

BSD UNIX Super Server: inetd inetd

 Super server example: BSD UNIX program  inetd reads a configuration file that lists all
inetd the services it should handle.
 supplies many of the small TCP/IP services that
network managers use for testing
 inetd creates a socket for each listed
 Uses a configuration file (text file) to make
addition of new services easier
service, and adds the socket to a fd_set
 /etc/inetd.conf given to select()
 Dynamically configurable

7
12/6/2007

Fields of an entry in an inetd configuration file Example of /etc/inetd.conf


# comments start with #
Field Meaning
echo stream tcp nowait root internal
Service name Name of a service to be offered echo dgram udp wait root internal
chargen stream tcp nowait root internal
Socket Type Type of socket to use chargen dgram udp wait root internal
ftp stream tcp nowait root /usr/sbin/ftpd ftpd -l
Protocol Name of protocol to be used with service
telnet stream tcp nowait root /usr/sbin/telnetd telnetd
Wait status Wait (iterative) or nowait (concurrent) finger stream tcp nowait root /usr/sbin/fingerd fingerd
# Authentication
Userid Login id. (in UNIX root has absolute privilege) auth stream tcp nowait nobody /usr/sbin/in.identd in.identd
-l -e -o
Server program Name of service program to execute or # TFTP
internal to use the version of the code tftp dgram udp wait root /usr/sbin/tftpd tftpd -s
compiled into inetd /tftpboot

Arguments 0 or more arguments to be passed

xinetd Summary
A multiprotocol server
 Some versions of Unix provide a service very is: a single-process server
that can handle multiple
similar to inetd called xinetd. transport protocols
Both of them do :
 configuration scheme is different
 basic idea (functionality) is the same…
Reduce overhead
Save system
A multiservice server resources
is: a single-process server
that can handle multiple Make maintenance easier
services

You might also like