Thread overview
Socket: The connection was reset
Feb 09, 2012
Nrgyzer
Feb 10, 2012
DNewbie
Feb 10, 2012
nrgyzer
Feb 10, 2012
DNewbie
Feb 10, 2012
nrgyzer
Feb 10, 2012
DNewbie
February 09, 2012
Hi guys,

I wrote the following few lines:

private {

	import std.socket;

}

void main() {

	Socket s = new TcpSocket();
	s.bind(new InternetAddress(80));
	s.listen(0);

	while(true) {

		Socket cs = s.accept();
		cs.sendTo("HTTP/1.1 200 OK\r\nContent-Length: 11\r\n\r\nHello World");
		cs.close();

	}

	s.close();

}

The code compiles successfully and I also the server also responses with "Hello World", but when I reload the page I sometimes get the following error (Firefox): "The
connection was reset" - I also often get the same error in other browsers. Is there anything wrong with the code?

Thanks in advance!
February 10, 2012
Try this

	while(true) {
		Socket cs = s.accept();
		cs.receive(new byte[1024]);
		cs.sendTo("HTTP/1.1 200 OK\r\nContent-Length: 11\r\n\r\nHello World");
		cs.close();
	}



On Thu, Feb 9, 2012, at 07:31 PM, Nrgyzer wrote:
> Hi guys,
> 
> I wrote the following few lines:
> 
> private {
> 
> 	import std.socket;
> 
> }
> 
> void main() {
> 
> 	Socket s = new TcpSocket();
> 	s.bind(new InternetAddress(80));
> 	s.listen(0);
> 
> 	while(true) {
> 
> 		Socket cs = s.accept();
> 		cs.sendTo("HTTP/1.1 200 OK\r\nContent-Length: 11\r\n\r\nHello World");
> 		cs.close();
> 
> 	}
> 
> 	s.close();
> 
> }
> 
> The code compiles successfully and I also the server also responses with
> "Hello World", but when I reload the page I sometimes get the following
> error (Firefox): "The
> connection was reset" - I also often get the same error in other
> browsers. Is there anything wrong with the code?
> 
> Thanks in advance!
> 


-- 

  D
February 10, 2012
Works perfectly, thanks :)
But... how can I read the complete HTTP-header? When I try the following:

	string header;
	ubyte[1024] buffer;
	while (cs.receive(buffer)) header ~= buffer;

... it works as long as the header doesn't have a length like 1024, 2048, 3072... Otherwise cs.receive() blocks forever and the server doesn't respond anything. Is there any solution how to prevent/solve this problem?


== Auszug aus DNewbie (run3@myopera.com)'s Artikel
> Try this
> 	while(true) {
> 		Socket cs = s.accept();
> 		cs.receive(new byte[1024]);
> 		cs.sendTo("HTTP/1.1 200 OK\r\nContent-Length: 11\r\n\r\nHello
World");
> 		cs.close();
> 	}
> On Thu, Feb 9, 2012, at 07:31 PM, Nrgyzer wrote:
> > Hi guys,
> >
> > I wrote the following few lines:
> >
> > private {
> >
> > 	import std.socket;
> >
> > }
> >
> > void main() {
> >
> > 	Socket s = new TcpSocket();
> > 	s.bind(new InternetAddress(80));
> > 	s.listen(0);
> >
> > 	while(true) {
> >
> > 		Socket cs = s.accept();
> > 		cs.sendTo("HTTP/1.1 200 OK\r\nContent-Length: 11\r\n\r\nHello
World");
> > 		cs.close();
> >
> > 	}
> >
> > 	s.close();
> >
> > }
> >
> > The code compiles successfully and I also the server also responses with
> > "Hello World", but when I reload the page I sometimes get the following
> > error (Firefox): "The
> > connection was reset" - I also often get the same error in other
> > browsers. Is there anything wrong with the code?
> >
> > Thanks in advance!
> >

February 10, 2012
nrgyzer,
please check the return value of 'receive'.

http://dlang.org/phobos/std_socket.html#receive



On Fri, Feb 10, 2012, at 02:06 PM, nrgyzer wrote:
> Works perfectly, thanks :)
> But... how can I read the complete HTTP-header? When I try the following:
> 
> 	string header;
> 	ubyte[1024] buffer;
> 	while (cs.receive(buffer)) header ~= buffer;
> 
> ... it works as long as the header doesn't have a length like 1024, 2048,
> 3072... Otherwise cs.receive() blocks forever and the server doesn't
> respond
> anything. Is there any solution how to prevent/solve this problem?
> 
> 
> == Auszug aus DNewbie (run3@myopera.com)'s Artikel
> > Try this
> > 	while(true) {
> > 		Socket cs = s.accept();
> > 		cs.receive(new byte[1024]);
> > 		cs.sendTo("HTTP/1.1 200 OK\r\nContent-Length: 11\r\n\r\nHello
> World");
> > 		cs.close();
> > 	}
> > On Thu, Feb 9, 2012, at 07:31 PM, Nrgyzer wrote:
> > > Hi guys,
> > >
> > > I wrote the following few lines:
> > >
> > > private {
> > >
> > > 	import std.socket;
> > >
> > > }
> > >
> > > void main() {
> > >
> > > 	Socket s = new TcpSocket();
> > > 	s.bind(new InternetAddress(80));
> > > 	s.listen(0);
> > >
> > > 	while(true) {
> > >
> > > 		Socket cs = s.accept();
> > > 		cs.sendTo("HTTP/1.1 200 OK\r\nContent-Length: 11\r\n\r\nHello
> World");
> > > 		cs.close();
> > >
> > > 	}
> > >
> > > 	s.close();
> > >
> > > }
> > >
> > > The code compiles successfully and I also the server also responses with
> > > "Hello World", but when I reload the page I sometimes get the following
> > > error (Firefox): "The
> > > connection was reset" - I also often get the same error in other
> > > browsers. Is there anything wrong with the code?
> > >
> > > Thanks in advance!
> > >
> 
> 


-- 

  D
February 10, 2012
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?

== Auszug aus DNewbie (run3@myopera.com)'s Artikel
> nrgyzer,
> please check the return value of 'receive'.
> http://dlang.org/phobos/std_socket.html#receive
> On Fri, Feb 10, 2012, at 02:06 PM, nrgyzer wrote:
> > Works perfectly, thanks :)
> > But... how can I read the complete HTTP-header? When I try the following:
> >
> > 	string header;
> > 	ubyte[1024] buffer;
> > 	while (cs.receive(buffer)) header ~= buffer;
> >
> > ... it works as long as the header doesn't have a length like 1024, 2048,
> > 3072... Otherwise cs.receive() blocks forever and the server doesn't
> > respond
> > anything. Is there any solution how to prevent/solve this problem?
> >
> >
> > == Auszug aus DNewbie (run3@myopera.com)'s Artikel
> > > Try this
> > > 	while(true) {
> > > 		Socket cs = s.accept();
> > > 		cs.receive(new byte[1024]);
> > > 		cs.sendTo("HTTP/1.1 200 OK\r\nContent-Length: 11\r\n\r\nHello
> > World");
> > > 		cs.close();
> > > 	}
> > > On Thu, Feb 9, 2012, at 07:31 PM, Nrgyzer wrote:
> > > > Hi guys,
> > > >
> > > > I wrote the following few lines:
> > > >
> > > > private {
> > > >
> > > > 	import std.socket;
> > > >
> > > > }
> > > >
> > > > void main() {
> > > >
> > > > 	Socket s = new TcpSocket();
> > > > 	s.bind(new InternetAddress(80));
> > > > 	s.listen(0);
> > > >
> > > > 	while(true) {
> > > >
> > > > 		Socket cs = s.accept();
> > > > 		cs.sendTo("HTTP/1.1 200 OK\r\nContent-Length: 11\r\n\r\nHello
> > World");
> > > > 		cs.close();
> > > >
> > > > 	}
> > > >
> > > > 	s.close();
> > > >
> > > > }
> > > >
> > > > The code compiles successfully and I also the server also responses with
> > > > "Hello World", but when I reload the page I sometimes get the following
> > > > error (Firefox): "The
> > > > connection was reset" - I also often get the same error in other
> > > > browsers. Is there anything wrong with the code?
> > > >
> > > > Thanks in advance!
> > > >
> >
> >

February 10, 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