Class: UDPSocket
- Inherits:
-
IPSocket
- Object
- IO
- BasicSocket
- IPSocket
- UDPSocket
- Defined in:
- socket.c
Instance Method Summary collapse
- #bind ⇒ Object
- #connect ⇒ Object
- #initialize ⇒ Object constructor
-
#recvfrom_nonblock(*args) ⇒ Object
Receives up to maxlen bytes from
udpsocketusing recvfrom(2) after O_NONBLOCK is set for the underlying file descriptor. - #send ⇒ Object
Methods inherited from IPSocket
#addr, getaddress, #peeraddr, #recvfrom
Methods inherited from BasicSocket
#close_read, #close_write, do_not_reverse_lookup, do_not_reverse_lookup=, for_fd, #getpeername, #getsockname, #getsockopt, #recv, #recv_nonblock, #setsockopt, #shutdown
Constructor Details
#initialize ⇒ Object
1715 1716 1717 |
# File 'socket.c', line 1715 static VALUE udp_init(argc, argv, sock) int argc; |
Instance Method Details
#bind ⇒ Object
1776 1777 1778 |
# File 'socket.c', line 1776 static VALUE udp_bind(sock, host, port) VALUE sock, host, port; |
#connect ⇒ Object
1758 1759 1760 |
# File 'socket.c', line 1758 static VALUE udp_connect(sock, host, port) VALUE sock, host, port; |
#recvfrom_nonblock(maxlen) ⇒ Array #recvfrom_nonblock(maxlen, flags) ⇒ Array
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 theMSG_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() 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
1877 1878 1879 1880 1881 |
# File 'socket.c', line 1877 static VALUE udp_recvfrom_nonblock(int argc, VALUE *argv, VALUE sock) { return s_recvfrom_nonblock(sock, argc, argv, RECV_IP); } |
#send ⇒ Object
1798 1799 1800 |
# File 'socket.c', line 1798 static VALUE udp_send(argc, argv, sock) int argc; |