2016-10-04 67 views
-1

我用做在服务器端的插口上一次失败的关闭:在什么情况下在前台或后台完成关机/关闭?

struct linger so_linger; 
so_linger.l_onoff = 1; 
so_linger.l_linger = 0; 
setsockopt(s, SOL_SOCKET, SO_LINGER, &so_linger, socklen_t)sizeof(so_linger)); 

shutdown(s, SHUT_WR); 
close(s); 

哪里s是我关闭套接字。

它的工作原理,但我有时遇到问题,它似乎影响一些服务器,而不是其他人。一些在Ubuntu上运行,一些在CoreOS上运行。它在CoreOS上运行良好。

在Ubuntu上,尽管关闭了调用,但我仍然从与套接字相关的epoll_wait中获取了一个事件。

我认为这会立即发生。但是,如果你使用非阻塞I/O,我想这可能不是这种情况。

它基本上意味着我在epoll_wait中获得了一个ev.data.ptr值集合中指向已被销毁的事件的事件。

所以,问题是,这是真的吗?半关闭不会从epoll中删除事件描述符,并且关闭将不会与非阻塞I/O同步?

因此,如果我不再需要事件,我应该实际上用EPOLL_CTL_DEL手动删除描述符?

+0

您正在发送FIN然后是RST。为什么? – EJP

+0

@EJP - 你的意思是这不是在做一个失败的关闭?它似乎虽然工作。 – Matt

回答

0

关闭套接字并将其从epoll集中清除。但有一个警告。参照epoll man page

> Q6 Will closing a file descriptor cause it to be removed from all 
      epoll sets automatically? 

    A6 Yes, but be aware of the following point. A file descriptor is a 
     reference to an open file description (see open(2)). Whenever a 
     file descriptor is duplicated via dup(2), dup2(2), fcntl(2) 
     F_DUPFD, or fork(2), a new file descriptor referring to the same 
     open file description is created. An open file description 
     continues to exist until all file descriptors referring to it 
     have been closed. A file descriptor is removed from an epoll set 
     only after all the file descriptors referring to the underlying 
     open file description have been closed (or before if the file 
     descriptor is explicitly removed using epoll_ctl(2) 
     EPOLL_CTL_DEL).This means that even after a file descriptor 
     that is part of an epoll set has been closed, events may be 
     reported for that file descriptor if other file descriptors 
     referring to the same underlying file description remain open. 

因此,有报道其close被称为插座上的事件的可能性。不确定您的测试是否适合您的测试,在某些平台上可以正常工作。当然,我不认为这与异步I/O有关。

相关问题