From 0d05a22602a49bdaba3f304ec6eb0808f1e6c286 Mon Sep 17 00:00:00 2001 From: Tomoyuki Sahara Date: Fri, 26 Dec 2014 16:00:50 +0900 Subject: don't call undefined method. fixes #15. --- mrblib/socket.rb | 7 ++++--- test/socket.rb | 17 +++++++++++++++++ 2 files changed, 21 insertions(+), 3 deletions(-) diff --git a/mrblib/socket.rb b/mrblib/socket.rb index e3a1487d4..2ff144080 100644 --- a/mrblib/socket.rb +++ b/mrblib/socket.rb @@ -63,9 +63,9 @@ class Addrinfo def inspect if ipv4? or ipv6? - if @protocol == Socket::IPPROTO_TCP + if @protocol == Socket::IPPROTO_TCP or (@socktype == Socket::SOCK_STREAM and @protocol == 0) proto = 'TCP' - elsif @protocol == Socket::IPPROTO_UDP + elsif @protocol == Socket::IPPROTO_UDP or (@socktype == Socket::SOCK_DGRAM and @protocol == 0) proto = 'UDP' else proto = '???' @@ -439,7 +439,8 @@ class Socket def recvfrom(maxlen, flags=0) msg, sa = _recvfrom(maxlen, flags) - [ msg, _ai_to_array(Addrinfo.new(sa)) ] + socktype = self.getsockopt(Socket::SOL_SOCKET, Socket::SO_TYPE).int + [ msg, Addrinfo.new(sa, Socket::PF_UNSPEC, socktype) ] end def recvfrom_nonblock(*args) diff --git a/test/socket.rb b/test/socket.rb index b602cc15d..517f5a00c 100644 --- a/test/socket.rb +++ b/test/socket.rb @@ -15,3 +15,20 @@ assert('Socket::getaddrinfo') do assert_equal Socket::SOCK_DGRAM, a[5] assert_equal Socket::IPPROTO_UDP, a[6] end + +assert('Socket#recvfrom') do + begin + sstr = "abcdefg" + s = Socket.new(Socket::AF_INET, Socket::SOCK_DGRAM, 0) + c = Socket.new(Socket::AF_INET, Socket::SOCK_DGRAM, 0) + s.bind(Socket.sockaddr_in(0, "127.0.0.1")) + c.send sstr, 0, s.getsockname + rstr, ai = s.recvfrom sstr.size + + assert_equal sstr, rstr + assert_true "127.0.0.1", ai.ip_address + ensure + s.close rescue nil + c.close rescue nil + end +end -- cgit v1.2.3