Re: http webserver using select()
- From: "Hubble" <reiner@xxxxxxxxx>
- Date: 18 Sep 2006 00:18:18 -0700
Thr wrote:
Hi,
I read some books about network programming..
Still, I have some questions:
1. let's assume that we have http server written using nonblocking i/o
and select().
How is it possible to server many requests simultaneously?
I mean - we have, for example, static binary file that is 700 MB long
(and many other, much smaller files, that are, for example, html
webpages)
.
Let's assume that server found new socket descriptor with select(), and
accepted it..
after receiving data from client, and after parsing request header we
found that he want this big file, and we started to send him chunks of
data..
I think that this situation should block sending data for other
clients, does it (until this 700 MB file is send) ?
If not, how is it possible to serve simultaneously many connections ?
"Binding" file descriptor with open file to accepted socket descriptor
with struct{} and queuing some chunks of data with fifo, and then -
iterating to the next socket descriptor returned by select()?
The trick lies in the accept call which generates a new file descriptor
for each
accepted connection. So you have on fd for the listening socket and
other
fds for each connection. Using select, you can detect which of the
connections
are read/writeable and if the listen socket will accept a new
connection.
The basic scheme is
generate socket fd via socket(2) -> listening socket
use bind(2) to set addr/port info
use listen(2)
loop
generate fd_set containing 1 bits for listening socket and each
connection socket
call select(2)
when fd_set is set for the listening socket, call accept(2) and
remember the
new connection.
For other 1 bits in fd_set, handle I/O
- if a fd is ready for read, read *all* pending data (this is
imporant)
- if you detect an eof, close or shutdown the socket
- if fd is ready for write, you can write data (if you want)
- if read has reached eof and you do not want to write more
data, close
socket and remove it from open connections.
end loop
The I/O handling is tricky, since the documentation is not really
clear. If says basically, that select will signal if a read I/O channel
is ready to read and a write I/O channel can receive data. If you try
to implement this using non blocking I/O, you must be aware that the
ready bit is only set on read fd's when *new* data arrives. If you read
the data
partially, you will be signalled that data is left only when the socket
delivers more data.
A much more simple approach is without select:
socket/bind/listen
while (newfd=accept)
{ fork
in child: handle I/O via newfd (you can dup this to
stdin/stdout)
}
Hubble.
.
- References:
- http webserver using select()
- From: Thr
- http webserver using select()
- Prev by Date: Re: http webserver using select()
- Next by Date: Re: http webserver using select()
- Previous by thread: Re: http webserver using select()
- Next by thread: Re: http webserver using select()
- Index(es):
Relevant Pages
|