123456789_123456789_123456789_123456789_123456789_

Class: UNIXServer

Relationships & Source Files
Super Chains via Extension / Inclusion / Inheritance
Class Chain:
Instance Chain:
Inherits: UNIXSocket
Defined in: ext/socket/unixserver.c,
ext/socket/socket.c,
ext/socket/lib/socket.rb

Overview

UNIXServer represents a UNIX domain stream server 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

::UNIXSocket - Inherited

.new

Creates a new UNIX client socket connected to path.

.pair

Creates a pair of sockets connected to each other.

.socketpair

Alias for UNIXSocket.pair.

::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

::UNIXSocket - Inherited

#addr

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

#path

Returns the path of the local address of unixsocket.

#peeraddr

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

#recv_io

Example.

#recvfrom

Receives a message via unixsocket.

#send_io

Sends io as file descriptor passing.

::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, #read_nonblock, #write_nonblock, #__read_nonblock, #__recv_nonblock, #__write_nonblock

Constructor Details

.new(path) ⇒ UNIXServer

Creates a new UNIX server socket bound to path.

require 'socket'

serv = UNIXServer.new("/tmp/sock")
s = serv.accept
p s.read
[ GitHub ]

  
# File 'ext/socket/unixserver.c', line 26

static VALUE
unix_svr_init(VALUE sock, VALUE path)
{
    return rsock_init_unixsock(sock, path, 1);
}

Instance Method Details

#__accept_nonblock(ex) (private)

This method is for internal use only.
[ GitHub ]

  
# File 'ext/socket/unixserver.c', line 63

static VALUE
unix_accept_nonblock(VALUE sock, VALUE ex)
{
    rb_io_t *fptr;
    struct sockaddr_un from;
    socklen_t fromlen;

    GetOpenFile(sock, fptr);
    fromlen = (socklen_t)sizeof(from);
    return rsock_s_accept_nonblock(rb_cUNIXSocket, ex, fptr,
			           (struct sockaddr *)&from, &fromlen);
}

#acceptunixsocket

Accepts an incoming connection. It returns a new ::UNIXSocket object.

UNIXServer.open("/tmp/sock") {|serv|
  UNIXSocket.open("/tmp/sock") {|c|
    s = serv.accept
    s.puts "hi"
    s.close
    p c.read #=> "hi\n"
  }
}
[ GitHub ]

  
# File 'ext/socket/unixserver.c', line 49

static VALUE
unix_accept(VALUE sock)
{
    rb_io_t *fptr;
    struct sockaddr_un from;
    socklen_t fromlen;

    GetOpenFile(sock, fptr);
    fromlen = (socklen_t)sizeof(struct sockaddr_un);
    return rsock_s_accept(rb_cUNIXSocket, fptr->fd,
		          (struct sockaddr*)&from, &fromlen);
}

#accept_nonblock([options]) ⇒ unixsocket

Accepts an incoming connection using accept(2) after O_NONBLOCK is set for the underlying file descriptor. It returns an accepted ::UNIXSocket for the incoming connection.

Example

require 'socket'
serv = UNIXServer.new("/tmp/sock")
begin # emulate blocking accept
  sock = serv.accept_nonblock
rescue IO::WaitReadable, Errno::EINTR
  IO.select([serv])
  retry
end
# sock is an accepted socket.

Refer to Socket#accept for the exceptions that may be thrown if the call to accept_nonblock fails.

accept_nonblock may raise any error corresponding to accept(2) failure, including Errno::EWOULDBLOCK.

If the exception is Errno::EWOULDBLOCK, Errno::EAGAIN, Errno::ECONNABORTED or Errno::EPROTO, it is extended by IO::WaitReadable. So IO::WaitReadable can be used to rescue the exceptions for retrying accept_nonblock.

By specifying a keyword argument exception to false, you can indicate that accept_nonblock should not raise an IO::WaitReadable exception, but return the symbol :wait_readable instead.

See

[ GitHub ]

  
# File 'ext/socket/lib/socket.rb', line 1353

def accept_nonblock(exception: true)
  __accept_nonblock(exception)
end

#listen(int) ⇒ 0

Alias for TCPServer#listen. Listens for connections, using the specified int as the backlog. A call to listen only applies if the socket is of type SOCK_STREAM or SOCK_SEQPACKET.

Parameter

  • backlog - the maximum length of the queue for pending connections.

Example 1

require 'socket'
include Socket::Constants
socket = Socket.new( AF_INET, SOCK_STREAM, 0 )
sockaddr = Socket.pack_sockaddr_in( 2200, 'localhost' )
socket.bind( sockaddr )
socket.listen( 5 )

Example 2 (listening on an arbitrary port, unix-based systems only):

require 'socket'
include Socket::Constants
socket = Socket.new( AF_INET, SOCK_STREAM, 0 )
socket.listen( 1 )

Unix-based Exceptions

On unix based systems the above will work because a new sockaddr struct is created on the address ADDR_ANY, for an arbitrary port number as handed off by the kernel. It will not work on Windows, because Windows requires that the socket is bound by calling bind before it can listen.

If the backlog amount exceeds the implementation-dependent maximum queue length, the implementation's maximum queue length will be used.

On unix-based based systems the following system exceptions may be raised if the call to listen fails:

  • Errno::EBADF - the socket argument is not a valid file descriptor

  • Errno::EDESTADDRREQ - the socket is not bound to a local address, and the protocol does not support listening on an unbound socket

  • Errno::EINVAL - the socket is already connected

  • Errno::ENOTSOCK - the socket argument does not refer to a socket

  • Errno::EOPNOTSUPP - the socket protocol does not support listen

  • Errno::EACCES - the calling process does not have appropriate privileges

  • Errno::EINVAL - the socket has been shut down

  • Errno::ENOBUFS - insufficient resources are available in the system to complete the call

Windows Exceptions

On Windows systems the following system exceptions may be raised if the call to listen fails:

  • Errno::ENETDOWN - the network is down

  • Errno::EADDRINUSE - the socket's local address is already in use. This usually occurs during the execution of bind but could be delayed if the call to bind was to a partially wildcard address (involving ADDR_ANY) and if a specific address needs to be committed at the time of the call to listen

  • Errno::EINPROGRESS - a Windows Sockets 1.1 call is in progress or the service provider is still processing a callback function

  • Errno::EINVAL - the socket has not been bound with a call to bind.

  • Errno::EISCONN - the socket is already connected

  • Errno::EMFILE - no more socket descriptors are available

  • Errno::ENOBUFS - no buffer space is available

  • Errno::ENOTSOC - socket is not a socket

  • Errno::EOPNOTSUPP - the referenced socket is not a type that supports the listen method

See

  • listen manual pages on unix-based systems

  • listen function in Microsoft's Winsock functions reference

#sysacceptfile_descriptor

Accepts a new connection. It returns the new file descriptor which is an integer.

UNIXServer.open("/tmp/sock") {|serv|
  UNIXSocket.open("/tmp/sock") {|c|
    fd = serv.sysaccept
    s = IO.new(fd)
    s.puts "hi"
    s.close
    p c.read #=> "hi\n"
  }
}
[ GitHub ]

  
# File 'ext/socket/unixserver.c', line 94

static VALUE
unix_sysaccept(VALUE sock)
{
    rb_io_t *fptr;
    struct sockaddr_un from;
    socklen_t fromlen;

    GetOpenFile(sock, fptr);
    fromlen = (socklen_t)sizeof(struct sockaddr_un);
    return rsock_s_accept(0, fptr->fd, (struct sockaddr*)&from, &fromlen);
}