//////////////////////////////
/// DRIVER
//////////////////////////////
void driver(int argc, char * argv[]) {
  
  // Argument #2 is the first command line argument, so
  // we want to make sure we have some here.
  if (argc > 1) {
  
    string type(argv[1]);
    
    // Make the string lowercase so it's not case-sensitive.
    type = str_tolower(type);
    
    unsigned int port = DEFAULT_PORT;
    
    // If they supplied a port to use.
    if (argc > 2) {
    
      string port_str(argv[2]);
      port_str = str_tolower(port_str);
      
      if (port_str != "-d") {
      
        port = str_to_int(argv[2]);
      
      }
    
    }
    
    // Create an object of the requested type and start running.
    if (type == "s" || type == "server") {
    
      ChatServer server;
      server.setup(port);
      server.run();
    
    } else if (type == "c" || type == "client") {
    
      string hostname = DEFAULT_HOST;
      
      if (argc > 3) {
      
        hostname = argv[3];
      
      }
      
      ChatClient client;
      client.setup(port, hostname);
      client.run();
    
    } else {
    
      cout << "Invalid arguments provided." << endl;
      cout << "Please supply arguments to the program in this format:" << endl;
      cout << argv[0] << " type [port [hostname]]" << endl;
      cout << "type:     Either 'S' for server or 'C' for client." << endl;
      cout << "port:     The port number to use ('-d' for default)." << endl;
      cout << "          Note: this program defaults to port " << DEFAULT_PORT
           << "." << endl;
      cout << "hostname: The hostname or IP address of the server to" << endl;
      cout << "          connect to (only used as client)." << endl;
      cout << "These are case-insensitive." << endl;
    
    }
  
  } else {
  
    cout << "No arguments provided." << endl;
    cout << "Please supply arguments to the program in this format:" << endl;
    cout << argv[0] << " type [port [hostname]]" << endl;
    cout << "type:     Either 'S' for server or 'C' for client." << endl;
    cout << "port:     The port number to use ('-d' for default)." << endl;
    cout << "          Note: this program defaults to port " << DEFAULT_PORT
         << endl;
    cout << "hostname: The hostname or IP address of the server to" << endl;
    cout << "          connect to (only used as client)." << endl;
    cout << "These are case-insensitive." << endl;
  
  }
  
}
Example #2
0
/**
 *  This is the main function which creates a chat client object
 *  and runs the chat.
 */
int main(int argc, char* argv[]) {
	ChatClient cc = ChatClient();
	cc.run(argc, argv);
}