networking - non blocking tcp client socket in ruby failed -


trying create tcp socket in non-blocking manner, failed. idea?

$ ruby nonblock_sock.rb  /home/tom/.rvm/rubies/ruby-2.3.1/lib/ruby/2.3.0/socket.rb:1207:in `__connect_nonblock': operation in progress - connect(2) block (io::einprogresswaitwritable)     /home/tom/.rvm/rubies/ruby-2.3.1/lib/ruby/2.3.0/socket.rb:1207:in `connect_nonblock'     nonblock_sock.rb:6:in `<main>' 

here code snippet

#not working yet,  require 'socket'  socket = socket.new(socket::af_inet, socket::sock_stream, 0)  sockaddr = socket.pack_sockaddr_in(80, 'localhost')  socket.connect_nonblock(sockaddr); 

change to:

require 'socket' socket = socket.new(socket::af_inet, socket::sock_stream, 0)  sockaddr = socket.pack_sockaddr_in(80, '127.0.0.1')  #this socket.connect_nonblock(sockaddr) 

if recall correctly, 1 needs pass ip instead of hostname(localhost) while creating address. this, connect_nonblock should raise einprogress connecting in non-blocking manner in background think can check io.select later.

edit: io::einprogresswaitwritable raised expected. connect_nonblock leaves connection establishment process background , raises it.

you should handling this:

begin   socket.connect_nonblock(sockaddr) rescue errno::einprogress    io.select(nil, [socket]) #wait socket writable   begin     socket.connect_nonblock(sockaddr)   rescue errno::eisconn     #=> means connection remote host has established successfully.     socket.write("stuff")   end end 

Comments

Popular posts from this blog

ios - MKAnnotationView layer is not of expected type: MKLayer -

ZeroMQ on Windows, with Qt Creator -

unity3d - Unity SceneManager.LoadScene quits application -