Last active
December 20, 2015 12:29
-
-
Save lucacervasio/6131451 to your computer and use it in GitHub Desktop.
Ruby tcp socket server with socket select
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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