Re: http webserver using select()




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.

.



Relevant Pages

  • Re: TcpClient
    ... If the client is completely finished, it should close the connection gracefully using shutdownor similar API, which will be signaled to you by a successful completion of a receive, with zero bytes. ... But it doesn't do so by providing you a way to poll for the "connectedness" of the socket. ... All that selectis telling you is that some sort of i/o event happened. ...
    (microsoft.public.dotnet.framework)
  • Re: WaitForMultipleObjects
    ... event handle acts as a server and is associated with a listening socket. ... accepted socket is bound to the second event again using the ... On XP all works as intended a connection can be made and data passed on ...
    (microsoft.public.windowsce.platbuilder)
  • WaitForMultipleObjects
    ... event handle acts as a server and is associated with a listening socket. ... accepted socket is bound to the second event again using the WSAEventSelect ... On XP all works as intended a connection can be made and data passed on the ...
    (microsoft.public.windowsce.platbuilder)
  • Re: How do you kill a completely locked up thread?
    ... The one way to cancel pending I/O on a socket and unwedge ... // Call BeginInvoke on delegate. ... // Note on last two parameters of Delegate BeginInvoke Method: ...
    (microsoft.public.dotnet.languages.csharp)
  • =?windows-1252?Q?Re=3A_If_shutdownOutput=28=29_doesn=92t_cause_other_end_to?= =?windows-1252
    ... connection to closed state. ... THe socket is reset instead of being closed, ... listening socket remains in LISTEN state. ... java docs say that ServerSocket only returns from accept ...
    (comp.lang.java.programmer)