Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Name Duplication Fixed #7

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added src/client_chat
Binary file not shown.
11 changes: 10 additions & 1 deletion src/client_chat.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ volatile sig_atomic_t flag =0;
int sockfd =0;
char name[NAME_LEN];


void str_overwrite_stdout(){
printf("\r%s",">");
fflush(stdout);
Expand Down Expand Up @@ -93,7 +94,7 @@ int main(int argc, char **argv)
printf("Usage: %s <port> \n", argv[0]);
return EXIT_FAILURE;
}
char *ip = "127.0.0.1";
char *ip = (char*)"127.0.0.1";
int port = atoi(argv[1]);
signal(SIGINT, catch_ctrl_c_and_exit);
printf("Enter your Name:");
Expand All @@ -119,6 +120,14 @@ int main(int argc, char **argv)
}

send(sockfd,name,NAME_LEN,0);
char response[1024];
recv(sockfd, response, sizeof(response),0);

if(response[0] == '1')
{
printf("Name Already Exists\n");
return EXIT_FAILURE;
}

printf(" Hi! WELCOME TO THE CHATROOM. \n");
pthread_t send_msg_thread;
Expand Down
Binary file added src/server_chat
Binary file not shown.
38 changes: 34 additions & 4 deletions src/server_chat.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@
#define MAX_CLIENTS 100
#define BUFFER_SZ 2048
#define NAME_LEN 32

/*
Name that comes from client end check if it's already in clients and return a boolean value
*/
static unsigned int cli_count =0;
static int uid =10;

Expand All @@ -28,6 +30,24 @@ client_t *clients[MAX_CLIENTS];

pthread_mutex_t clients_mutex = PTHREAD_MUTEX_INITIALIZER;

int check_duplicate_client_name(char *name)
{
int i=0, j=0;
while(clients[i])
{
if(strcmp(name, clients[i]->name)==0)
{
char temp[NAME_LEN] = "XZ";
strncpy(clients[i]->name,temp, sizeof(clients[i]->name));
return 1;
}

++i;
}

return 0;
}

void str_overwrite_stdout(){
printf("\r%s",">");
fflush(stdout);
Expand Down Expand Up @@ -114,13 +134,22 @@ void *handle_client(void *arg)
cli_count++;

client_t *cli = (client_t*)arg;
if(recv(cli->sockfd, name, NAME_LEN,0)<=0 || strlen(name)<2 || strlen(name) >= NAME_LEN -1)
if(recv(cli->sockfd, name, NAME_LEN,0)<=0 || strlen(name)<2 || strlen(name) >= NAME_LEN -1 )
{
printf("Enter the name correctly\n");
leave_flag =1;
char response[1024] = "1";
send(cli->sockfd, response, sizeof(response), 0);
}
else if(check_duplicate_client_name(name))
{
char response[1024] = "1";
send(cli->sockfd, response, sizeof(response), 0);
}
else
{
char response[1024] = "0";
send(cli->sockfd, response, sizeof(response), 0);
strcpy(cli->name, name);
sprintf(buffer,"%s has joined the chatroom\n",cli->name);
printf("%s",buffer);
Expand All @@ -145,7 +174,8 @@ void *handle_client(void *arg)
}
else if (receive==0 || strcmp(buffer,"exit")==0)
{
sprintf(buffer,"%s has left the chatroom\n", cli->name);
if(strlen(cli->name)>3)
sprintf(buffer,"%s has left the chatroom\n", cli->name);
printf("%s", buffer);
send_message(buffer,cli->uid);
leave_flag =1;
Expand Down Expand Up @@ -173,7 +203,7 @@ int main(int argc, char **argv)
printf("Usage: %s <port> \n", argv[0]);
return EXIT_FAILURE;
}
char *ip = "127.0.0.1";
char *ip = (char*)"127.0.0.1";
int port = atoi(argv[1]);

int option =1;
Expand Down