1

I created a chatroom with clients and a server and everything works. But I have a problem of printing. The problem is that the client can write his message and read the message from the other clients at the same time. So I used a thread to do both at the same time. It works but look at the example what the problem is :

Here I start writing my first messHere is the received message that interrupts my writingAnd here I continue writing

I don't know if it's clear : I start writing a message but I don't send it yet because it's not over but in the same time a message is received so it prints it after the beginning of my writing message and after I can continue writing.

Do you have an idea how to fix the problem ?

Here is the client code :

void getMessage(char* message) {
  size_t ln = -1;
  while (ln <= 0 || ln > MESSAGE_MAX-1) {
    printf(">");
    fgets(message, MESSAGE_MAX, stdin);
    ln = strlen(message) - 1;
  }

  if (message[ln] == '\n')
    message[ln] = '\0';
}

void *thread_message(void *arg)
{
  char* message;
  message = malloc (sizeof(char) * MESSAGE_MAX);
  int* sockfd = (int*)arg;

  while (1) {
    getMessage(message);
    if (send(*sockfd, message, strlen(message), 0) == -1){
      perror("Client: send message");
    }
  }
  pthread_exit(NULL);
}

int main(int argc, char *argv[])
{
  int sockfd, numbytes;  
  struct sockaddr_in their_addr;
         // connector's address information 
  struct hostent *he;
  char buf[MESSAGE_MAX + PSEUDO_MAX];

  // ...
  }

  while (1) { 
    if ((numbytes=recv(sockfd, buf, MESSAGE_MAX+PSEUDO_MAX, 0)) == -1) {
      perror("Client: recv");
      return EXIT_FAILURE;
    }

    // ...


      char* message;
      message = malloc (sizeof(char) * MESSAGE_MAX);

      // Here I launch the thread to write a message
      pthread_t thread;
      if (pthread_create(&thread, NULL, thread_message, &sockfd)) {
      perror("pthread_create");
      return EXIT_FAILURE;
      }
    }

    // Here I print the messages I receive
    else
    {
      buf[numbytes] = '\0';
      printf("%s",buf);
    }

  }

  close(sockfd);
  return EXIT_SUCCESS;
} 

Thanks for your help ! :)

Danvdb
  • 103
  • 2
  • 13
  • 1
    Why are you printing the received messages to the same buffer / location of the messages that are being typed by the local client? – o_weisman Dec 16 '14 at 12:38
  • Because there are many clients. So the other messages are coming from other clients and have to be printed : it's a chat between clients. – Danvdb Dec 16 '14 at 12:42
  • 1
    In a typical chat application, received messages and messages that were already sent, are printed to the main chat area, while messages that are being typed by the local user are printed to a different text box. How are you handling that? – o_weisman Dec 16 '14 at 13:22

2 Answers2

1

One way to solve your problem: maybe using getch from ncurses. Save what you have already inputted into a buffer. And display messages from other clients and your input in different line of the terminal.

Note: I never used libncurses before, but when i see examples of ncurses, i know it can fulfill the need of a chat program. Hope that helps.

D3Hunter
  • 1,329
  • 10
  • 21
0

First, I believe that you're title doesn't match your question. You want to know about printing with threads, right? Second, try to explain in more details where each message comes from and where you got confused.

Said that, I believe that your problem is thread synchronism.

  • How thread works? In a really simplified way, each thread will have a time slice to execute and then, it will give space for other threads from your program to execute. That's why you must use mutex to access shared variable, so threads don't use old values or overwrite data.
  • But how do I print consistently? Well, you may use semaphores. Check this other stackoverflow question. Check on google for examples in C, but this post gives you the basic knowledge to work this out.

:)

Community
  • 1
  • 1
delirium
  • 868
  • 6
  • 20