Skip to content

Commit

Permalink
Merge pull request #1778 from iconara/fix_1777
Browse files Browse the repository at this point in the history
Fix #1777: ServerSocket#accept_nonblock to return a correct Sockaddr
  • Loading branch information
headius committed Jul 25, 2014
2 parents 7211c60 + 97b15d3 commit 8ef711f
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ private RubyArray doAcceptNonblock(ThreadContext context, Channel channel) {

RubySocket socket = doAccept(context, channel);
SocketChannel socketChannel = (SocketChannel)socket.getChannel();
InetSocketAddress addr = (InetSocketAddress)socketChannel.socket().getLocalSocketAddress();
InetSocketAddress addr = (InetSocketAddress)socketChannel.socket().getRemoteSocketAddress();

return context.runtime.newArray(
socket,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
require 'socket'

#https://github.com/jruby/jruby/issues/1777
describe 'ServerSocket#accept_nonblock' do
it 'returns the client address' do
server_port = 2**15 + rand(2**15)
puts 'server running on %d' % server_port
addrinfos = Socket.getaddrinfo('localhost', server_port, nil, Socket::SOCK_STREAM)
_, port, _, ip, address_family, socket_type = addrinfos.shift
sockaddr = Socket.sockaddr_in(port, ip)
client_socket = Socket.new(address_family, socket_type, 0)
server_socket = ServerSocket.new(address_family, socket_type, 0)
server_socket.bind(sockaddr, 5)
begin
client_socket.connect_nonblock(sockaddr)
rescue Errno::EINPROGRESS, Errno::EALREADY
end
IO.select([server_socket])
client_socket, client_sockaddr = server_socket.accept_nonblock
port = Socket.unpack_sockaddr_in(client_sockaddr).first
port.should eq(client_socket.remote_address.ip_port)
port.should_not eq(server_port)
end
end

0 comments on commit 8ef711f

Please sign in to comment.