Class | BasicSocket |
In: |
ext/socket/socket.c
|
Parent: | IO |
Gets a socket option. These are protocol and system specific, see your local sytem documentation for details. The option is returned as a String with the data being the binary value of the socket option.
Some socket options are integers with boolean values, in this case getsockopt could be called like this:
optval = sock.getsockopt(Socket::SOL_SOCKET,Socket::SO_REUSEADDR) optval = optval.unpack "i" reuseaddr = optval[0] == 0 ? false : true
Some socket options are integers with numeric values, in this case getsockopt could be called like this:
optval = sock.getsockopt(Socket::IPPROTO_IP, Socket::IP_TTL) ipttl = optval.unpack("i")[0]
Option values may be structs. Decoding them can be complex as it involves examining your system headers to determine the correct definition. An example is a +struct linger+, which may be defined in your system headers as:
struct linger { int l_onoff; int l_linger; };
In this case getsockopt could be called like this:
optval = sock.getsockopt(Socket::SOL_SOCKET, Socket::SO_LINGER) onoff, linger = optval.unpack "ii"
Receives up to maxlen bytes from socket using recvfrom(2) after O_NONBLOCK is set for the underlying file descriptor. flags is zero or more of the MSG_ options. The result, mesg, is the data received.
When recvfrom(2) returns 0, Socket#recv_nonblock returns an empty string as data. The meaning depends on the socket: EOF on TCP, empty packet on UDP, etc.
serv = TCPServer.new("127.0.0.1", 0) af, port, host, addr = serv.addr c = TCPSocket.new(addr, port) s = serv.accept c.send "aaa", 0 IO.select([s]) p s.recv_nonblock(10) #=> "aaa"
Refer to Socket#recvfrom for the exceptions that may be thrown if the call to recv_nonblock fails.
BasicSocket#recv_nonblock may raise any error corresponding to recvfrom(2) failure, including Errno::EAGAIN.
Sets a socket option. These are protocol and system specific, see your local sytem documentation for details.
Some socket options are integers with boolean values, in this case setsockopt could be called like this:
sock.setsockopt(Socket::SOL_SOCKET,Socket::SO_REUSEADDR, true)
Some socket options are integers with numeric values, in this case setsockopt could be called like this:
sock.setsockopt(Socket::IPPROTO_IP, Socket::IP_TTL, 255)
Option values may be structs. Passing them can be complex as it involves examining your system headers to determine the correct definition. An example is an ip_mreq, which may be defined in your system headers as:
struct ip_mreq { struct in_addr imr_multiaddr; struct in_addr imr_interface; };
In this case setsockopt could be called like this:
optval = IPAddr.new("224.0.0.251") + Socket::INADDR_ANY sock.setsockopt(Socket::IPPROTO_IP, Socket::IP_ADD_MEMBERSHIP, optval)
ruby-doc.org is hosted and maintained by James Britt and Rising Tide Software, a Ruby application development company in Phoenix, Arizona. The site was created in 2002 as part of the Ruby Documentation Project to promote the Ruby language and to help other Ruby hackers.
Documentation content on ruby-doc.org is provided by remarkable members of the Ruby community.
For more information on the Ruby programming language, visit ruby-lang.org.
Want to help improve Ruby's API docs? See Ruby Documentation Guidelines.