fr::HttpSocket has been removed. fr::Sendable has been added. Classes can inherit this if they want to be sendable through sockets. Both fr::Http (which is inherited by fr::HttpRequest and fr::HttpResponse), and fr::Packet inherit this, to allow them to be sent through fr::Socket::send()/fr::Socket::receive(). Fixed broken fr::SocketSelector and fr::SocketReactor, as they no longer accepted fr::Listener's due to fr::Listener no longer inheriting fr::Socket.
79 lines
2.2 KiB
C++
79 lines
2.2 KiB
C++
#include <iostream>
|
|
#include <frnetlib/Packet.h>
|
|
#include <frnetlib/TcpSocket.h>
|
|
#include <frnetlib/TcpListener.h>
|
|
#define PORT "8081"
|
|
|
|
|
|
int main()
|
|
{
|
|
//Create a new TCP socket to maintain connections, and a new Tcp Listener to accept connections
|
|
fr::TcpSocket client; //fr::SSLSocket for SSL
|
|
fr::TcpListener listener; //fr::SSLListener for HTTPS
|
|
|
|
//Bind to a port
|
|
if(listener.listen(PORT) != fr::Socket::Success)
|
|
{
|
|
std::cout << "Failed to bind to port: " << PORT << std::endl;
|
|
return -1;
|
|
}
|
|
|
|
std::cout << "Listener is listening on port: " << PORT << "..." << std::endl;
|
|
|
|
//Start accepting connections
|
|
while(true)
|
|
{
|
|
std::cout << "Waiting for a new connection ..." << std::endl;
|
|
|
|
//Accept a new connection
|
|
if(listener.accept(client) != fr::Socket::Success)
|
|
{
|
|
std::cout << "Failed to accept client, shutdown" << std::endl;
|
|
break;
|
|
}
|
|
|
|
while(true) //Infinite loop to keep the connection active
|
|
{
|
|
try
|
|
{
|
|
//Receive an fr::Packet from the client
|
|
fr::Packet packet;
|
|
if(client.receive(packet) != fr::Socket::Success)
|
|
{
|
|
std::cout << "Failed to receive request" << std::endl;
|
|
client.close_socket();
|
|
break;
|
|
}
|
|
|
|
//Extract the data from the packet
|
|
std::string str1, str2;
|
|
float age;
|
|
packet >> str1 >> age >> str2;
|
|
|
|
//Print out what we got
|
|
std::cout << "Client sent:" << str1 << ", " << age << ", " << str2 << std::endl;
|
|
|
|
//Send back the same packet
|
|
if(client.send(packet) != fr::Socket::Success)
|
|
{
|
|
throw std::string("Failed to send packet to client");
|
|
}
|
|
}
|
|
catch(const std::exception &e)
|
|
{
|
|
//Print out what happened
|
|
std::cout << "Error: " << e.what() << std::endl;
|
|
|
|
//Close connection
|
|
client.close_socket();
|
|
break;
|
|
}
|
|
}
|
|
|
|
|
|
}
|
|
|
|
return 0;
|
|
}
|
|
|