0% found this document useful (0 votes)
4 views

Multiplexing Example Program

Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Multiplexing Example Program

Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

After Server Successfully started and listening for incoming connections.

The code does the following:

 Create listening socket and initialize for select. We create the listening socket
using socket, bind, and listen and initialize our data structures assuming that the
only descriptor that we will select on initially is the listening socket.
 Block in select. select waits for something to happen, which is one of the following:
o The establishment of a new client connection.
o The arrival of data on the existing connection.
o A FIN on the existing connection.
o A RST on the existing connection.
 accept new connections.
o If the listening socket is readable, a new connection has been established.
o We call accept and update our data structures accordingly. We use the first
unused entry in the client array to record the connected socket.
o The number of ready descriptors is decremented, and if it is 0
(tcpcliserv/tcpservselect01.c#L62), we can avoid the next for loop. This lets us
use the return value from select to avoid checking descriptors that are not
ready.
 Check existing connections.
o In the second nested for loop, a test is made for each existing client
connection as to whether or not its descriptor is in the descriptor set returned
by select, and a line is read from the client and echoed back to the client.
Otherwsie, if the client closes the connection, read returns 0 and we update our
data structures accordingly.
o We never decrement the value of maxi, but we could check for this possibility
each time a client closes its connection.

#include "unp.h"

int
main(int argc, char **argv)
{
int i, maxi, maxfd, listenfd, connfd, sockfd;
int nready, client[FD_SETSIZE];
ssize_t n;
fd_set rset, allset;
char buf[MAXLINE];
socklen_t clilen;
struct sockaddr_in cliaddr, servaddr;

listenfd = Socket(AF_INET, SOCK_STREAM, 0);

bzero(&servaddr, sizeof(servaddr));
servaddr.sin_family = AF_INET;
servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
servaddr.sin_port = htons(SERV_PORT);
Bind(listenfd, (SA *) &servaddr, sizeof(servaddr));

Listen(listenfd, LISTENQ);

maxfd = listenfd; /* initialize */


maxi = -1; /* index into client[] array */
for (i = 0; i < FD_SETSIZE; i++)
client[i] = -1; /* -1 indicates available entry */
FD_ZERO(&allset);
FD_SET(listenfd, &allset);
/* end fig01 */

/* include fig02 */
for ( ; ; ) {
rset = allset; /* structure assignment */
nready = Select(maxfd+1, &rset, NULL, NULL, NULL);

if (FD_ISSET(listenfd, &rset)) { /* new client connection */


clilen = sizeof(cliaddr);
connfd = Accept(listenfd, (SA *) &cliaddr, &clilen);

printf("new client: %s, port %d\n",


Inet_ntop(AF_INET, &cliaddr.sin_addr, 4, NULL),
ntohs(cliaddr.sin_port));

for (i = 0; i < FD_SETSIZE; i++)


if (client[i] < 0) {
client[i] = connfd; /* save descriptor */
break;
}
if (i == FD_SETSIZE)
err_quit("too many clients");
FD_SET(connfd, &allset); /* add new descriptor to set */
if (connfd > maxfd)
maxfd = connfd; /* for select */
if (i > maxi)
maxi = i; /* max index in client[] array
*/

if (--nready <= 0)
continue; /* no more readable descriptors
*/
}

for (i = 0; i <= maxi; i++) { /* check all clients for data


*/
if ( (sockfd = client[i]) < 0)
continue;
if (FD_ISSET(sockfd, &rset)) {
if ( (n = Read(sockfd, buf, MAXLINE)) == 0) {
/* connection closed by client */
Close(sockfd);
FD_CLR(sockfd, &allset);
client[i] = -1;
} else
Writen(sockfd, buf, n);

if (--nready <= 0)
break; /* no more readable descriptors
*/
}
}
}
}
/* end fig02 */

You might also like