diff --git a/ext/socket/socket.c b/ext/socket/socket.c index 3c91ec472a..8aed248b29 100644 --- a/ext/socket/socket.c +++ b/ext/socket/socket.c @@ -2931,15 +2931,18 @@ sock_accept_nonblock(sock) * * Socket#accept */ static VALUE -sock_sysaccept(VALUE sock) +sock_sysaccept(sock) + VALUE sock; { OpenFile *fptr; + VALUE sock2; char buf[1024]; socklen_t len = sizeof buf; GetOpenFile(sock, fptr); - return rb_assoc_new(s_accept(0,fptr->fd,(struct sockaddr*)buf,&len), - rb_str_new(buf, len)); + sock2 = s_accept(0,fptr->fd,(struct sockaddr*)buf,&len); + + return rb_assoc_new(sock2, rb_str_new(buf, len)); } #ifdef HAVE_GETHOSTNAME diff --git a/test/socket/test_socket.rb b/test/socket/test_socket.rb index 6b4805bbd0..00a16b1529 100644 --- a/test/socket/test_socket.rb +++ b/test/socket/test_socket.rb @@ -79,5 +79,19 @@ class TestSocket < Test::Unit::TestCase assert_raise(ArgumentError) { Socket.unpack_sockaddr_un(sockaddr_in) } sockaddr_un = Socket.sockaddr_un("/tmp/s") assert_raise(ArgumentError) { Socket.unpack_sockaddr_in(sockaddr_un) } + end if Socket.respond_to?(:sockaddr_un) + + def test_sysaccept + serv = Socket.new(Socket::AF_INET, Socket::SOCK_STREAM, 0) + serv.bind(Socket.sockaddr_in(0, "127.0.0.1")) + serv.listen 5 + c = Socket.new(Socket::AF_INET, Socket::SOCK_STREAM, 0) + c.connect(serv.getsockname) + fd, peeraddr = serv.sysaccept + assert_equal(c.getsockname, peeraddr) + ensure + serv.close if serv + c.close if c + IO.for_fd(fd).close if fd end -end if defined?(Socket) && Socket.respond_to?(:sockaddr_un) +end if defined?(Socket)