I have a listener that will pass arbitrary data, HTTP requests, to a network socket which is then delivered over TCP. This works fine for the first request but the listener does not accept subsequent new requests.
My question is:
If I have sock=accept(listener,(struct addr *)&sin, &sinlen); then, based on the socket function reference, the listener socket remains open and I should be able to re-call accept() any number of times for subsequent requests. Is this correct? If so, can someone more familiar than I with socket programming please explain how this code might look?
Yes, you can
accept()many times on the listening socket. To service multiple clients, you need to avoid blocking I/O — i.e., you can’t just read from the socket and block until data comes in. There are two approaches: you can service each client in its own thread (or its own process, by usingfork()on UNIX systems), or you can useselect(). Theselect()function is a way of checking whether data is available on any of a group of file descriptors. It’s available on both UNIX and Windows.