Where are MSG_ options defined for ruby sockets?
In the documentation for Ruby class Socket::recv, there is a mention 开发者_开发问答of a second option parameter "flag" which is said to be zero or more of MSG_ options.
I checked a few different sites and wasn't able to find what the MSG_ choices are. Can anyone point me to the documentation for these flags?
They have the same names as the corresponding #define
s in the C BSD socket stack, except for the Socket::
in front. (And for the record, to answer the exact question you asked, I should say "in ext/socket/socket.c
" in the Ruby source tree.) So:
>> require 'socket'
=> true
>> Socket::MSG_PEEK
=> 2
You can probably see this by typing man 2 recv
, though you may need to install a man pages package first. There are also online man pages, see perhaps: man 2 recv here.
For now, here is what you need, these are the Posix options taken from the NetBSD man page. There are a lot more available on Linux. When running on Linux, Ruby will define the additional symbols, otherwise they may be undefined depending on the host. (Thanks, mark4o.)
The flags argument to a recv call is formed by or'ing one or more of the
values:
MSG_OOB process out-of-band data
MSG_PEEK peek at incoming message
MSG_WAITALL wait for full request or error
The MSG_OOB flag requests receipt of out-of-band data that would not be
received in the normal data stream. Some protocols place expedited data
at the head of the normal data queue, and thus this flag cannot be used
with such protocols. The MSG_PEEK flag causes the receive operation to
return data from the beginning of the receive queue without removing that
data from the queue. Thus, a subsequent receive call will return the
same data. The MSG_WAITALL flag requests that the operation block until
the full request is satisfied. However, the call may still return less
data than requested if a signal is caught, an error or disconnect occurs,
or the next data to be received is of a different type than that
returned.
I got stuck with this thing couple of months ago. First, you are using the 'Socket' class, right? Because that's what I was using.
I don't know for sure, but it depends upon the protocol you are creating with the class 'Socket'.
Like, if you create the socket as a TCP socket:
sock = Socket.open(Socket::PF_INET, Socket::SOCK_RAW, Socket::IPPROTO_RAW)
the flags will be the six flags of the TCP protocol (URG, ACK, PSH, etc.).
I'm not sure of that, but but the 0 is working for me perfectly.
精彩评论