changed in main

This commit is contained in:
2026-06-17 21:25:12 +03:00
parent 9a150d90ad
commit e3bcc8bc62
+25 -17
View File
@@ -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 "NetworkConfig.h"
#include "EchoServer.h" #include "server/ServerSocketThread.h"
#include "SimpleClient.h" #include "SimpleClient.h"
#include <iostream> #include <iostream>
#include <string> #include <string>
#include <thread>
#include <chrono>
int main(const int argc, char* argv[]) { int main(int argc, char* argv[]) {
// 1. Turn on the networking rulebook
NetworkStarter network; NetworkStarter network;
// 2. Check what the user typed when they ran the program
if (argc > 1 && std::string(argv[1]) == "server") { if (argc > 1 && std::string(argv[1]) == "server") {
EchoServer server(8080); ServerSocketThread server(8080);
server.start(); // Boss says: "Server, start working!"
// 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") { else if (argc > 1 && std::string(argv[1]) == "client") {
if (SimpleClient client; client.connectTo("127.0.0.1", 8080)) { SimpleClient client;
client.talk("Hello, Server! Are you there?\n"); if (client.connectTo("127.0.0.1", 8080)) {
client.talk("This is a second message!\n"); client.talk("Hello, Threaded Server!\n");
client.talk("Are you still listening?\n");
} }
} }
else { else {
std::cout << "How to use:\n"; std::cout << "How to use:\n";
std::cout << " Run as Server: echo_app server\n"; std::cout << " Run Server (runs for 10s then stops): echo_app server\n";
std::cout << " Run as Client: echo_app client\n"; std::cout << " Run Client (connects to server): echo_app client\n";
} }
return 0; return 0;
} }