Commit 70aaa378 authored by Samarth Joshi's avatar Samarth Joshi

Adding code to remove fd from epoll instance after socket closed by client

parent 409ba370
File added
......@@ -63,6 +63,7 @@ void func(int sockfd,struct message* requestMessage)
n++;
printf("[Message sent to server]\n[[Status:%c]\n[Key:%s]\n[Value:%s]]\n",requestMessage->status,requestMessage->key,requestMessage->value);
write(sockfd, requestMessage, sizeof(struct message));
sleep(2);
}
//printf("[Message sent to server]\n[[Status:%c]\n[Key:%s]\n[Value:%s]]",requestMessage->status,requestMessage->key,requestMessage->value);
......
File added
......@@ -108,7 +108,7 @@ void *worker(void *args) {
read(read_pipe, newfd, sizeof(newfd));
printf("\nread %d\n", *newfd);
ev.data.fd=*newfd;
ev.events = EPOLLIN;
ev.events = EPOLLIN | EPOLLRDHUP;
if (epoll_ctl(epollfd, EPOLL_CTL_ADD, *newfd, &ev) == -1) {
perror("epoll_ctl: read_pipe");
exit(EXIT_FAILURE);
......@@ -117,21 +117,26 @@ void *worker(void *args) {
fflush(stdout);
}
else{
int flag = events[i].events;
struct message *requestMessage= malloc(sizeof(struct message));
int readlength=read(events[i].data.fd , requestMessage, sizeof(struct message));
if(requestMessage->status ==EOF)
{
epoll_ctl( epollfd, EPOLL_CTL_DEL, events[i].data.fd , NULL );
close(events[i].data.fd);
if (flag & EPOLLRDHUP) {
/* Connection was closed.
Remove socket from epoll instance
Close the socket and dont process more info from this socket
*/
char eof;
read(events[i].data.fd , &eof, sizeof(char));
epoll_ctl( epollfd, EPOLL_CTL_DEL, events[i].data.fd , NULL );
close(events[i].data.fd);
continue;
}
else
{
if (flag & EPOLLIN) {
struct message *requestMessage= malloc(sizeof(struct message));
int readlength=read(events[i].data.fd , requestMessage, sizeof(struct message));
printf("\n[Message Received from client]\n[[Status:%c][Key:%s][Value:%s]]",requestMessage->status,requestMessage->key,requestMessage->value);
fflush(stdout);
}
}
}
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment