I have problem with ending detached thread:
void fun_5(int sock_connect)
{
unsigned char buff2[76] = {0x01 ,0x01 ,0x02 ,0x02 ,0x00 ,0x4a ,0x31 ,0x00 ,0x00 ,0x00 ,0x00 ,0x00 ,0x00 ,0x00 ,0x00 ,0x00 ,0x00 ,0x00 ,0x00 ,0x00 ,0x04 ,0x00 ,0x00 ,0x50 ,0x00 ,0x00 ,0x00 ,0x00 ,0x00 ,0x50 ,0x4c ,0x4d ,0x4f ,0x4d ,0x49 ,0x46 ,0x41 ,0x58 ,0x20 ,0x20 ,0x4e ,0x52 ,0x20 ,0x41 ,0x00 ,0x34 ,0x41 ,0x50 ,0x3c ,0x32 ,0x31 ,0x3e ,0x35 ,0x46 ,0x2e ,0x52 ,0x32 ,0x31 ,0x30 ,0x2f ,0x31 ,0x35 ,0x2f ,0x31 ,0x32 ,0x31 ,0x35 ,0x3a ,0x31 ,0x31 ,0x3a ,0x30 ,0x36 ,0x00 ,0xff ,0xef};
while(thfive)
{
if((send( sock_connect, buff2, sizeof(buff2), MSG_NOSIGNAL ) ) <= 0 )
{
close(sock_connect);
break;
}
sleep(5);
}
}
This thread sends bytes in every 5 sec when client which connect to server(my program) sends specified sequence of bytes("start"), and it should stop sending when client sends other sequence("stop").
while(thfive) <<- thfive is true when client sends "start", and its false when client sends "stop"
But when client sends "start" after "stop" in less then 5 sec, thread running all the time. Because of that this solution is not the best. There is option to checking condition even when thread sleep? Or how I can kill this detached thread from main thread?
It works in detached thread, because in the same time, program doing other functions. I have more threads like this in the same time ( which sends bytes every 1.5 sec, 10 sec etc). I'm still a novice in programming, and the first time I use multitasking.
Thanks.
Edit 1: My OS is Linux.