Handle args for server: use port in cli, PORT #ifdef, #else 0
This commit is contained in:
parent
a5712c52a4
commit
d63d5538eb
2 changed files with 35 additions and 8 deletions
40
src/server.c
40
src/server.c
|
@ -16,9 +16,8 @@
|
||||||
* */
|
* */
|
||||||
struct client_details clients[MAX_CLIENTS];
|
struct client_details clients[MAX_CLIENTS];
|
||||||
|
|
||||||
int main(void) {
|
int main(int argc, char *argv[]) {
|
||||||
int serv_sockid, client_sockid;
|
int server_port, serv_sockid, client_sockid, client_id;
|
||||||
int client_id;
|
|
||||||
int message_pipe[2];
|
int message_pipe[2];
|
||||||
struct sockaddr_in server_socket;
|
struct sockaddr_in server_socket;
|
||||||
struct sockaddr_in client_socket;
|
struct sockaddr_in client_socket;
|
||||||
|
@ -28,6 +27,8 @@ int main(void) {
|
||||||
long addr_len;
|
long addr_len;
|
||||||
char eot = EOT;
|
char eot = EOT;
|
||||||
|
|
||||||
|
server_port = handle_args(argc, argv);
|
||||||
|
|
||||||
/* Signal handling */
|
/* Signal handling */
|
||||||
signal(SIGCHLD, interrupt);
|
signal(SIGCHLD, interrupt);
|
||||||
signal(SIGINT, interrupt);
|
signal(SIGINT, interrupt);
|
||||||
|
@ -43,11 +44,7 @@ int main(void) {
|
||||||
|
|
||||||
/* Initialize default values for the socket */
|
/* Initialize default values for the socket */
|
||||||
server_socket.sin_family = AF_INET;
|
server_socket.sin_family = AF_INET;
|
||||||
#ifdef PORT
|
server_socket.sin_port = htons(server_port);
|
||||||
server_socket.sin_port = htons(PORT);
|
|
||||||
#else
|
|
||||||
server_socket.sin_port = 0;
|
|
||||||
#endif
|
|
||||||
#ifdef LISTEN_ALL
|
#ifdef LISTEN_ALL
|
||||||
server_socket.sin_addr.s_addr = makeip4(0, 0, 0, 0);
|
server_socket.sin_addr.s_addr = makeip4(0, 0, 0, 0);
|
||||||
#else
|
#else
|
||||||
|
@ -133,6 +130,33 @@ int main(void) {
|
||||||
return EXIT_SUCCESS;
|
return EXIT_SUCCESS;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* Handle command line arguments */
|
||||||
|
int handle_args(int argc, char *argv[]) {
|
||||||
|
long port;
|
||||||
|
|
||||||
|
/* Command line has no port, use default */
|
||||||
|
if (argc == 1) {
|
||||||
|
#ifdef PORT
|
||||||
|
return PORT;
|
||||||
|
#else
|
||||||
|
return 0;
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Command line has a port, convert it to int */
|
||||||
|
if (argc == 2) {
|
||||||
|
port = strtol(argv[1], NULL, 10);
|
||||||
|
if ((0 < port) && (port < 65536)) {
|
||||||
|
// Conversion OK, return the port
|
||||||
|
return (int) port;
|
||||||
|
}
|
||||||
|
// Otherwise, could not convert: exit down below
|
||||||
|
}
|
||||||
|
|
||||||
|
fprintf(stderr, "usage: %s [<server_port>]\n", argv[0]);
|
||||||
|
exit(EXIT_FAILURE);
|
||||||
|
}
|
||||||
|
|
||||||
/* Compute the next free client_id in the client array */
|
/* Compute the next free client_id in the client array */
|
||||||
int get_next_free_client_id(void) {
|
int get_next_free_client_id(void) {
|
||||||
for (int i = 0; i < MAX_CLIENTS; i++) {
|
for (int i = 0; i < MAX_CLIENTS; i++) {
|
||||||
|
|
|
@ -10,6 +10,9 @@ struct client_details {
|
||||||
int pipe[2];
|
int pipe[2];
|
||||||
};
|
};
|
||||||
|
|
||||||
|
/* Handle command line arguments */
|
||||||
|
int handle_args(int, char**);
|
||||||
|
|
||||||
/* Compute the next free client_id in the client array */
|
/* Compute the next free client_id in the client array */
|
||||||
int get_next_free_client_id(void);
|
int get_next_free_client_id(void);
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue