Skip to content

Fixed server crash issue #3

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions AF_UNIX/server.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ main(int argc, char *argv[])
int result;
int data;
char buffer[BUFFER_SIZE];
int clientExited = 0; // for avoiding the server crash in case client closes connection

/*In case the program exited inadvertently on the last run,
*remove the socket.
Expand Down Expand Up @@ -112,12 +113,26 @@ main(int argc, char *argv[])
perror("read");
exit(EXIT_FAILURE);
}

else if (ret == 0) // check if client exited
{
printf("Client exited\n");
clientExited = 1;
break;
}

/* Add received summand. */
memcpy(&data, buffer, sizeof(int));
if(data == 0) break;
result += data;
}

// skip writing in case client exited
if (clientExited)
{
clientExited = 0;
continue;
}

/* Send result. */
memset(buffer, 0, BUFFER_SIZE);
Expand Down