diff --git a/smallchat-server.c b/smallchat-server.c index 5f4679e..91d68b3 100644 --- a/smallchat-server.c +++ b/smallchat-server.c @@ -43,7 +43,7 @@ * =========================================================================== */ #define MAX_CLIENTS 1000 // This is actually the higher file descriptor. -#define SERVER_PORT 7711 +#define DEFAULT_SERVER_PORT 7711 /* This structure represents a connected client. There is very little * info about it: the socket descriptor and the nick name, if set, otherwise @@ -113,7 +113,7 @@ void freeClient(struct client *c) { } /* Allocate and init the global stuff. */ -void initChat(void) { +void initChat(int server_port) { Chat = chatMalloc(sizeof(*Chat)); memset(Chat,0,sizeof(*Chat)); /* No clients at startup, of course. */ @@ -122,11 +122,12 @@ void initChat(void) { /* Create our listening socket, bound to the given port. This * is where our clients will connect. */ - Chat->serversock = createTCPServer(SERVER_PORT); + Chat->serversock = createTCPServer(server_port); if (Chat->serversock == -1) { perror("Creating listening socket"); exit(1); } + printf("smallchat server is run on port %d \n", server_port); } /* Send the specified string to all connected clients but the one @@ -148,8 +149,9 @@ void sendMsgToAllClientsBut(int excluded, char *s, size_t len) { * 1. Accept new clients connections if any. * 2. Check if any client sent us some new message. * 3. Send the message to all the other clients. */ -int main(void) { - initChat(); +int main(int argc, char **argv) { + int server_port = argc == 2 ? atoi(argv[1]) : DEFAULT_SERVER_PORT; + initChat(server_port); while(1) { fd_set readfds;