Class: UDPSocket
Relationships & Source Files | |
Super Chains via Extension / Inclusion / Inheritance | |
Class Chain:
|
|
Instance Chain:
|
|
Inherits: |
IPSocket
|
Defined in: | ext/socket/udpsocket.c, ext/socket/lib/socket.rb |
Overview
UDPSocket
represents a UDP/IP socket.
Class Attribute Summary
::BasicSocket
- Inherited
.do_not_reverse_lookup | Gets the global do_not_reverse_lookup flag. |
.do_not_reverse_lookup= | Sets the global do_not_reverse_lookup flag. |
Class Method Summary
-
.new([address_family]) ⇒ socket
constructor
Creates a new
UDPSocket
object.
::IPSocket
- Inherited
.getaddress | Lookups the IP address of host. |
::BasicSocket
- Inherited
.for_fd | Returns a socket object which contains the file descriptor, fd. |
Instance Attribute Summary
::BasicSocket
- Inherited
#do_not_reverse_lookup | Gets the do_not_reverse_lookup flag of basicsocket. |
#do_not_reverse_lookup= | Sets the do_not_reverse_lookup flag of basicsocket. |
Instance Method Summary
-
#bind(host, port) # = > 0))
Binds udpsocket to host:port.
-
#connect(host, port) ⇒ 0
Connects udpsocket to host:port.
-
#recvfrom_nonblock(maxlen [, flags[, outbuf [, options]]]) ⇒ Array, sender_inet_addr
Receives up to maxlen bytes from
udpsocket
using recvfrom(2) after O_NONBLOCK is set for the underlying file descriptor. -
#send(mesg, flags, host, port) ⇒ numbytes_sent
Sends mesg via udpsocket.
- #__recvfrom_nonblock(len, flg, str, ex) private Internal use only
::IPSocket
- Inherited
#addr | Returns the local address as an array which contains address_family, port, hostname and numeric_address. |
#inspect | Return a string describing this |
#peeraddr | Returns the remote address as an array which contains address_family, port, hostname and numeric_address. |
#recvfrom | Receives a message and return the message as a string and an address which the message come from. |
::BasicSocket
- Inherited
#close_read | Disallows further read using shutdown system call. |
#close_write | Disallows further write using shutdown system call. |
#connect_address | Returns an address of the socket suitable for connect in the local machine. |
#getpeereid | Returns the user and group on the peer of the UNIX socket. |
#getpeername | Returns the remote address of the socket as a sockaddr string. |
#getsockname | Returns the local address of the socket as a sockaddr string. |
#getsockopt | Gets a socket option. |
#local_address | Returns an |
#recv | Receives a message. |
#recv_nonblock | Receives up to maxlen bytes from |
#recvmsg | recvmsg receives a message using recvmsg(2) system call in blocking manner. |
#recvmsg_nonblock | recvmsg receives a message using recvmsg(2) system call in non-blocking manner. |
#remote_address | Returns an |
#send | send mesg via basicsocket. |
#sendmsg | sendmsg sends a message using sendmsg(2) system call in blocking manner. |
#sendmsg_nonblock | sendmsg_nonblock sends a message using sendmsg(2) system call in non-blocking manner. |
#setsockopt | Sets a socket option. |
#shutdown | Calls shutdown(2) system call. |
#__recvmsg, #__recvmsg_nonblock, #__sendmsg, #__sendmsg_nonblock, #read_nonblock, #write_nonblock, #__read_nonblock, #__recv_nonblock, #__write_nonblock |
Constructor Details
.new([address_family]) ⇒ socket
Creates a new UDPSocket
object.
address_family should be an integer, a string or a symbol: Socket::AF_INET, “AF_INET”, :INET
, etc.
require 'socket'
UDPSocket.new #=> #<UDPSocket:fd 3>
UDPSocket.new(Socket::AF_INET6) #=> #<UDPSocket:fd 4>
# File 'ext/socket/udpsocket.c', line 28
static VALUE udp_init(int argc, VALUE *argv, VALUE sock) { VALUE arg; int family = AF_INET; int fd; if (rb_scan_args(argc, argv, "01", &arg) == 1) { family = rsock_family_arg(arg); } fd = rsock_socket(family, SOCK_DGRAM, 0); if (fd < 0) { rb_sys_fail("socket(2) - udp"); } return rsock_init_sock(sock, fd); }
Instance Method Details
#__recvfrom_nonblock(len, flg, str, ex) (private)
# File 'ext/socket/udpsocket.c', line 226
static VALUE udp_recvfrom_nonblock(VALUE sock, VALUE len, VALUE flg, VALUE str, VALUE ex) { return rsock_s_recvfrom_nonblock(sock, len, flg, str, ex, RECV_IP); }
#bind(host, port) # = > 0))
# File 'ext/socket/udpsocket.c', line 131
static VALUE udp_bind(VALUE sock, VALUE host, VALUE port) { struct udp_arg arg; VALUE ret; GetOpenFile(sock, arg.fptr); arg.res = rsock_addrinfo(host, port, rsock_fd_family(arg.fptr->fd), SOCK_DGRAM, 0); ret = rb_ensure(udp_bind_internal, (VALUE)&arg, rsock_freeaddrinfo, (VALUE)arg.res); if (!ret) rsock_sys_fail_host_port("bind(2)", host, port); return INT2FIX(0); }
#connect(host, port) ⇒ 0
# File 'ext/socket/udpsocket.c', line 86
static VALUE udp_connect(VALUE sock, VALUE host, VALUE port) { struct udp_arg arg; VALUE ret; GetOpenFile(sock, arg.fptr); arg.res = rsock_addrinfo(host, port, rsock_fd_family(arg.fptr->fd), SOCK_DGRAM, 0); ret = rb_ensure(udp_connect_internal, (VALUE)&arg, rsock_freeaddrinfo, (VALUE)arg.res); if (!ret) rsock_sys_fail_host_port("connect(2)", host, port); return INT2FIX(0); }
#recvfrom_nonblock(maxlen [, flags[, outbuf [, options]]]) ⇒ Array
, 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#recv_nonblock
returns nil. In most cases it means the connection was closed, but it may also mean an empty packet was received, as the underlying API makes it impossible to distinguish these two cases.
Parameters
-
maxlen
- the number of bytes to receive from the socket -
flags
- zero or more of theMSG_
options -
outbuf
- destination String buffer -
options
- keyword hash, supportingexception: false
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
begin # emulate blocking recvfrom
p s2.recvfrom_nonblock(10) #=> ["aaa", ["AF_INET", 33302, "localhost.localdomain", "127.0.0.1"]]
rescue IO::WaitReadable
IO.select([s2])
retry
end
Refer to Socket#recvfrom for the exceptions that may be thrown if the call to recvfrom_nonblock fails.
recvfrom_nonblock
may raise any error corresponding to recvfrom(2) failure, including Errno::EWOULDBLOCK
.
If the exception is Errno::EWOULDBLOCK
or Errno::EAGAIN
, it is extended by IO::WaitReadable
. So IO::WaitReadable
can be used to rescue the exceptions for retrying recvfrom_nonblock.
By specifying a keyword argument exception to false
, you can indicate that recvfrom_nonblock should not raise an IO::WaitReadable
exception, but return the symbol :wait_readable
instead.
See
# File 'ext/socket/lib/socket.rb', line 1277
def recvfrom_nonblock(len, flag = 0, outbuf = nil, exception: true) __recvfrom_nonblock(len, flag, outbuf, exception) end
#send(mesg, flags, host, port) ⇒ numbytes_sent
#send(mesg, flags, sockaddr_to) ⇒ numbytes_sent
#send(mesg, flags) ⇒ numbytes_sent
numbytes_sent
#send(mesg, flags, sockaddr_to) ⇒ numbytes_sent
#send(mesg, flags) ⇒ numbytes_sent
# File 'ext/socket/udpsocket.c', line 202
static VALUE udp_send(int argc, VALUE *argv, VALUE sock) { VALUE flags, host, port; struct udp_send_arg arg; VALUE ret; if (argc == 2 || argc == 3) { return rsock_bsock_send(argc, argv, sock); } rb_scan_args(argc, argv, "4", &arg.sarg.mesg, &flags, &host, &port); StringValue(arg.sarg.mesg); GetOpenFile(sock, arg.fptr); arg.sarg.fd = arg.fptr->fd; arg.sarg.flags = NUM2INT(flags); arg.res = rsock_addrinfo(host, port, rsock_fd_family(arg.fptr->fd), SOCK_DGRAM, 0); ret = rb_ensure(udp_send_internal, (VALUE)&arg, rsock_freeaddrinfo, (VALUE)arg.res); if (!ret) rsock_sys_fail_host_port("sendto(2)", host, port); return ret; }