Reputation: 3890
My server needs to send data when client connects to it. I am using Epoll ET mode. But how to do it? Could any one give me a simple example for me?
Upvotes: 0
Views: 610
Reputation: 5720
Assuming you are listening on your socket (socket
, bind
, listen
), and have added it's descriptor to epoll (epoll_create
and epoll_ctl
), then epoll_wait
will tell you when there is a new connection to accept.
First you accept the connection (sockfd
is descriptor of socket you're listening on, efd
is epoll instance) and add it to your epoll instance:
int connfd = accept4(sockfd, NULL, 0, SOCK_NONBLOCK);
struct epoll_event ev;
ev.events = EPOLLOUT | EPOLLET;
ev.data.fd = connfd;
epoll_ctl(efd, EPOLL_CTL_ADD, connfd, &ev)
Then you go back to your main loop and call epoll_wait
again. It will tell you when the socket is ready for writing, and you just happily write
or sendfile
away.
Add lots of error checking, and probably TCP_CORK
and you're done. There's a working example on github.com/grahamking/netshare/.
I hope this gives you enough information to get started.
Upvotes: 1