I am trying to create a simple client-server chat program. On the client side, I unscrew another thread to read any incoming data from the server. The problem is that I want to gracefully complete this second thread when a person exits the main thread. I tried to use the general variable "running" to complete, the problem is that the socket read () command is a lock command, so if I execute while (running == 1), the server must send something before it is read read and the while condition can be checked again. I am looking for a method (only for common unix sockets) to do non-blocking reading, some form of peek () would basically work, as I can constantly check the loop to make sure I am done.
The read stream loop is lower, right now it does not have any mutex for common variables, but I plan to add that don't worry later!;)
void *serverlisten(void *vargp) { while(running == 1) { read(socket, readbuffer, sizeof(readbuffer)); printf("CLIENT RECIEVED: %s\n", readbuffer); } pthread_exit(NULL); }
source share