Advanced Sockets - PowerPoint PPT Presentation

About This Presentation
Title:

Advanced Sockets

Description:

Office hours on Tuesdays between 4 and 6. The problem. You are a server and you want to listen for incoming connections as well as keep ... – PowerPoint PPT presentation

Number of Views:30
Avg rating:3.0/5.0
Slides: 10
Provided by: ashish9
Category:

less

Transcript and Presenter's Notes

Title: Advanced Sockets


1
Advanced Sockets
Introduction to Networking Instructor Prof.
Aleksandar Kuzmanovic
John Otto TA , Intro to Networking Jan 24,
2007 Recital 3
2
Homework grader
  • Geet Duggal, geet_at_geetduggal.com.
  • Office hours on Tuesdays between 4 and 6

3
The problem
  • You are a server and you want to listen for
    incoming connections as well as keep reading from
    the connections you already have.
  • Blocking
  • "block" is techie jargon for "sleep".
  • Lots of functions block.
  • accept() blocks.
  • All the recv() functions block

4
The select call
Enables you to deal with many clients at the same
time
HOW ?
Monitors several sockets at the same time. tell
you which ones are ready for reading, which are
ready for writing,
5
Synopsis of select()
  • include ltsys/time.hgt
  • include ltsys/types.hgt
  • include ltunistd.hgt
  • int select(int numfds, fd_set readfds, fd_set
    writefds, fd_set exceptfds, struct timeval
    timeout)
  • It'll tell you which ones are ready for reading,
    which are ready for writing, and which sockets
    have raised exceptions, if you really want to
    know that.

6
Manipulate sets
  • Each set is of the type fd_set. The following
    macros operate on this type
  • FD_ZERO(fd_set set) -- clears a file descriptor
    set
  • FD_SET(int fd, fd_set set) -- adds fd to the set
  • FD_CLR(int fd, fd_set set) -- removes fd from
    the set
  • FD_ISSET(int fd, fd_set set) -- tests to see if
    fd is in the set
  • Example - when select() returns, readfds will be
    modified to reflect which of the file descriptors
    you selected is ready for reading. Test them
    with the macro FD_ISSET(),

7
THE CODE
  • A multi-person chat server

8
DEMO
A multi-person chat server
9
Important Points
  • Accepting new connections via select
  • Client closes connection
  • Select returns socket ready to read
  • recv() will return 0.
Write a Comment
User Comments (0)
About PowerShow.com