用C中的Linux TCP/IP服务器处理CTRL 您所在的位置:网站首页 ns开机画面 用C中的Linux TCP/IP服务器处理CTRL

用C中的Linux TCP/IP服务器处理CTRL

2023-03-28 07:52| 来源: 网络整理| 查看: 265

问题描述

我目前正在使用Linux TCP/IP服务器.服务器程序正在在C中运行.我目前正在测试它,但是每次我使用CTRL-C退出它,它使用的端口尚未发布,它也不释放其编写的数据库.一个人如何定义接收CTRL+C信号时退出的子例程?

推荐答案

两个选项:

添加一个清理例程: int at atexit(void(*function)(void)(void)); hook ctrl+c with: sighandler_t signal(intnignum,signum,sighandler_t handler);

正如R所指出的, sigaction 比 推荐答案

Two options:

Add a cleanup routine with: int atexit(void (*function)(void)); Hook Ctrl+C with: sighandler_t signal(int signum, sighandler_t handler);

As R pointed out, sigaction is more portable than signal, but perhaps less idiomatic for Linux.

Warning: atexit routines won't run if your program is killed with SIGKILL (Ctrl+/) or any other unhandled signal is received.

其他推荐答案

Lookup setsockopt and SO_REUSEADDR. This option must have been set on the old original server process's socket or the new one will not be able to bind the port until the TIME_WAIT period expires.

其他推荐答案

@Bortds Generally the port won't be released immediately, you have to wait to for some time. I found this from a server project I worked.



【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有