在线文档教程
Ruby 2.4

UDPSocket

UDPSocket类

Parent:IPSocket

UDPSocket代表一个UDP / IP套接字。

公共类方法

new(address_family) → socket Show source

创建一个新的UDPSocket对象。

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>

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 }

公共实例方法

bind(host, port) #→ 0 Show source

udpsocket绑定到host:port

u1 = UDPSocket.new u1.bind("127.0.0.1", 4913) u1.send "message-to-self", 0, "127.0.0.1", 4913 p u1.recvfrom(10) #=> ["message-to", ["AF_INET", 4913, "localhost", "127.0.0.1"]]

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 Show source

Connects udpsocket to host:port.

这使得可以发送没有目的地地址。

u1 = UDPSocket.new u1.bind("127.0.0.1", 4913) u2 = UDPSocket.new u2.connect("127.0.0.1", 4913) u2.send "uuuu", 0 p u1.recvfrom(10) #=> ["uuuu", ["AF_INET", 33230, "localhost", "127.0.0.1"]]

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]]) → mesg, sender_inet_addr()

在为基础文件描述符设置O_NONBLOCK后,使用recvfrom(2)从udpsocket接收maxlen字节。 标志是零个或多个MSG_选项。 结果的第一个元素mesg是收到的数据。 第二个元素sender_inet_addr是一个表示发件人地址的数组。

当recvfrom(2)返回0时,Socket#recvfrom_nonblock返回一个空字符串作为数据。它意味着一个空包。

参数

  • maxlen - 从套接字接收的字节数

  • flags- 零个或多个MSG_选项

  • outbuf - 目标字符串缓冲区

  • options - 关键字散列,支持 exception: false

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

如果调用recvfrom_nonblock失败,可以引发异常,请参考Socket#recvfrom 。

#recvfrom_nonblock可能引发对应于recvfrom(2)失败的错误,包括Errno :: EWOULDBLOCK。

如果该异常是Errno :: EWOULDBLOCK或Errno :: EAGAIN,则它由IO :: WaitReadable扩展。所以IO :: WaitReadable可以用来解救重试recvfrom_nonblock的异常。

通过指定关键字参数异常false,你可以指出#recvfrom_nonblock应该不会引发IO :: WaitReadable异常,但返回的符号:wait_readable来代替。

参阅

  • Socket#recvfrom

# File ext/socket/lib/socket.rb, line 1254 def recvfrom_nonblock(len, flag = 0, outbuf = nil, exception: true) __recvfrom_nonblock(len, flag, outbuf, exception) end

send(mesg, flags, host, port) → numbytes_sent Show source

send(mesg, flags, sockaddr_to) → numbytes_sent

send(mesg, flags) → numbytes_sent

通过udpsocket 发送mesg

flags 应该是Socket :: MSG_ *常量的按位或。

u1 = UDPSocket.new u1.bind("127.0.0.1", 4913) u2 = UDPSocket.new u2.send "hi", 0, "127.0.0.1", 4913 mesg, addr = u1.recvfrom(10) u1.send mesg, 0, addr[3], addr[1] p u2.recv(100) #=> "hi"

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; }