将多维数据集UDPSocket
与IO
对象一起使用的最佳做法是什么?我正在为我的库编写序列化类,并希望将网络套接字视为与File
或StringIO
对象相同,而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>'
答案 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}}