| Class | UDPSocket |
| In: |
lib/resolv-replace.rb
ext/socket/socket.c |
| Parent: | Object |
| bind | -> | original_resolv_bind |
| connect | -> | original_resolv_connect |
| send | -> | original_resolv_send |
# File lib/resolv-replace.rb, line 25
25: def bind(host, port)
26: original_resolv_bind(IPSocket.getaddress(host), port)
27: end
# File lib/resolv-replace.rb, line 30
30: def connect(host, port)
31: original_resolv_connect(IPSocket.getaddress(host), port)
32: end
Receives up to maxlen bytes from udpsocket using recvfrom(2) after O_NONBLOCK is set for the underlying file descriptor. flags is zero or more of the MSG_ options. The first element of the results, mesg, is the data received. The second element, sender_inet_addr, is an array to represent the sender address.
When recvfrom(2) returns 0, Socket#recvfrom_nonblock returns an empty string as data. It means an empty packet.
require 'socket'
s1 = UDPSocket.new
s1.bind("127.0.0.1", 0)
s2 = UDPSocket.new
s2.bind("127.0.0.1", 0)
s2.connect(*s1.addr.values_at(3,1))
s1.connect(*s2.addr.values_at(3,1))
s1.send "aaa", 0
IO.select([s2])
p s2.recvfrom_nonblock(10) #=> ["aaa", ["AF_INET", 33302, "localhost.localdomain", "127.0.0.1"]]
Refer to Socket#recvfrom for the exceptions that may be thrown if the call to recvfrom_nonblock fails.
UDPSocket#recvfrom_nonblock may raise any error corresponding to recvfrom(2) failure, including Errno::EAGAIN.
/*
* call-seq:
* udpsocket.recvfrom_nonblock(maxlen) => [mesg, sender_inet_addr]
* udpsocket.recvfrom_nonblock(maxlen, flags) => [mesg, sender_inet_addr]
*
* Receives up to _maxlen_ bytes from +udpsocket+ using recvfrom(2) after
* O_NONBLOCK is set for the underlying file descriptor.
* _flags_ is zero or more of the +MSG_+ options.
* The first element of the results, _mesg_, is the data received.
* The second element, _sender_inet_addr_, is an array to represent the sender address.
*
* When recvfrom(2) returns 0,
* Socket#recvfrom_nonblock returns an empty string as data.
* It means an empty packet.
*
* === Parameters
* * +maxlen+ - the number of bytes to receive from the socket
* * +flags+ - zero or more of the +MSG_+ options
*
* === Example
* require 'socket'
* s1 = UDPSocket.new
* s1.bind("127.0.0.1", 0)
* s2 = UDPSocket.new
* s2.bind("127.0.0.1", 0)
* s2.connect(*s1.addr.values_at(3,1))
* s1.connect(*s2.addr.values_at(3,1))
* s1.send "aaa", 0
* IO.select([s2])
* p s2.recvfrom_nonblock(10) #=> ["aaa", ["AF_INET", 33302, "localhost.localdomain", "127.0.0.1"]]
*
* Refer to Socket#recvfrom for the exceptions that may be thrown if the call
* to _recvfrom_nonblock_ fails.
*
* UDPSocket#recvfrom_nonblock may raise any error corresponding to recvfrom(2) failure,
* including Errno::EAGAIN.
*
* === See
* * Socket#recvfrom
*/
static VALUE
udp_recvfrom_nonblock(int argc, VALUE *argv, VALUE sock)
{
return s_recvfrom_nonblock(sock, argc, argv, RECV_IP);
}
# File lib/resolv-replace.rb, line 35
35: def send(mesg, flags, *rest)
36: if rest.length == 2
37: host, port = rest
38: begin
39: addrs = Resolv.getaddresses(host)
40: rescue Resolv::ResolvError
41: raise SocketError, "Hostname not known: #{host}"
42: end
43: err = nil
44: addrs[0...-1].each {|addr|
45: begin
46: return original_resolv_send(mesg, flags, addr, port)
47: rescue SystemCallError
48: end
49: }
50: original_resolv_send(mesg, flags, addrs[-1], port)
51: else
52: original_resolv_send(mesg, flags, *rest)
53: end
54: end