diff --git a/CMakeLists.txt b/CMakeLists.txt index 2d492f1..a6f4a56 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -6,6 +6,7 @@ set( CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} ${CMAKE_CURRENT_SOURCE_DIR}/cmake_mo #User options option(USE_SSL "Use SSL" ON) +option(BUILD_EXAMPLES "Build frnetlib examples" ON) set(FRNETLIB_BUILD_SHARED_LIBS false CACHE BOOL "Build shared library.") if(USE_SSL) @@ -65,6 +66,11 @@ if(USE_SSL) endif() endif() +#Build examples if needbe +if(BUILD_EXAMPLES) + add_subdirectory(examples) +endif() + install( TARGETS frnetlib RUNTIME DESTINATION bin COMPONENT bin diff --git a/examples/simple_server_and_client/CMakeLists.txt b/examples/simple_server_and_client/CMakeLists.txt new file mode 100644 index 0000000..db5b49d --- /dev/null +++ b/examples/simple_server_and_client/CMakeLists.txt @@ -0,0 +1,8 @@ +add_executable(simple_client tcpsocket_client.cpp) +target_link_libraries(simple_client frnetlib) + +add_executable(simple_server tcpsocket_server.cpp) +target_link_libraries(simple_server frnetlib) + +install(TARGETS simple_client simple_server + DESTINATION "bin") diff --git a/examples/simple_server_and_client/tcpsocket_client.cpp b/examples/simple_server_and_client/tcpsocket_client.cpp new file mode 100644 index 0000000..01dc2fd --- /dev/null +++ b/examples/simple_server_and_client/tcpsocket_client.cpp @@ -0,0 +1,86 @@ +#include +#include + +#define SERVER_IP "127.0.0.1" +#define SERVER_PORT "8081" + + +int send_a_packet(fr::TcpSocket &socket) +{ + std::cout << "Going to send something..." << std::endl; + + //Send the request + fr::Packet packet; + packet << "Hello there, I am " << (float)1.2 << " years old"; + if(socket.send(packet) != fr::Socket::Success) + { + //Failed to send packet + std::cout << "Seems got something wrong when sending" << std::endl; + return -1; + } + + //Receive a response + std::cout << "Waiting for a response..." << std::endl; + if(socket.receive(packet) != fr::Socket::Success) + { + std::cout << "Failed to receive server response!" << std::endl; + return -2; + } + + //Extract the response, you can surround this in a try/catch block to catch errors + std::string str1, str2; + float age; + packet >> str1 >> age >> str2; + std::cout << "Server sent: " << str1 << ", " << age << ", " << str2 << "\n\n\n" << std::endl; + return 0; +} + + +int main() +{ + + //Try and connect to the server, create a new TCP object for the job and then connect + fr::TcpSocket socket; //Use an fr::SSLSocket if SSL + if(socket.connect(SERVER_IP, SERVER_PORT) != fr::Socket::Success) + { + //Failed to connect + std::cout << "Failed to connect to: " << SERVER_IP << ":" << SERVER_PORT << std::endl; + return -1; + } + + //For storing user input and send_a_packet response + std::string op_str; + int rtn = 0; + + //Keep going until either we, or the server closes the connection + while(true) + { + //Ask the user what to do + std::cout << "Choose what you want to do, `c` for `continue`, `q` for `quit`:" << std::endl; + std::cin >> op_str; + if(op_str.length() > 1) + { + std::cout << "Seems that you inputted more than one character, please retry." << std::endl; + continue; + } + + switch(op_str[0]) + { + case 'c': + std::cout << "continue" << std::endl; + rtn = send_a_packet(socket); + break; + case 'q': + break; + } + + //Exit/error check + if(op_str[0] == 'q') + break; + if(rtn != 0) + break; + } + + std::cout << "All done, bye!" << std::endl; +} + diff --git a/examples/simple_server_and_client/tcpsocket_server.cpp b/examples/simple_server_and_client/tcpsocket_server.cpp new file mode 100644 index 0000000..bf1686f --- /dev/null +++ b/examples/simple_server_and_client/tcpsocket_server.cpp @@ -0,0 +1,76 @@ +#include +#include +#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; +} +