Skip to content

Commit

Permalink
cannot connect as existing user
Browse files Browse the repository at this point in the history
  • Loading branch information
gesslerpd committed Aug 17, 2016
1 parent b7e95e9 commit 1c21401
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 2 deletions.
2 changes: 2 additions & 0 deletions src/chat.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@

#define LIST "list\n"

#define ERROR "error\n"

#define USERNAMExMESSAGE ": "

// General
Expand Down
12 changes: 10 additions & 2 deletions src/client.c
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,15 @@ void *receiver() {
}

receiveBuffer[nbytes] = '\0';
printf("%s", receiveBuffer);
pthread_mutex_unlock(&mutexsum);
if(strcmp(ERROR, receiveBuffer) == 0) {
printf("Error: The username %s is already taken.\n", sendBuffer);
done = TRUE;
pthread_mutex_destroy(&mutexsum);
pthread_exit(NULL);
}
else {
printf("%s", receiveBuffer);
pthread_mutex_unlock(&mutexsum);
}
}
}
9 changes: 9 additions & 0 deletions src/server.c
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,15 @@ int connectClient(struct sockaddr_in newClient, char *username) {
while(element != NULL) {
if(strcmp(element->username, username) == 0) {
printf("Cannot connect client user already exists\n");
strcpy(responseBuffer, "");
strcat(responseBuffer, ERROR);
if((sendto
(sockfd, responseBuffer, strlen(responseBuffer), 0, (struct sockaddr *) &newClient,
sizeof(struct sockaddr))) == SYSERR) {
perror("sendto");
close(sockfd);
exit(EXIT_FAILURE);
}
return SYSERR;
}
element = element->next;
Expand Down

0 comments on commit 1c21401

Please sign in to comment.