I have this UDP Client-Server chat, a small program that I made for a university course, everything runs just fine. But the professor asked me what would happen if a user crashed while sending a message, the server will keep waiting to receive the message from the client that does not exist anymore, so will wait forever. I tried to think of a solution but couldn't find one and really hope someone can help me.
I will leave just the snippet of code that causes the issue. If needed I can provide more or even the full repo.
void Forward_message(int sockfd, ListHead* head, struct sockaddr_in cliaddr){
Message msg = {0};
int len = sizeof(cliaddr);
if(head->size >1){
int read_bytes;
do{
read_bytes = 0;
read_bytes = recvfrom(sockfd,(Message *)&msg, sizeof(Message),MSG_WAITALL,(struct sockaddr* )&cliaddr, &len );
if(read_bytes == -1)perror("An error occurred while receiving Message from the Client"),exit(1);
}while( (read_bytes != sizeof(Message)) );
Send_msg(&msg,sockfd,head);
printf("\nmessage forwarded\n");
}
}
The issue is caused by the recvfrom
inside the do
while
.