i wrote a little program an found a strange effect in the epoll
implementation. The following program simple tests these epoll api. It
create a socket and a epoll instance. If epoll_wait waits for the socket
and only EPOLLOUT is requested, epoll_wait returns and have the EPOLLHUP
flag set in the returned event structure.
I think if i request only the EPOLLOUT event epoll_wait only give me all
EPOLLOUT event and NOT the EPOLLHUP events.
My system:
SuSE Linux 9.0
Kernel 2.6.4 (self compiled)
Does anybody have the same issue?
Thanks in advance,
A. Roth
#include <stdio.h>
#include <sys/epoll.h>
#include <sys/socket.h>
int main(int argc,char ** argv)
{
int sock;
int epfd;
int i;
int res;
struct epoll_event events[8];
struct epoll_event ev;
sock = socket(AF_INET,SOCK_STREAM,0);
printf("Socket descriptor %i\n",sock);
epfd = epoll_create(8);
printf("Epoll descriptor %i\n",epfd);
ev.data.u32 = 1;
ev.events = EPOLLOUT;
res = epoll_ctl(epfd,EPOLL_CTL_ADD,sock,&ev);
printf("epoll_ctl returned %i\n",res);
for(i = 0; i < 5; i++)
{
res = epoll_wait(epfd,events,8,-1);
printf("epoll_wait returned %i\n",res);
for(;res>0;res--)
{
printf("event %X id %i\n",events[res-1].events,events[res-1].data.u32);
}
}
close(epfd);
close(sock);
}
>Hi,
>
>i wrote a little program an found a strange effect in the epoll
>implementation. The following program simple tests these epoll api. It
>create a socket and a epoll instance. If epoll_wait waits for the socket
>and only EPOLLOUT is requested, epoll_wait returns and have the EPOLLHUP
>flag set in the returned event structure.
>I think if i request only the EPOLLOUT event epoll_wait only give me all
>EPOLLOUT event and NOT the EPOLLHUP events.
>
>My system:
>SuSE Linux 9.0
>Kernel 2.6.4 (self compiled)
>
>Does anybody have the same issue?
This is expected behaviour. Precisely the same occurs with poll().
EPOLLHUP and EPOLLERR are only ever set in the returned events mask --
you don't set them in the epoll_ctl() call(s). EPOLLHUP means the
other end of a communication was closed. In this particular case, it
is returned for an unconnected stream socket.
Cheers,
Michael
If this is the 'by design' behaviour it's ok. But how can i wait on a
socket until the socket has connected? If i can epoll_wait i always
returns EPOLLHUP until the socket is connected (so i get a busy loop).
Is there any possiblity to wait for a connect without epoll?
Andreas
> If this is the 'by design' behaviour it's ok. But how can i wait on a
> socket until the socket has connected? If i can epoll_wait i always
> returns EPOLLHUP until the socket is connected (so i get a busy loop).
> Is there any possiblity to wait for a connect without epoll?
To wait until a socket has connected, wait only for writability.
DS
This is a somewhat different question than you origianlly asked...
Note the following:
If you do a normal connect(), then it blocks until the connection has
been completed. There's thus no need for poll()/epoll()/select().
If you make the socket non-blocking before you do the connect() (you
don't say this, but I suspect it's the scenario you are thinking of),
then
a) the three-way handshake (assuming TCP) will be initiated.
b) connect() will return immediately with the error EINPROGRESS (may
vary by platform).
c) a later select()/poll()/epoll() with a non-zero timeout will block
until the connection completes, and then indicate the socket as being
writable (writefds / POLLOUT / EPOLLOUT).
Cheers,
Michael