如何在IO中多态使用Ruby UDPSocket?

时间:2017-10-28 17:17:57

标签: ruby io udp

将多维数据集UDPSocketIO对象一起使用的最佳做法是什么?我正在为我的库编写序列化类,并希望将网络套接字视为与FileStringIO对象相同,而UDPSocket确实从IO继承,但是当我尝试write_nonblock时,套接字会引发Errno::EDESTADDRREQ

2.4.1 :1 > require 'socket'
 => true 
2.4.1 :2 > sock = UDPSocket.new
 => #<UDPSocket:fd 4> 
2.4.1 :3 > sock.bind('127.0.0.1', 0)
 => 0 
2.4.1 :4 > sock.write_nonblock 'nonblock'
Errno::EDESTADDRREQ: Destination address required
        from <internal:prelude>:136:in `__write_nonblock'
        from <internal:prelude>:136:in `write_nonblock'
        from (irb):4
        from /usr/local/rvm/rubies/ruby-2.4.1/bin/irb:11:in `<main>'

1 个答案:

答案 0 :(得分:0)

套接字必须是connect并且是write之前的send,而无需通过2.4.1 :5 > sock.connect('127.0.0.1', 12345) => 0 2.4.1 :6 > sock.write_nonblock 'nonblock' => 8 指定目的地:

{{1}}