You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Socket#accept_nonblock (ServerSocket#accept_nonblock in JRuby) returns the client socket and the client's address. However, in JRuby the address is wrong, the port is the server's and not the connecting client's.
Here's a demonstration, run with for example rvm jruby-1.7.12,ruby-2.1.1 do ruby path/to/script.rb:
require'socket'ServerSocketImpl=RUBY_ENGINE == 'jruby' ? ::ServerSocket : Socket# add `+ rand(100)` or something similar here if you want to run# multiple times and avoid getting "address already in use" errorserver_port=8888addrinfos=Socket.getaddrinfo('localhost',server_port,nil,Socket::SOCK_STREAM)_,port,_,ip,address_family,socket_type=addrinfos.shiftsockaddr=Socket.sockaddr_in(port,ip)client_socket=Socket.new(address_family,socket_type,0)server_socket=ServerSocketImpl.new(address_family,socket_type,0)ifRUBY_ENGINE == 'jruby'server_socket.bind(sockaddr,5)elseserver_socket.bind(sockaddr)server_socket.listen(5)endbeginclient_socket.connect_nonblock(sockaddr)rescueIO::EINPROGRESSWaitWritableendIO.select([server_socket])client_socket,client_sockaddr=server_socket.accept_nonblockport,host=Socket.unpack_sockaddr_in(client_sockaddr)puts"#{RUBY_ENGINE} Client connected from #{host}:#{port}"
This is the output I get:
jruby Client connected from 127.0.0.1:8888
ruby Client connected from ::1:52090
Notice that in JRuby the port is the one the server is running on and in MRI it's the port the client connects from.
The text was updated successfully, but these errors were encountered:
Socket#accept_nonblock
(ServerSocket#accept_nonblock
in JRuby) returns the client socket and the client's address. However, in JRuby the address is wrong, the port is the server's and not the connecting client's.Here's a demonstration, run with for example
rvm jruby-1.7.12,ruby-2.1.1 do ruby path/to/script.rb
:This is the output I get:
Notice that in JRuby the port is the one the server is running on and in MRI it's the port the client connects from.
The text was updated successfully, but these errors were encountered: