Cannot interrupt / terminate Ruby script with Ctrl + C on Windows

My script opens a TCP connection and reads data from the server. If the server is not responding, I try to terminate the script using Ctrl + C, but it does not work. The only way to abort the script is to kill the process in the task manager. Any ideas on how to abort such a script?

require 'socket'

host = '...'
port = ...

s = TCPSocket.open(host, port)

while line = s.gets
  puts line.chop
end

s.close 
+3
source share
3 answers

trap ("SIGINT") {clear; Output}

+1
source

I noticed that I press Ctrl + C, waiting for a response from the server, and for some time the last response arrives, the application terminates. But while I wait on the socket (s.gets), Ctrl + C has no effect.

, .

require 'socket'

host = '...'
port = ...

t = Thread.new do
  s = TCPSocket.open(host, port)

  while line = s.gets
    puts line.chop
  end

  s.close 
end

STDIN.getc

script, . .

+1

On Windows, type Ctrl + Pause

0
source

Source: https://habr.com/ru/post/1778402/


All Articles