www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Re: Socket: The connection was reset

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
Feb 10 2012