Unix : Epoll, 在服务器中捕获 ctrl+d 和 ctrl+c [英] Unix : Epoll, catch ctrl+d and ctrl+c in server

查看:100
本文介绍了Unix : Epoll, 在服务器中捕获 ctrl+d 和 ctrl+c的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用epoll搭建了一个服务器,这是我初始化epoll的代码:

I use epoll to build a server, this is the code where I init epoll :

core->fd_epoll = epoll_create(LIMIT_CLIENT);
ev.events = EPOLLIN | EPOLLPRI | EPOLLERR | EPOLLHUP;
ev.data.fd = core->socket_main;
epoll_ctl(core->fd_epoll, EPOLL_CTL_ADD, core->socket_main, &ev);
while (1)
{
  nfds = epoll_wait(core->fd_epoll, &ev, 90000, -1);
  ...
}

当我用它来检查我的 fds 上是否有新东西时:

And when I use it to check if there's something new on my fds :

for (i = 0; i < nfds; i++)
{
  fd = ev[i].data.fd;
  if (fd == core->socket_main)
    {
      socket_fils = socket_accept(core->socket_main, 0);
      event.data.fd = socket_fils;
      event.events = EPOLLIN | EPOLLET | EPOLLRDHUP;
      xepoll_ctl(core->fd_epoll, EPOLL_CTL_ADD, socket_fils, &event);
      printf("Incoming => FD fils %d\n", socket_fils);
    }
  else
    printf("Event %x\n", ev[i].events);
}

当我使用 netcat 向服务器发送消息时,位域事件等于 1 (EPOLLIN)当我发送 ctrl+c 时,netcat 退出并且我的位域等于 2001(EPOLLIN 和 EPOLLRDHUP)当我发送 ctrl+d 时,netcat 不会退出,但我的位域也等于 2001...

When I use netcat to send a message to the server the bitfield events is equal to 1 (EPOLLIN) When I send a ctrl+c, netcat quits and my bitfield is equal to 2001 (EPOLLIN and EPOLLRDHUP) When I send a ctrl+d, netcat doesn't quit but my bitfield is equal to 2001 too...

在 ctrl+d 之后,我的服务器关闭了套接字.这不正常...... ctrl+d 不应该关闭套接字并返回不同的位域.

After a ctrl+d, my server closes the socket. It's not normal... A ctrl+d should'nt close the socket and return a different bitfield.

我怎么知道,在服务器中,是 ctrl+c 还是 ctrl+d ?

How can I know, in the server, if it's ctrl+c or ctrl+d ?

谢谢.

推荐答案

ctrl+cctrl+d 您的服务器无法直接看到"运行 netcat 的终端上的按键.它们分别导致向 netcat 发送一个 SIGINT 信号,并导致 netcat 在其标准输入上看到一个 EOF 条件.netcat 用它做什么实际上取决于 netcat,而不取决于您的服务器.这是他们为我做的事情:

ctrl+c and ctrl+d keypresses on the terminal that is running netcat cannot be "seen" directly by your server. They cause, respectively, a SIGINT signal to be sent to netcat, and an EOF condition to be seen by netcat on its stdin. What netcat does with that is really up to netcat, not up to your server. Here's what they do for me:

  • ctrl+c 将 SIGINT 发送给 netcat:netcat 被杀死,因为这是 SIGINT 的默认操作,而 netcat不会改变它.当 netcat 死掉时,套接字会自动关闭.服务器将此视为可用的传入数据,与您看到的 EPOLLIN|EPOLLRDHUP 条件一致.如果你阅读socket,你会发现一个EOF在等着你.

  • ctrl+c which sends SIGINT to netcat: netcat is killed because that is the default action of SIGINT, and netcat doesn't change it. When netcat dies the socket is automatically closed. The server senses this as available incoming data, consistent with the EPOLLIN|EPOLLRDHUP condition you are seeing. If you read the socket, you will find that an EOF is waiting for you.

ctrl+d 在 netcat 的标准输入上发送 EOF:netcat 注意到了 EOF.它不会通过套接字发送更多数据.但是,如果服务器有更多数据要发送,它会继续运行并从套接字读取数据.

ctrl+d which sends an EOF on netcat's stdin: netcat noticed the EOF. It will send no further data through the socket. However, it continues running and reading from the socket in case the server has more data to send.

换句话说,我无法重现您看到的 netcat 行为(使用 Linux 2.6 和 netcat v1.10-38).也许您的 netcat 版本在读取 stdin 上的 EOF 后关闭了套接字以进行写入?

In other words, I can't reproduce the netcat behaviour you are seeing (with Linux 2.6 and netcat v1.10-38). Perhaps your version of netcat shuts down the socket for writing after reading an EOF on stdin?

这篇关于Unix : Epoll, 在服务器中捕获 ctrl+d 和 ctrl+c的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆