problem with sockets under win32; premature connection termination

Downs default_357-line at yahoo.de
Sat May 12 12:28:14 PDT 2007


Yes, um, I kind of solved it for miniserv in the meantime ^^ But my solution is specific to HTTP.

The solution is simply to do another attempt at receiving data right before shutting down. ^^
Now, there are two possibilities.
a) The client transmits the header, then shuts down the connection (SocketShutdown.SEND).
  In this case, the server-side recv call will return zero - connection closed.
  BUT, strangely, it will also block until all the missing data is sent.
b) The client transmits the header, then keeps the connection open (to send another header?)
  In this case, we simply re-enqueue the socket and get persistent connections practically for free :D

The respective miniserv code looks like thus:

 >     try {
 >       push; /// if this succeeds, we have a persistant connection on our hands
 >       scope(success) first(this, (BufferingLayer buf) { buf.push_back(recv); logln("Re-enqueue!"); reenq(buf); });
 >     } catch (Exception e) close; /// nope; the client already shutdown his SEND. Ah well. Terminate.

Push basically asks the lowest member of the filter-chain (the socket back-end) to "push"
received data to the next higher element. It's basically a receive call.

Now, if there was a problem in-between, my socket back-end code will throw an exception (after the blocking recv returns).
In this case, we simply close the socket and are done with it. Otherwise, we add the additional data we received
to the front of our incoming buffer and re-enqueue the socket.

Easy, huh? ^^ I'm not sure how well this method translates to other protocols though, mainly because I don't completely
understand why it works myself :p

Well, good luck ~
  -- downs, happy



More information about the Digitalmars-d mailing list