Building a simple http server using juce sockets

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;
}

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;
}

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

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.

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!