I/O Multiplexing

 

Ø     Select

Ø     Concurrent Client

Ø     Concurrent TCP Server Using Select

Ø     Poll

Ø     Concurrent TCP Server Using Poll



 


 

Ø    Select:

 

n = select ( length, readset, writeset, exceptset, time)

 

values of  time:

 

       NULL      indifinate wait
          0            no-wait  (poll)
                    wait T  (sec + micro-sec)


Under what conditions a descriptor is ready?

à      ready for reading:

§ Receive buffer has data > low-water mark

(default is 1, set SO_RCVLOWWAT socket option to change).

§ Receive FIN from the other side.

§ A listen socket has a completed connection.

§ An error condition (e.g., receive RST ).

à      ready for writing:

§ Send buffer has space  > low-water mark

   (default is 2048, set SO_SNDLOWWAT socket option to change).

§ Sent FIN out (close write end).

§ An error condition.

à      ready for exception condition:

E.g., out-of-band date is received



 


Ø  Concurrent Client

 

Example1:  echo client:

 

  strcliselect01.c   &  it is used in tcpcli01.c

 

  Server: (run an echo server at port 9877)
      % cd  /home/cs779/stevens3rd.book/unpv13e/tcpcliserv
      % tcpserv01


  Client:
      %
cd  /home/cs779/stevens3rd.book/unpv13e/select
       
%   tcpcli01 127.0.0.1
       %  tcpcli01 127.0.0.1 
< infile  > outfile

      Problem:
                   infile is NOT the same as outfile,
                   since we exit when stdin is done:

                   if (Fgets(sendline, MAXLINE, fp) == NULL)

                        return; 

        

ü   Example2:

 strcliselect02.c   &  it is used in tcpcli02.c
    

 The Problem above is fixed (use of  shutdown):
         % tcpcli02  127.0.0.1   < infile  > outfile

          if (Fgets(sendline, MAXLINE, fp) == NULL) {

                    stdineof = 1;

                    Shutdown(sockfd, SHUT_WR);  /* send FIN */



 


Ø    Concurrent TCP Server using Select

 

ü   Example3: tcpservselect01.c

 

Server:

      % cd  /home/cs779/stevens3rd.book/unpv13e/tcpcliserv
      % tcpservselect01

 

Client:

      % cd  /home/cs779/stevens3rd.book/unpv13e/tcpcliserv
      %   tcpcli01 127.0.0.1
     
(repeat for as many clients as you like)


     Problem: denial of service attack
          

%   tcpcli012  127.0.0.1

 

    The user sends data without newline,

    The server will hung (blocked) forever!

 

 tcpcli012  uses  

 str_cli12.c  (this program dos not send newline!)

 



 

 


Ø    Poll:

 

struct pollfd   fds[MaxNofds];

 

client[i].fd = sockfd;

client[i].events = POLLRDNORM;

 

n = poll (struct  pollfd    fds[], nfds_t  sizefds,  int  timeout);

 

values of timeout:


       INFTIM     indifinate wait

          0          no-wait  (poll)
         T          T  millisec


  if (client[i].revents & POLLRDNORM) {

          read (sockfd, ….);

  }

 

 


Ø    Concurrent TCP Server using Poll



ü   Example4: tcpservpoll01.c

 

Server:

      % cd  /home/cs779/stevens3rd.book/unpv13e/tcpcliserv
      % tcpservpoll01

 

Client:

      % cd  /home/cs779/stevens3rd.book/unpv13e/tcpcliserv
        %   tcpcli01 127.0.0.1 OR telnet 127.0.0.1  9877
     
(repeat for as many clients as you like)