简体   繁体   English

在 c 编程中处理客户端 - 服务器套接字中的 ctrl+c

[英]handle ctrl+c in client-server socket in c programing

I wrote a telnet server in C programing language.我用C编程语言写了一个telnet服务器。 Clients connect to the server using this command :客户端使用以下命令连接到服务器:

telnet *IP_Address*

I want when a client presses ctrl+c in its teminal, server closes the client connection but the server socket remains open.我希望当客户端在其终端中按下 ctrl+c 时,服务器关闭客户端连接但服务器套接字保持打开状态。 To to that put this command in first line of main function :为此,将此命令放在主函数的第一行:

signal(SIGINT, CtrlC_Handler);

and this is CtrlC_Handler function :这是 CtrlC_Handler 函数:

void CtrlC_Handler(int sig) {

     close(_c->_client_socket);
}

the problem is when client press ctrl-c in its terminal connection doesn't close and commandline terminal doesn't response to client :问题是当客户端在其终端连接中按 ctrl-c 没有关闭并且命令行终端没有响应客户端时:

在此处输入图片说明

But when the server socket is closed, client can types in terminal again.但是当服务器套接字关闭时,客户端可以再次输入终端。 server and client are in different servers.服务器和客户端在不同的服务器上。 can anybody help how can i caught ctrl+c signal in this case ?任何人都可以帮助我在这种情况下如何捕获 ctrl+c 信号?

Your server handles the interrupt signal, which the process will receive when the terminal running the server gets a Ctrl-C.您的服务器处理中断信号,当运行服务器的终端收到 Ctrl-C 时,进程将收到该信号。 It has nothing to do with Ctrl-C being pressed in telnet .它与在telnet中按下 Ctrl-C 无关。

telnet reacts to Ctrl-C by sending two control sequences: telnet通过发送两个控制序列来Ctrl-C

You can react to receiving these sequences (specifically, the first one in your case; reacting to the second one should be obligatory by the telnet protocol, but it's a moot point if you break the connection :P ).您可以对接收这些序列做出反应(特别是在您的情况下的第一个序列;对第二个序列做出反应应该是 telnet 协议的强制性要求,但如果您断开连接,这是一个有争议的问题:P)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM