Created
October 18, 2018 13:49
-
-
Save moret/768a75b7dae9e5b1d243425bbbdda870 to your computer and use it in GitHub Desktop.
ruby socket read timeout client-socket-options.rb
This file contains hidden or 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
#client-socket-options.rb | |
require 'socket' | |
host = '127.0.0.1' | |
port = 2000 | |
timeout = 2 | |
timeout_val = [ timeout, 0 ].pack("l_2") | |
s = Socket.new(Socket::AF_INET, Socket::SOCK_STREAM, 0) | |
s.setsockopt(::Socket::SOL_SOCKET, ::Socket::SO_RCVTIMEO, timeout_val) | |
s.setsockopt(::Socket::SOL_SOCKET, ::Socket::SO_SNDTIMEO, timeout_val) | |
s.connect(Socket.pack_sockaddr_in(port, host)) | |
begin | |
while data = s.read(1000) | |
puts data | |
end | |
rescue => e | |
puts e | |
end | |
s.close |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment