multithreading - Unable to make socket Accept Non Blocking ruby 2.2 -
i have been searching whole day socket accept non blocking. found recv non blocking wouldn't benefit me in anyway. script first starts new socket class. binds client ip 127.0.0.1 , port 6112. starts multi threading. multi threading takes @sock.accept. << blocking. have used accept_nonblock. though, throw me following error:
io::ewouldblockwaitreadable : non-blocking socket operation not completed immediately. - accept(2) block i using ruby 2.2. note: not intend use rails solve problem, or give me shortcut. sticking pure ruby 2.2. here script:
require 'socket' include socket::constants @sock = socket.new(af_inet, sock_stream, 0) @sockaddr = socket.sockaddr_in(6112, '127.0.0.1') @sock.bind(@sockaddr) @sock.listen(5) thread.new(@sock.accept_nonblock) |connection| @client = client.new(ip, connection, self) @clients.push(@client) begin while connection packet = connection.recv(55555) if packet == nil deleteclient(connection) else @toput = "[recv]: #{packet}" puts @toput end end rescue exception => e if e.class != ioerror line1 = e.backtrace[0].split(".rb").last line = line1.split(":")[1] @log.error(e, e.class, e.backtrace[0].split(".rb").first + ".rb",line) puts "#{ e } (#{ e.class })" end end def deleteclient(connection) @clients.delete(@client) connection.close end
http://docs.ruby-lang.org/en/2.2.0/socket.html#method-i-accept_nonblock
accept_nonblock raises exception when can't accept connection. expected rescue exception , io.select socket.
begin # emulate blocking accept client_socket, client_addrinfo = socket.accept_nonblock rescue io::waitreadable, errno::eintr io.select([socket]) retry end a patch has recently been accepted add exception: false option accept_nonblock, allow use without using exceptions flow control. don't know it's shipped yet, though.
Comments
Post a Comment