Fred Nicolson ff25d11089 Removed socket mutexes. Added more tests. Improved examples.
Socket mutexes are no longer really required, and so have been removed.

Added more tests for network encoding functions, and the URL parser.

The URL parser now returns a path preceeded with a '/' instead of cutting it out. Added get_uri() to URL, for getting the whole URI, so users don't have to concat it themselves from the more specialised functions.

Fixed default socket connect timeout checking for the wrong value.

Fixed request_type_strings not containing all of the possible request types.

Fixed README using old socket close syntax.

Cleaned up the examples a bit.
2018-02-01 11:56:34 +00:00

52 lines
1.3 KiB
C++

//
// Created by fred.nicolson on 01/02/18.
//
#include <frnetlib/HttpRequest.h>
#include <frnetlib/HttpResponse.h>
#include <frnetlib/TcpListener.h>
#include <iostream>
int main()
{
fr::Socket::Status err;
fr::TcpSocket client;
fr::TcpListener listener;
//Bind to a port
if((err = listener.listen("8081")) != fr::Socket::Success)
{
std::cerr << "Failed to bind to port: " << err << std::endl;
return EXIT_FAILURE;
}
while(true)
{
//Accept a new connection
if((err = listener.accept(client)) != fr::Socket::Success)
{
std::cerr << "Failed to accept new connection: " << err << std::endl;
continue;
}
//Receive client HTTP request
fr::HttpRequest request;
if((err = client.receive(request)) != fr::Socket::Success)
{
std::cerr << "Failed to receive request from client: " << err << std::endl;
}
//Construct a response
fr::HttpResponse response;
response.set_body("<h1>Hello, World!</h1>");
//Send it
if((err = client.send(response)) != fr::Socket::Success)
{
std::cerr << "Failed to send response to client: " << err << std::endl;
}
//Close connection
client.close_socket();
}
}