diff --git a/client.c b/client.c index bc21656..d38292b 100644 --- a/client.c +++ b/client.c @@ -12,7 +12,7 @@ #include #include -#define SERVER_ADDR "39.107.83.159" +#define SERVER_ADDR "127.0.0.1" #define SERVER_PORT 6666 #define SCREEN_BEGIN_LINE 1 @@ -43,6 +43,16 @@ void init_client(int sockfd); void write_display_win(char buffer[]); int main(int argc, char *argv[]){ + // chech addr and port + char *server_addr = SERVER_ADDR; + int server_port = SERVER_PORT; + if(argc == 3){ + server_addr = argv[1]; + server_port = atoi(argv[2]); + } + + printf("try to connect %s:%d......\n", server_addr, server_port); + printf("please input your name(length between 1~12): "); scanf("%s", username); name_length = strlen(username); @@ -68,8 +78,8 @@ int main(int argc, char *argv[]){ struct sockaddr_in addr; socklen_t addrlen = sizeof(addr); addr.sin_family = AF_INET; - addr.sin_addr.s_addr = inet_addr(SERVER_ADDR); - addr.sin_port = htons(SERVER_PORT); + addr.sin_addr.s_addr = inet_addr(server_addr); + addr.sin_port = htons(server_port); int res = connect(sockfd, (struct sockaddr*)&addr, addrlen); assert(res != -1); diff --git a/server.c b/server.c index 54a2471..e113470 100644 --- a/server.c +++ b/server.c @@ -18,7 +18,7 @@ */ -#define SERVER_PORT 6666 +#define SERVER_PORT 6666 #define MAX_CLIENT_NUM 32 #define SERVER_INS "SERVER-INS" #define SERVER_INS_FMT "%s&%d&%d&%s&%s" @@ -56,6 +56,10 @@ void forward_message(char *username, char *message); int main(int argc, char *argv[]) { + int run_port = SERVER_PORT; + if(argc == 2){ + run_port = atoi(argv[1]); + } // set sigint handler signal(SIGINT, sigint_handler); // ignore sigpipe @@ -76,13 +80,13 @@ int main(int argc, char *argv[]) memset(&serv_addr, 0, sizeof(serv_addr)); serv_addr.sin_family = AF_INET; serv_addr.sin_addr.s_addr = inet_addr("0.0.0.0"); - serv_addr.sin_port = htons(SERVER_PORT); + serv_addr.sin_port = htons(run_port); assert(bind(sockfd, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) != -1); // listen assert(listen(sockfd, 20) != -1); - printf("run tcp server in %s:%d ......\n", "127.0.0.1", SERVER_PORT); + printf("run tcp server in %s:%d ......\n", "127.0.0.1", run_port); struct sockaddr_in clnt_addr; socklen_t clnt_addr_size = sizeof(clnt_addr); while (1){ @@ -251,4 +255,4 @@ void send_server_ins(char *fmt, ...){ buffer[length+1] = '\n'; printf("[%s]: %s\n", "SEND_ALL", buffer); send_all(buffer, length); -} \ No newline at end of file +}