diff --git a/main.cpp b/main.cpp index a1e5550..ba98870 100644 --- a/main.cpp +++ b/main.cpp @@ -1,34 +1,42 @@ -/** - * main.cpp is the manager. It hires a NetworkStarter, checks if it was told to be a "server" or a "client", - * creates that object, and tells it to do its job. It doesn't need to know the messy details of sockets - */ - #include "NetworkConfig.h" -#include "EchoServer.h" +#include "server/ServerSocketThread.h" #include "SimpleClient.h" #include #include +#include +#include -int main(const int argc, char* argv[]) { - // 1. Turn on the networking rulebook +int main(int argc, char* argv[]) { NetworkStarter network; - // 2. Check what the user typed when they ran the program if (argc > 1 && std::string(argv[1]) == "server") { - EchoServer server(8080); - server.start(); // Boss says: "Server, start working!" + ServerSocketThread server(8080); + + // 1. Start the server in the background + server.start(); + + // 2. Simulate the server running for 10 seconds. + // In a real app, this might be waiting for a user to press 'q' to quit. + std::cout << "⏳ Server will run for 10 seconds, then auto-stop...\n"; + std::this_thread::sleep_for(std::chrono::seconds(10)); + + // 3. Send the interrupt command! + server.stop(); + + std::cout << "🏁 Main program is now exiting.\n"; } else if (argc > 1 && std::string(argv[1]) == "client") { - if (SimpleClient client; client.connectTo("127.0.0.1", 8080)) { - client.talk("Hello, Server! Are you there?\n"); - client.talk("This is a second message!\n"); + SimpleClient client; + if (client.connectTo("127.0.0.1", 8080)) { + client.talk("Hello, Threaded Server!\n"); + client.talk("Are you still listening?\n"); } } else { std::cout << "How to use:\n"; - std::cout << " Run as Server: echo_app server\n"; - std::cout << " Run as Client: echo_app client\n"; + std::cout << " Run Server (runs for 10s then stops): echo_app server\n"; + std::cout << " Run Client (connects to server): echo_app client\n"; } return 0; -} \ No newline at end of file +}