Multiplexing using select

 

Chapters 6 of Textbook
Based on I/O multiplexing useing Select
 

· Select  call:

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


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 */

 

TCP Echo Server using Select (instead of fork):


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  call:

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

 

values of timeout:


       INFTIM     indifinate wait

          0          no-wait  (poll)
         T          T  millisec


TCP Echo Server using Poll:


Example3: 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)

 


 

 

Select in Java