add options to set address and port, improved usage page
This commit is contained in:
65
main.cc
65
main.cc
@@ -23,7 +23,7 @@
|
|||||||
|
|
||||||
using namespace std;
|
using namespace std;
|
||||||
|
|
||||||
const int PORT_NUM = 8888;
|
int PORT_NUM = 8888;
|
||||||
string IP_ADDRESS = "0.0.0.0";
|
string IP_ADDRESS = "0.0.0.0";
|
||||||
const int DEFAULT_CUR_X = 2; // the x position of the preferred default cursor
|
const int DEFAULT_CUR_X = 2; // the x position of the preferred default cursor
|
||||||
// position for message entry
|
// position for message entry
|
||||||
@@ -60,33 +60,52 @@ struct waitClientArgs {
|
|||||||
// struct to keep track of
|
// struct to keep track of
|
||||||
};
|
};
|
||||||
|
|
||||||
void usage(char *progname);
|
void usage(char *progname, bool *m);
|
||||||
|
|
||||||
int
|
int
|
||||||
main(int argc, char *argv[])
|
main(int argc, char *argv[])
|
||||||
{
|
{
|
||||||
/*
|
/*
|
||||||
* userInput: character buffer for the user's next message
|
* ch: getopt
|
||||||
* exit: set to true when infinite loop is to end
|
* exit: set to true when infinite loop is to end
|
||||||
|
* userInput: character buffer for the user's next message
|
||||||
* modeless: check whether a modeless exit was accidental
|
* modeless: check whether a modeless exit was accidental
|
||||||
*/
|
*/
|
||||||
char *userInput = new char[1024];
|
int ch;
|
||||||
bool exit = false;
|
bool exit = false;
|
||||||
|
char *userInput = new char[1024];
|
||||||
bool modeless = false;
|
bool modeless = false;
|
||||||
|
|
||||||
if (argc > 1) {
|
if (argc == 1) {
|
||||||
if (!strncmp(argv[1], "client", 6)) {
|
usage(argv[0], &modeless);
|
||||||
logFileName = "client.txt";
|
|
||||||
mode = CLIENT_MODE;
|
|
||||||
} else if (!strncmp(argv[1], "server", 6)) {
|
|
||||||
logFileName = "server.txt";
|
|
||||||
mode = SERVER_MODE;
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
usage(argv[0]);
|
|
||||||
modeless = true;
|
|
||||||
goto leave;
|
goto leave;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
while ((ch = getopt(argc, argv, "m:a:p:h")) != -1)
|
||||||
|
switch (ch) {
|
||||||
|
case 'm':
|
||||||
|
if (!strncmp(optarg, "client", 6)) {
|
||||||
|
logFileName = "client.txt";
|
||||||
|
mode = CLIENT_MODE;
|
||||||
|
} else if (!strncmp(optarg, "server", 6)) {
|
||||||
|
logFileName = "server.txt";
|
||||||
|
mode = SERVER_MODE;
|
||||||
|
} else {
|
||||||
|
usage(argv[0], &modeless);
|
||||||
|
goto leave;
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
case 'a':
|
||||||
|
IP_ADDRESS = optarg;
|
||||||
|
break;
|
||||||
|
case 'p':
|
||||||
|
PORT_NUM = atoi(optarg);
|
||||||
|
break;
|
||||||
|
case 'h': default:
|
||||||
|
usage(argv[0], &modeless);
|
||||||
|
goto leave;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
// putting this lower down to circumvent the terminal brick when the
|
// putting this lower down to circumvent the terminal brick when the
|
||||||
// socket was already in use results in all network functonality no
|
// socket was already in use results in all network functonality no
|
||||||
@@ -180,17 +199,29 @@ leave:
|
|||||||
"setting mode.");
|
"setting mode.");
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
return 2;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* remember to call 'goto leave' after running */
|
||||||
void
|
void
|
||||||
usage(char *progname)
|
usage(char *progname, bool *m) // m:a:p:h
|
||||||
{
|
{
|
||||||
printf("\x1b[1mUsage:\x1b[0m\n"
|
printf("\x1b[1mUsage:\x1b[0m\n"
|
||||||
"\t%s client\t\x1b[3m# run as client\x1b[0m\n"
|
"\t%s client\t\x1b[3m# run as client\x1b[0m\n"
|
||||||
"\t%s server\t\x1b[3m# run as server\x1b[0m\n",
|
"\t%s server\t\x1b[3m# run as server\x1b[0m\n"
|
||||||
|
"\n\n"
|
||||||
|
"\x1b[1mOptions:\x1b[0m\n"
|
||||||
|
"\t\x1b[1m-m [client|server]\x1b[0m\tMode \x1b[1;31m(required)\x1b[0m\n"
|
||||||
|
"\n"
|
||||||
|
"\t\x1b[1m-a [255.255.255.255]\x1b[0m\tAddress\n"
|
||||||
|
"\t\t\t\tserver mode: listen address \x1b[33m(default: 0.0.0.0)\x1b[0m\n"
|
||||||
|
"\t\t\t\tclient mode: address of server \x1b[33m(default: 127.0.0.1)\x1b[0m\n"
|
||||||
|
"\n"
|
||||||
|
"\t\x1b[1m-p [0-65535]\x1b[0m\t\tPort number\n",
|
||||||
progname, progname);
|
progname, progname);
|
||||||
|
*m = true;
|
||||||
}
|
}
|
||||||
|
|||||||
2
public.h
2
public.h
@@ -8,7 +8,7 @@ using namespace std;
|
|||||||
typedef enum { NO_MODE = 0, SERVER_MODE = 1, CLIENT_MODE = 2 } chatmode_t;
|
typedef enum { NO_MODE = 0, SERVER_MODE = 1, CLIENT_MODE = 2 } chatmode_t;
|
||||||
|
|
||||||
extern chatmode_t mode;
|
extern chatmode_t mode;
|
||||||
extern const int PORT_NUM;
|
extern int PORT_NUM;
|
||||||
extern string IP_ADDRESS;
|
extern string IP_ADDRESS;
|
||||||
extern const int DEFAULT_CUR_X;
|
extern const int DEFAULT_CUR_X;
|
||||||
extern const int DEFAULT_CUR_Y;
|
extern const int DEFAULT_CUR_Y;
|
||||||
|
|||||||
Reference in New Issue
Block a user