ClientServerProject/main.cpp

208 lines
5.1 KiB
C++
Raw Normal View History

2021-02-09 23:02:05 -05:00
#if WIN32
#define NOMINMAX
#endif
#include "sockets.h"
#include "user_input.h"
#include <chrono>
#include "server.h"
#include "osyncstream.h"
#include "switch_expression.h"
/*
*
auto server(const addrinfo_up& address) noexcept -> void
{
const auto socket = net::socket(AF_INET, SOCK_STREAM, 0);
net::bind(socket, address);
net::listen(socket);
std::cout << "[ Server Started ]" << std::endl;
//below needs update.
do
{
auto const client = accept(socket, nullptr, nullptr);
if (!client)
continue;
printf("Got a client.\n");
auto operation = net::recv(client);
try
{
switch (std::stoi(operation))
{
case 1:
net::send(client, net::system("date /t"));
break;
case 4:
net::send(client, net::system("netstat -ant"));
break;
default:
net::send(client, "INVALID INPUT!");
break;
}
}
catch (std::exception&)
{
net::send(client, "INVALID INPUT!");
}
net::send(client, "<=> T123 <=>");
net::closesocket(client);
printf("Closed Client\n");
} while (true);
}
auto client(const addrinfo_up& address) noexcept -> void
{
while(true)
{
const auto operation = get_input<unsigned int>("Select Command:\n\t1. Date", "");
const auto total_clients = get_input<unsigned int>("Input Number of Clients: ", "");
std::vector<std::thread> threads;
for(auto i = 0u; i < total_clients; ++i)
{
const auto prefix = "[client " + std::to_string(i) + "]: ";
try
{
threads.emplace_back(std::thread([=, &address]()
{
sscout << prefix << "started" << std::endl;
const auto socket = net::socket(AF_INET, SOCK_STREAM, 0);
net::connect(socket, address);
net::send(socket, std::to_string(operation));
sscout << prefix << "sent command" << std::endl;
const auto& response = net::recv(socket);
sscout << prefix << "got response\n" << prefix << response << std::endl;
net::closesocket(socket);
sscout << prefix << "closed" << std::endl;
}));
}
catch (std::exception&) { sscout << prefix << " failed to init " << std::endl; }
}
for (auto& thread : threads)
thread.join();
}
}
*
*/
bool onClientConnected(networked& client)
{
sscout << "Accepted Client on Server.\n";
using namespace std::chrono_literals;
auto start = std::chrono::high_resolution_clock::now();
//std::this_thread::sleep_for(2000ms);
return true;
}
auto onClientMessaged(networked& client, std::string& message)
{
sscout << "message was " << message << "\n";
client.send_message("thanks for the message!");
client.disconnect();
return true;
}
inline auto resolve_selection(const int selection) -> std::string
{
if (selection == 1)
return "Server";
else if (selection == 2)
return "Client";
return "";
}
int main()
{
net::prologue();
const auto mode = get_input<int>("Select Mode\n\t1. Server\n\t2. Client\n>:","Invalid Choice!",math::is_between<0,3>);
while (true)
{
std::string ipaddress{};
std::uint16_t port{};
if (mode == 1)
ipaddress = "0.0.0.0";
else
ipaddress = get_input<std::string>("Enter IP: ");
port = get_input<std::uint16_t>("Enter Port:");
sscout << resolve_selection(mode) << " selected, ip/port is " << ipaddress << "/" << port << ".\n";
if (mode == 2)
{
client net_client{};
net_client.message_received += [](std::string& what){
sscout << "GOT " << what << ".\n";
return true;
};
if(net_client.connect(ipaddress, port))
{
net_client.send_message("!Hello!\n");
net_client.send_message("Second Message\n");
net_client.send_message("Final Message!\n");
while(net_client.is_valid()){}
}
else
{
sscout << "NO NET\n";
}
net_client.disconnect();
}
}
auto listen_server = []()
{
server srv{};
srv.client_connected += onClientConnected;
srv.message_received += onClientMessaged;
auto thread = srv.listen("0.0.0.0", 80);
if(thread.joinable())
{
thread.join();
}else{
sscout << "Thread is bad!\n";
}
};
net::epilogue();
return 0;
/*
*
const auto selection = get_input<int>("Select Mode\n\t1. Server\n\t2. Client\n>:", "Invalid Choice!", [](const auto x) {return x > 0 && x < 3; });
auto address = std::string("0.0.0.0");
auto port = 0_p;
addrinfo_up address_binding{};
while(!address_binding.get())
{
if (selection == 2)
address = get_input<std::string>("Enter Remote IP (aaa.bbb.ccc.ddd): ", "");
port = get_input<unsigned short>("Enter Port: ", "");
address_binding = net::getaddrinfo(address, std::to_string(port));
}
try
{
std::cout << "You have selected " << (selection == 1 ? "Server" : "Client") << std::endl;
std::cout << "Address: " << address << ":" << port << std::endl;
}catch(std::exception&){}
if (selection == 2)
client(address_binding);
else
listen_server(address_binding);
*
*/
net::epilogue();
}