-1

I wrote a telnet server in C programing language. 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. To to that put this command in first line of main function :

signal(SIGINT, CtrlC_Handler);

and this is CtrlC_Handler function :

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 :

enter image description here

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 ?

f_y
  • 73
  • 9
  • Does your client code cope correctly with all the `EINVAL` errors that will result from closing the socket in the signal handler? What *is* your client code? – user207421 Dec 10 '19 at 06:42
  • 1
    @user207421: The client is `telnet`. It is not "OP's client", it is a standard command-line tool. – Amadan Dec 10 '19 at 06:43
  • @user207421 : I use telnet command-line tool. i did'n write client code myself – f_y Dec 10 '19 at 06:47
  • 2
    Pressing Ctrl-C on the client side does not send a signal to the server but to .. well .. the client. – alk Dec 10 '19 at 06:49

1 Answers1

4

Your server handles the interrupt signal, which the process will receive when the terminal running the server gets a Ctrl-C. It has nothing to do with Ctrl-C being pressed in telnet.

telnet reacts to Ctrl-C by sending two control sequences:

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 ).

Community
  • 1
  • 1
Amadan
  • 191,408
  • 23
  • 240
  • 301