Skip to content

Instantly share code, notes, and snippets.

@jsvd
Created October 11, 2023 10:43
Show Gist options
  • Save jsvd/20c86b9751d39fc3162d0dd3a241a62e to your computer and use it in GitHub Desktop.
Save jsvd/20c86b9751d39fc3162d0dd3a241a62e to your computer and use it in GitHub Desktop.
require 'socket'
HOST = 'localhost'
PORT = 5555
def connect_and_close
socket = TCPSocket.new(HOST, PORT)
linger = [1,0].pack('ii')
socket.setsockopt(Socket::SOL_SOCKET, Socket::SO_LINGER, linger)
socket.close
end
def tcp_receiver(socket)
socket.each { |line| puts line }
rescue Errno::ECONNRESET
puts "connection reset"
end
server_thread = Thread.new do
server_socket = TCPServer.new(HOST, PORT)
loop do
socket = server_socket.accept
Thread.new(socket) do |socket|
tcp_receiver(socket)
end
end
end
sleep 1
10.times { connect_and_close }
server_thread.join
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment