# Building a simple http server using juce sockets

**URL:** <https://forum.juce.com/t/building-a-simple-http-server-using-juce-sockets/20217>\
**Category:** General JUCE discussion\
**Created:** [December 9, 2016, 10:25pm UTC](https://forum.juce.com/t/building-a-simple-http-server-using-juce-sockets/20217 "2016-12-09T22:25:36Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![BK8](https://avatars.discourse-cdn.com/v4/letter/b/6de8d8/32.png) [@BK8](https://forum.juce.com/u/BK8)\
**Post date:** [December 9, 2016, 10:25pm UTC](https://forum.juce.com/t/building-a-simple-http-server-using-juce-sockets/20217/1 "2016-12-09T22:25:36Z")

</div>

I’m trying to build a simple server using sockets but when I try to connect using local host on Chrome, I get no response and Chrome tries to load the page forever. But I’m getting the request of the web browser on my aplication. I already turn off the firewall but it is not working. Im not sure if the way I’m writing to the socket is correct, please help.

```
#include "../JuceLibraryCode/JuceHeader.h"
#include <string>
#define MAX_URL_SIZE 2000

//==============================================================================
int main (int argc, char* argv[])
{

    StreamingSocket* socket = new StreamingSocket();
	StreamingSocket* conection;
	
	bool listening=socket->createListener(8080,"127.0.0.1");
	

	char buffer[MAX_URL_SIZE];

	
	char response[] ="HTTP/1.1 200 OK\r\n"
			"Content-Type: text/html; charset=UTF-8\r\n\r\n"
			"Hello, world!\r\n";
	
	if(listening)
	{
		printf("Listening to ip: 127.0.0.1 ip\n");
		while(1){

			conection=socket->waitForNextConnection();
		
			if(conection!=nullptr){
				conection->read (buffer,MAX_URL_SIZE-1, true);
				
				bool isReady;
				

				printf("%s \n",buffer);

				conection->waitUntilReady(isReady,-1);
				conection->write(response,sizeof(response));
				
				conection->close();

			}
		 }
	}
	else
		printf("Conection Error \n");
    return 0;
}

```

---

<div class="post-metadata">

**Author:** ![BK8](https://avatars.discourse-cdn.com/v4/letter/b/6de8d8/32.png) [@BK8](https://forum.juce.com/u/BK8)\
**Post date:** [December 9, 2016, 11:10pm UTC](https://forum.juce.com/t/building-a-simple-http-server-using-juce-sockets/20217/2 "2016-12-09T23:10:10Z")

</div>

The main problem was I bloking the conection, now It is working.

```
#include "../JuceLibraryCode/JuceHeader.h"
#include <string>
#define MAX_URL_SIZE 2000

//==============================================================================
int main (int argc, char* argv[])
{

    StreamingSocket* socket = new StreamingSocket();
	StreamingSocket* conection;
	
	
	bool listening=socket->createListener(8080,"127.0.0.1");
	

	char buffer[MAX_URL_SIZE];

	
	char response[] ="HTTP/1.1 200 OK\r\n"
					"Content-Type: text/html; charset=UTF-8\r\n\r\n"
					"<p>This is a paragraph.</p>"
					"<p>This is another paragraph.</p>";

	
	if(listening)
	{
		printf("Listening to ip: 127.0.0.1 ip\n");
		while(1){

			conection=socket->waitForNextConnection();
		
			if(conection!=nullptr){
				printf("Request: ");
				conection->read (buffer,MAX_URL_SIZE-1, false);

				printf("%s \n",buffer);

				conection->write(response,strlen(response));
				
				conection->close();

			}

		 }
	}
	else
		printf("Conection Error \n");
    return 0;
}
```

---

<div class="post-metadata">

**Author:** ![matkatmusic](https://avatars.discourse-cdn.com/v4/letter/m/dfb087/32.png) [@matkatmusic](https://forum.juce.com/u/matkatmusic)\
**Post date:** [December 10, 2016, 2:30pm UTC](https://forum.juce.com/t/building-a-simple-http-server-using-juce-sockets/20217/3 "2016-12-10T14:30:30Z")

</div>

Why are you using printf() over std::cout or DBG() ?

---

<div class="post-metadata">

**Author:** ![BK8](https://avatars.discourse-cdn.com/v4/letter/b/6de8d8/32.png) [@BK8](https://forum.juce.com/u/BK8)\
**Post date:** [December 11, 2016, 2:59am UTC](https://forum.juce.com/t/building-a-simple-http-server-using-juce-sockets/20217/4 "2016-12-11T02:59:10Z")

</div>

The main reason is because it does the job, but I’m also using char arrays thats why I think I prefer printf to std::cout.

---

<div class="post-metadata">

**Author:** ![jules](https://avatars.discourse-cdn.com/v4/letter/j/41988e/32.png) [@jules](https://forum.juce.com/u/jules)\
**Post date:** [December 11, 2016, 8:35am UTC](https://forum.juce.com/t/building-a-simple-http-server-using-juce-sockets/20217/5 "2016-12-11T08:35:13Z")

</div>

Both very much discouraged nowadays… As is declaring all your variables at the start of the function, and not using smart pointers (did you mean to just leak all those objects?)… You’ll definitely find things a lot easier in the long run if you get into some modern C++ habits!
