123456789_123456789_123456789_123456789_123456789_

Class: UNIXSocket

Relationships & Source Files
Extension / Inclusion / Inheritance Descendants
Subclasses:
Super Chains via Extension / Inclusion / Inheritance
Class Chain:
Instance Chain:
Inherits: BasicSocket
Defined in: ext/socket/unixsocket.c

Overview

UNIXSocket represents a UNIX domain stream client socket.

Class Attribute Summary

::BasicSocket - Inherited

.do_not_reverse_lookup

Gets the global do_not_reverse_lookup flag.

.do_not_reverse_lookup=

Sets the global do_not_reverse_lookup flag.

Class Method Summary

::BasicSocket - Inherited

.for_fd

Returns a socket object which contains the file descriptor, fd.

Instance Attribute Summary

::BasicSocket - Inherited

#do_not_reverse_lookup

Gets the do_not_reverse_lookup flag of basicsocket.

#do_not_reverse_lookup=

Sets the do_not_reverse_lookup flag of basicsocket.

Instance Method Summary

::BasicSocket - Inherited

#close_read

Disallows further read using shutdown system call.

#close_write

Disallows further write using shutdown system call.

#connect_address

Returns an address of the socket suitable for connect in the local machine.

#getpeereid

Returns the user and group on the peer of the UNIX socket.

#getpeername

Returns the remote address of the socket as a sockaddr string.

#getsockname

Returns the local address of the socket as a sockaddr string.

#getsockopt

Gets a socket option.

#local_address

Returns an ::Addrinfo object for local address obtained by getsockname.

#recv

Receives a message.

#recv_nonblock

Receives up to maxlen bytes from socket using recvfrom(2) after O_NONBLOCK is set for the underlying file descriptor.

#recvmsg

recvmsg receives a message using recvmsg(2) system call in blocking manner.

#recvmsg_nonblock

recvmsg receives a message using recvmsg(2) system call in non-blocking manner.

#remote_address

Returns an ::Addrinfo object for remote address obtained by getpeername.

#send

send mesg via basicsocket.

#sendmsg

sendmsg sends a message using sendmsg(2) system call in blocking manner.

#sendmsg_nonblock

sendmsg_nonblock sends a message using sendmsg(2) system call in non-blocking manner.

#setsockopt

Sets a socket option.

#shutdown

Calls shutdown(2) system call.

#__recvmsg, #__recvmsg_nonblock, #__sendmsg, #__sendmsg_nonblock

Constructor Details

.new(path) ⇒ UNIXSocket

Creates a new UNIX client socket connected to path.

require 'socket'

s = UNIXSocket.new("/tmp/sock")
s.send "hello", 0

Class Method Details

.pair([type [, protocol]]) ⇒ UNIXSocket .socketpair([type [, protocol]]) ⇒ UNIXSocket
Also known as: .socketpair

Creates a pair of sockets connected to each other.

socktype should be a socket type such as: :STREAM, :DGRAM, :RAW, etc.

protocol should be a protocol defined in the domain. 0 is default protocol for the domain.

s1, s2 = UNIXSocket.pair
s1.send "a", 0
s1.send "b", 0
p s2.recv(10) #=> "ab"

.pair([type [, protocol]]) ⇒ UNIXSocket .socketpair([type [, protocol]]) ⇒ UNIXSocket

Alias for .pair.

Instance Method Details

#addrArray, unix_path

Returns the local address as an array which contains address_family and unix_path.

Example

serv = UNIXServer.new("/tmp/sock")
p serv.addr #=> ["AF_UNIX", "/tmp/sock"]

#path ⇒ path

Returns the path of the local address of unixsocket.

s = UNIXServer.new("/tmp/sock")
p s.path #=> "/tmp/sock"

#peeraddrArray, unix_path

Returns the remote address as an array which contains address_family and unix_path.

Example

serv = UNIXServer.new("/tmp/sock")
c = UNIXSocket.new("/tmp/sock")
p c.peeraddr #=> ["AF_UNIX", "/tmp/sock"]

#recv_io([klass [, mode]]) ⇒ IO

Example

UNIXServer.open("/tmp/sock") {|serv|
  UNIXSocket.open("/tmp/sock") {|c|
    s = serv.accept

    c.send_io STDOUT
    stdout = s.recv_io

    p STDOUT.fileno #=> 1
    p stdout.fileno #=> 7

    stdout.puts "hello" # outputs "hello\n" to standard output.
  }
}

klass will determine the class of io returned (using the IO.for_fd singleton method or similar). If klass is nil, an integer file descriptor is returned.

mode is the same as the argument passed to IO.for_fd

#recvfrom(maxlen [, flags[, outbuf]]) ⇒ Array, unixaddress

Receives a message via unixsocket.

maxlen is the maximum number of bytes to receive.

flags should be a bitwise OR of Socket::MSG_* constants.

outbuf will contain only the received data after the method call even if it is not empty at the beginning.

s1 = Socket.new(:UNIX, :DGRAM, 0)
s1_ai = Addrinfo.unix("/tmp/sock1")
s1.bind(s1_ai)

s2 = Socket.new(:UNIX, :DGRAM, 0)
s2_ai = Addrinfo.unix("/tmp/sock2")
s2.bind(s2_ai)
s3 = UNIXSocket.for_fd(s2.fileno)

s1.send "a", 0, s2_ai
p s3.recvfrom(10) #=> ["a", ["AF_UNIX", "/tmp/sock1"]]

#send_io(io) ⇒ nil

Sends io as file descriptor passing.

s1, s2 = UNIXSocket.pair

s1.send_io STDOUT
stdout = s2.recv_io

p STDOUT.fileno #=> 1
p stdout.fileno #=> 6

stdout.puts "hello" # outputs "hello\n" to standard output.

io may be any kind of ::IO object or integer file descriptor.