diff --git a/main.cpp b/main.cpp index e73147a..986e16f 100644 --- a/main.cpp +++ b/main.cpp @@ -61,22 +61,45 @@ void check_server_client(int argc, char** argv, Server* server, Client* client) } connect_code = std::string(argv[2]); /* The connect code is a special string, that contains the server address and port. It is given by the server. */ addr_port = connect_code::decode(connect_code); - client = new Client(4, 'T', addr_port[0].data(), std::stoi(addr_port[1])); + try { + client = new Client(4, 'T', addr_port[0].data(), std::stoi(addr_port[1])); + } catch (int e) { + throw; + } catch (std::exception& e) { + throw; + } } /* GAME STARTED IN SERVER MODE */ else if (strcmp(argv[1],"-S") == 0) { - std::string addr = "127.0.0.1"; + std::string addr; uint16_t port; + + /* No IP address or port specified */ if (argc < 3) { + throw EXCEPT_TOOFEWARGS; + } + + /* IP address but no port */ + else if (argc < 4) { std::cout << "No port specified, using 6500..." << std::endl; + addr = std::string(argv[2]); port = 6500; } else { - port = std::stoi(std::string(argv[2])); + addr = std::string(argv[2]); + port = std::stoi(std::string(argv[3])); } std::string code = connect_code::encode(addr, std::to_string(port)); std::cout << "Your code is " << code << std::endl; + try { + server = new Server(4, 'T', addr.data(), port); + } catch (int e) { + throw; + } + catch (std::exception& e) { + throw; + } } else { @@ -102,6 +125,17 @@ int main(int argc, char** argv) { std::cout << "Invalid argument. Optional arguments are -S for server mode, or -C for client mode." << std::endl; return -2; } + if (e == EXCEPT_CONNREFUSED) { + std::cout << "Connection refused. Wrong IP address or port specified." << std::endl; + return -3; + } + if (e == EXCEPT_ADDRNOTAVAIL) { + std:: cout << "Unable to use requested address for server." << std::endl; + return -4; + } + } catch(std::invalid_argument& inv) { + std::cout << inv.what() << std::endl; + return -5; } /* Initialize window and other variables */