Shutting down thread with Socket blocking for connection

Vidar Wahlberg canidae at exent.net
Tue Mar 6 10:00:07 PST 2012


On 2012-03-05 09:38, Dmitry Olshansky wrote:
> ...
> while(working){
>
> if(select(set, null, null, 10) > 0){ //10 usec wait on a socket, may do
> plain 0
> sock.accept(); // no blocking here
> }
> set.reset();
> set.add(sock);
> receiveTimeout(dur!"us"(1), (int code){ working = false; });
> }
>...

Thanks for the answers.

However, I still have a problem:
What I'd really like to do is have one thread listening for incoming 
connections and spawn a new thread for each connection (it's a simple 
HTTP server, I'm interested in learning about sockets & threads, I'm not 
looking for a HTTP server). "Socket.accept()" returns a Socket whenever 
a connection is made, my intention was to pass this to a new thread, but 
as I pointed out in the initial post I can't find a way to do so, and 
I'm unable to find a solution to this problem from your replies.
Does this mean that it's recommended to keep the listening socket and 
all the sockets it spawns in a single thread? I don't know a whole lot 
on how sockets works, but can't that easily cause poor performance when 
you're reading from or writing to multiple sockets?

I've not been able to find some D2 code that use multiple threads in 
conjunction with sockets, but if anyone know about some examples I'd be 
happy to read that.


More information about the Digitalmars-d-learn mailing list