This is not any error in code, but to get an idea how to accomplish the following criteria. I have seen similar questions here and here, but did not get any particular way to do it.
Consider the following parts of code:
....
pthread_create(&Receiver,NULL,ReceiveFromServer,(void*)&ClientSoc);
sending =1;
while (sending)
{
SHOW_PROMPT;
gets(message);
memcpy(Packet.data,message,MAX_DATA_LENGTH);
if (SetCommand(&Packet,message) == CMD_EXT)
sending = 0;
send(ClientSoc,&Packet,PACKET_SIZE,0);
}
close(ClientSoc);
pthread_join(Receiver,NULL);
return 0;
....
And the ReceiveFromServer
function:
void* ReceiveFromServer(void* ClientSoc)
{
int receiving =1;
int Status;
strPacket Packet;
while(receiving)
{
if(recv(*(int*)ClientSoc,&Packet,PACKET_SIZE,0)>0)
{
ParseReply(Packet);
SHOW_PROMPT;
}
if(GET_COMMAND(Packet.Header) == CMD_EXT)
receiving = 0;
}
return NULL;
}
Assume everything is declared or defined correctly.
So the thread terminates depending upon the received data from server, but the sender still loops as it do not know that the receiver is terminated. The sender only comes out of loop depending upon the user input (a particular word such as 'exit').
How to notify the parent that the thread is terminating?
I tried to make sending
global and change it from inside the thread, but it didn't work.