From 19829b796d4bffa2f0a8a9963998378fe16c3131 Mon Sep 17 00:00:00 2001 From: liqiang2020 <361066751@qq.com> Date: Sat, 27 Jan 2024 15:04:03 +0800 Subject: [PATCH] server get PORT from args server get PORT from args --- smallchat-server.c => smartchat-server.c | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) rename smallchat-server.c => smartchat-server.c (98%) diff --git a/smallchat-server.c b/smartchat-server.c similarity index 98% rename from smallchat-server.c rename to smartchat-server.c index 67b1589..b2df829 100644 --- a/smallchat-server.c +++ b/smartchat-server.c @@ -43,7 +43,6 @@ * =========================================================================== */ #define MAX_CLIENTS 1000 // This is actually the higher file descriptor. -#define 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 +112,7 @@ void freeClient(struct client *c) { } /* Allocate and init the global stuff. */ -void initChat(void) { +void initChat(unsigned short port) { Chat = chatMalloc(sizeof(*Chat)); memset(Chat,0,sizeof(*Chat)); /* No clients at startup, of course. */ @@ -122,7 +121,7 @@ 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(port); if (Chat->serversock == -1) { perror("Creating listening socket"); exit(1); @@ -148,8 +147,12 @@ 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[]) { + if (argc != 2) { + printf("Usage: %s \n", argv[0]); + exit(1); + } + initChat(atoi(argv[1])); while(1) { fd_set readfds;