Socket: The connection was reset

DNewbie run3 at myopera.com
Fri Feb 10 13:04:45 PST 2012



On Fri, Feb 10, 2012, at 07:44 PM, nrgyzer wrote:
> Yep, thanks... but I already checked out the return value and the problem
> is "If the socket is blocking, receive waits until there is data to be
> received.". The following
> socket blocks and the server doesn't respond:
> 
> while(true) {
> 
> 	Socket cs = s.accept();
> 	ubyte[] header;
> 	ubyte[1] buffer;
> 	while (cs.receive(buffer)) header ~= buffer;
> 
> 	cs.sendTo("HTTP/1.1 200 OK\r\nContent-Length: 11\r\n\r\nHello World");
> 	cs.close();
> 
> }
> 
> cs.receive() blocks (because no more data is available) - cs.sendTo() and
> cs.close() isn't called, because cs.receive() waits for more data. I can
> solve the problem by using
> non-blocking sockets:
> 
> while(true) {
> 
> 	Socket cs = s.accept();
> 	cs.blocking(false);
> 	ubyte[] header;
> 	ubyte[1] buffer;
> 	while (cs.receive(buffer)) header ~= buffer;
> 
> 	cs.sendTo("HTTP/1.1 200 OK\r\nContent-Length: 11\r\n\r\nHello World");
> 	cs.close();
> 
> }
> 
> But... how can I make sure that I got all data sent by the
> client/browser?

It depends on the protocol. In HTTP you should check if the receive buffer contains CRLF CRLF:
http://en.wikipedia.org/wiki/Hypertext_Transfer_Protocol#Example_session



More information about the Digitalmars-d-learn mailing list