Skip to content

Commit

Permalink
add addr and port in command.
Browse files Browse the repository at this point in the history
  • Loading branch information
lambdafate committed Jun 4, 2020
1 parent 157e278 commit 1f555c0
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 7 deletions.
16 changes: 13 additions & 3 deletions client.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
#include <locale.h>
#include <assert.h>

#define SERVER_ADDR "39.107.83.159"
#define SERVER_ADDR "127.0.0.1"
#define SERVER_PORT 6666

#define SCREEN_BEGIN_LINE 1
Expand Down Expand Up @@ -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);
Expand All @@ -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);
Expand Down
12 changes: 8 additions & 4 deletions server.c
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
Expand All @@ -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){
Expand Down Expand Up @@ -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);
}
}

0 comments on commit 1f555c0

Please sign in to comment.