Skip to content

Instantly share code, notes, and snippets.

@lucacervasio
Last active December 20, 2015 12:29
Show Gist options
  • Save lucacervasio/6131451 to your computer and use it in GitHub Desktop.
Save lucacervasio/6131451 to your computer and use it in GitHub Desktop.
Ruby tcp socket server with socket select
#!/usr/bin/env ruby
require 'socket'
@mysocket = TCPSocket.new('127.0.0.1', 9761)
while true
ready = IO.select([@mysocket])
readable = ready[0]
readable.each do |socket|
if socket == @mysocket
buf = @mysocket.recv_nonblock(1024)
if buf.length == 0
puts "The server connection is dead. Exiting."
exit
else
puts "Received a message: #{buf}"
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment