cserverclientdistributed-systematoi

ATOI in C messing with other strings in SERVER/CLIENT GMS application


We are trying to make a GMS server/client chat. We are encountering an issue when using atoi on this part of the code

unserialize_tcp(buffer,&type, &grpname, &member_name, &received_addr, &received_port);
printf("1%s %s %s\n", grpname, member_name, received_addr);
addr = atoi(received_addr);
client.sin_addr.s_addr = addr;
client.sin_port = received_port;
printf("2%s %s \n", grpname, member_name);

and the output is this as you can see the member name loses it's original value after we use atoi

Succesfully created RECEIVER WORKER and SOCKET WORKER 

Available options
     type 'exit' to close gms  
BEFORE
AFTER
BEFORE
New Client socket 4 stored at index 0
packet 0_omada_onoma_16777343_26385
1omada onoma 16777343
2omada @���x 
3omada @���x 
4omada @���x 
Group omada just got a new member @���x 
We didn't notify every member of the group  about the new member

this is the unserialize_tcp function

void unserialize_tcp(char* message, type_t *type, char** grpname, char** member_name, char** received_addr, int* received_port) {

    char msg[TCP_PACKET] = {'\0'};
    char *saveptr;
    char *ptr = message;
    char *delim = "_";
    int i;
    for (i = 0; ptr[i]; ptr[i] == '_' ? i++ : *ptr++);
    if (i != 4) {
        printf("Message corrupted\n");
        return;
    }
    strcpy(msg, message);
    *type = atoi(strtok_r(msg, delim, &saveptr));
    *grpname = strtok_r(NULL, delim, &saveptr);
    *member_name = strtok_r(NULL, delim, &saveptr);
    *received_addr = strtok_r(NULL, delim, &saveptr);
    *received_port = atoi(strtok_r(NULL, delim, &saveptr));
    

}

and this is the receive_worker thread that the problem with atoi originates


void *gms_receive_worker(void *args) {
  int i;
  char buffer[TCP_PACKET];
  char *grpname = (char*)calloc(16, sizeof(char));
  char* member_name = (char*)calloc(16, sizeof(char));
  type_t type ;
  char* received_addr = (char*)calloc(LENGTH_MSG, sizeof(char));
  char *msg;
  msg = (char*) malloc(sizeof(char)*TCP_PACKET);
  int received_port, addr;
  
  struct sockaddr_in client;
  while(!flag) {
    for (i=0; i< MAX_CLIENTS; i++) {
      memset(buffer, '\0',sizeof(buffer));
      if (recv(client_sockets[i], (char*)buffer, TCP_PACKET, MSG_WAITALL) < 0 ) {
            continue;
      }
      
      if(strlen(buffer) > 0) {
        printf("packet %s\n", buffer);
        unserialize_tcp(buffer,&type, &grpname, &member_name, &received_addr, &received_port);
        printf("1%s %s %s\n", grpname, member_name, received_addr);
        addr = atoi(received_addr);
        client.sin_addr.s_addr = addr;
        client.sin_port = received_port;
         printf("2%s %s \n", grpname, member_name);
        if (type == EXIT_TCP_SOCKET) {
          printf(BOLDRED"Client exited and the socket %d just closed\n"RESET, client_sockets[i]);
          client_sockets[i] = 0;
          continue;
        }
        else if (type == GROUP_JOIN) {
          printf("3%s %s \n", grpname, member_name);
          if (search_gms_list(list, grpname, member_name) != NULL) {
            msg = serialize_tcp(GROUP_REJECT_MEMBER, grpname, member_name, received_addr, received_port);
            if(send(client_sockets[i], (const char *)msg, TCP_PACKET, MSG_NOSIGNAL) < 0 ) {
                printf(BOLDRED"Failed inside group join\n"RESET);
                exit(1);
            }
            continue;
          }
           printf("4%s %s \n", grpname, member_name);
          printf(BOLDRED"Group %s just got a new member %s \n"RESET, grpname, member_name);
          
          add_gms_list(list, grpname, member_name, client, client_sockets[i]);
          msg = serialize_tcp(GROUP_SOMEONE_JOINED, grpname, member_name, received_addr, received_port);
            
          if (gms_sender_worker(grpname,member_name, msg) == 1) {
            printf(CYAN"We notified every member of the group %s about the new member\n"RESET, grpname);
            continue;
          }
          else{
            printf(CYAN"We didn't notify every member of the group %s about the new member\n"RESET, grpname);
            continue;
          }
            
        }
        else if (type == GROUP_LEAVE ) {
          if (search_gms_list(list, grpname, member_name) == NULL) {
            msg = serialize_tcp(LEAVE_FAILED, grpname, member_name, received_addr, received_port);
            if(send(client_sockets[i], (const char *)msg, TCP_PACKET, MSG_NOSIGNAL) < 0 ) {
                printf(BOLDRED"Failed inside group join\n"RESET);
                exit(1);
            }
            continue;
          }
          printf(BOLDRED"Group %s lost the member %s \n"RESET, grpname, member_name);

          delete_gms_list(list, grpname, member_name, client);
          msg = serialize_tcp(GROUP_SOMEONE_LEFT, grpname, member_name, received_addr, received_port);
            
          if (gms_sender_worker(grpname,member_name, msg) == 1) {
            printf(CYAN"We notified every member of the group %s about the member that left\n"RESET, grpname);
            continue;
          }
          else{
            printf(CYAN"We didn't notify every member of the group %s about the member that left\n"RESET, grpname);
            continue;
          }
        }
        else{
          printf(BOLDRED"You are not supposed to see this \n"RESET);
        }
      }
    }
  }
  free(grpname);
  free(member_name);
  free(received_addr);
  free(msg);
  

  pthread_exit(NULL);
}


when we put atoi in comments the member_name holds it's original value The issue is very strange and we cannot find a solution. Any idea is appreciated thanks!

What we expected: Well we didn't expect for atoi to ruin the value of another string, we expected to see the same print statements. but we do not


Solution

  • You need to remove the msg string in unserialize tcp and use the message where msg goes