HEX
Server: Apache
System: Windows NT MAGNETO-ARM 10.0 build 22000 (Windows 10) AMD64
User: Michel (0)
PHP: 7.4.7
Disabled: NONE
Upload Files
File: C:/Ruby27-x64/share/doc/ruby/html/UNIXSocket.html
<!DOCTYPE html>

<html>
<head>
<meta charset="UTF-8">

<title>class UNIXSocket - RDoc Documentation</title>

<script type="text/javascript">
  var rdoc_rel_prefix = "./";
  var index_rel_prefix = "./";
</script>

<script src="./js/navigation.js" defer></script>
<script src="./js/search.js" defer></script>
<script src="./js/search_index.js" defer></script>
<script src="./js/searcher.js" defer></script>
<script src="./js/darkfish.js" defer></script>

<link href="./css/fonts.css" rel="stylesheet">
<link href="./css/rdoc.css" rel="stylesheet">




<body id="top" role="document" class="class">
<nav role="navigation">
  <div id="project-navigation">
    <div id="home-section" role="region" title="Quick navigation" class="nav-section">
  <h2>
    <a href="./index.html" rel="home">Home</a>
  </h2>

  <div id="table-of-contents-navigation">
    <a href="./table_of_contents.html#pages">Pages</a>
    <a href="./table_of_contents.html#classes">Classes</a>
    <a href="./table_of_contents.html#methods">Methods</a>
  </div>
</div>

    <div id="search-section" role="search" class="project-section initially-hidden">
  <form action="#" method="get" accept-charset="utf-8">
    <div id="search-field-wrapper">
      <input id="search-field" role="combobox" aria-label="Search"
             aria-autocomplete="list" aria-controls="search-results"
             type="text" name="search" placeholder="Search" spellcheck="false"
             title="Type to search, Up and Down to navigate, Enter to load">
    </div>

    <ul id="search-results" aria-label="Search Results"
        aria-busy="false" aria-expanded="false"
        aria-atomic="false" class="initially-hidden"></ul>
  </form>
</div>

  </div>

  

  <div id="class-metadata">
    
    <div id="parent-class-section" class="nav-section">
  <h3>Parent</h3>

  
  <p class="link"><a href="BasicSocket.html">BasicSocket</a>
  
</div>

    
    
    <!-- Method Quickref -->
<div id="method-list-section" class="nav-section">
  <h3>Methods</h3>

  <ul class="link-list" role="directory">
    
    <li ><a href="#method-c-new">::new</a>
    
    <li ><a href="#method-c-pair">::pair</a>
    
    <li ><a href="#method-c-socketpair">::socketpair</a>
    
    <li ><a href="#method-i-addr">#addr</a>
    
    <li ><a href="#method-i-path">#path</a>
    
    <li ><a href="#method-i-peeraddr">#peeraddr</a>
    
    <li ><a href="#method-i-recv_io">#recv_io</a>
    
    <li ><a href="#method-i-recvfrom">#recvfrom</a>
    
    <li ><a href="#method-i-send_io">#send_io</a>
    
  </ul>
</div>

  </div>
</nav>

<main role="main" aria-labelledby="class-UNIXSocket">
  <h1 id="class-UNIXSocket" class="class">
    class UNIXSocket
  </h1>

  <section class="description">
    
<p><a href="UNIXSocket.html"><code>UNIXSocket</code></a> represents a UNIX domain stream client socket.</p>

  </section>

  
  <section id="5Buntitled-5D" class="documentation-section">
    

    

    

    

    
     <section id="public-class-5Buntitled-5D-method-details" class="method-section">
       <header>
         <h3>Public Class Methods</h3>
       </header>

    
      <div id="method-c-new" class="method-detail ">
        
        
        <div class="method-heading">
          <span class="method-callseq">
            new(path) &rarr; unixsocket
          </span>
          
          <span class="method-click-advice">click to toggle source</span>
          
        </div>
        
        

        <div class="method-description">
          
          <p>Creates a new UNIX client socket connected to <em>path</em>.</p>

<pre class="ruby"><span class="ruby-identifier">require</span> <span class="ruby-string">&#39;socket&#39;</span>

<span class="ruby-identifier">s</span> = <span class="ruby-constant">UNIXSocket</span>.<span class="ruby-identifier">new</span>(<span class="ruby-string">&quot;/tmp/sock&quot;</span>)
<span class="ruby-identifier">s</span>.<span class="ruby-identifier">send</span> <span class="ruby-string">&quot;hello&quot;</span>, <span class="ruby-value">0</span>
</pre>
          
          

          
          <div class="method-source-code" id="new-source">
            <pre>static VALUE
unix_init(VALUE sock, VALUE path)
{
    return rsock_init_unixsock(sock, path, 0);
}</pre>
          </div>
          
        </div>

        

        
      </div>

    
      <div id="method-c-pair" class="method-detail ">
        
        
        <div class="method-heading">
          <span class="method-callseq">
            pair([type [, protocol]])       &rarr; [unixsocket1, unixsocket2]
          </span>
          
          <span class="method-click-advice">click to toggle source</span>
          
        </div>
        
        

        <div class="method-description">
          
          <p>Creates a pair of sockets connected to each other.</p>

<p><em>socktype</em> should be a socket type such as: :STREAM, :DGRAM, :RAW, etc.</p>

<p><em>protocol</em> should be a protocol defined in the domain. 0 is default protocol for the domain.</p>

<pre class="ruby"><span class="ruby-identifier">s1</span>, <span class="ruby-identifier">s2</span> = <span class="ruby-constant">UNIXSocket</span>.<span class="ruby-identifier">pair</span>
<span class="ruby-identifier">s1</span>.<span class="ruby-identifier">send</span> <span class="ruby-string">&quot;a&quot;</span>, <span class="ruby-value">0</span>
<span class="ruby-identifier">s1</span>.<span class="ruby-identifier">send</span> <span class="ruby-string">&quot;b&quot;</span>, <span class="ruby-value">0</span>
<span class="ruby-identifier">p</span> <span class="ruby-identifier">s2</span>.<span class="ruby-identifier">recv</span>(<span class="ruby-value">10</span>) <span class="ruby-comment">#=&gt; &quot;ab&quot;</span>
</pre>
          
          

          
          <div class="method-source-code" id="pair-source">
            <pre>static VALUE
unix_s_socketpair(int argc, VALUE *argv, VALUE klass)
{
    VALUE domain, type, protocol;
    VALUE args[3];

    domain = INT2FIX(PF_UNIX);
    rb_scan_args(argc, argv, &quot;02&quot;, &amp;type, &amp;protocol);
    if (argc == 0)
        type = INT2FIX(SOCK_STREAM);
    if (argc &lt;= 1)
        protocol = INT2FIX(0);

    args[0] = domain;
    args[1] = type;
    args[2] = protocol;

    return rsock_sock_s_socketpair(3, args, klass);
}</pre>
          </div>
          
        </div>

        

        
      </div>

    
      <div id="method-c-socketpair" class="method-detail ">
        
        
        <div class="method-heading">
          <span class="method-callseq">
            socketpair([type [, protocol]]) &rarr; [unixsocket1, unixsocket2]
          </span>
          
          <span class="method-click-advice">click to toggle source</span>
          
        </div>
        
        

        <div class="method-description">
          
          <p>Creates a pair of sockets connected to each other.</p>

<p><em>socktype</em> should be a socket type such as: :STREAM, :DGRAM, :RAW, etc.</p>

<p><em>protocol</em> should be a protocol defined in the domain. 0 is default protocol for the domain.</p>

<pre class="ruby"><span class="ruby-identifier">s1</span>, <span class="ruby-identifier">s2</span> = <span class="ruby-constant">UNIXSocket</span>.<span class="ruby-identifier">pair</span>
<span class="ruby-identifier">s1</span>.<span class="ruby-identifier">send</span> <span class="ruby-string">&quot;a&quot;</span>, <span class="ruby-value">0</span>
<span class="ruby-identifier">s1</span>.<span class="ruby-identifier">send</span> <span class="ruby-string">&quot;b&quot;</span>, <span class="ruby-value">0</span>
<span class="ruby-identifier">p</span> <span class="ruby-identifier">s2</span>.<span class="ruby-identifier">recv</span>(<span class="ruby-value">10</span>) <span class="ruby-comment">#=&gt; &quot;ab&quot;</span>
</pre>
          
          

          
          <div class="method-source-code" id="socketpair-source">
            <pre>static VALUE
unix_s_socketpair(int argc, VALUE *argv, VALUE klass)
{
    VALUE domain, type, protocol;
    VALUE args[3];

    domain = INT2FIX(PF_UNIX);
    rb_scan_args(argc, argv, &quot;02&quot;, &amp;type, &amp;protocol);
    if (argc == 0)
        type = INT2FIX(SOCK_STREAM);
    if (argc &lt;= 1)
        protocol = INT2FIX(0);

    args[0] = domain;
    args[1] = type;
    args[2] = protocol;

    return rsock_sock_s_socketpair(3, args, klass);
}</pre>
          </div>
          
        </div>

        

        
      </div>

    
    </section>
  
     <section id="public-instance-5Buntitled-5D-method-details" class="method-section">
       <header>
         <h3>Public Instance Methods</h3>
       </header>

    
      <div id="method-i-addr" class="method-detail ">
        
        
        <div class="method-heading">
          <span class="method-callseq">
            addr &rarr; [address_family, unix_path]
          </span>
          
          <span class="method-click-advice">click to toggle source</span>
          
        </div>
        
        

        <div class="method-description">
          
          <p>Returns the local address as an array which contains address_family and unix_path.</p>

<p>Example</p>

<pre class="ruby"><span class="ruby-identifier">serv</span> = <span class="ruby-constant">UNIXServer</span>.<span class="ruby-identifier">new</span>(<span class="ruby-string">&quot;/tmp/sock&quot;</span>)
<span class="ruby-identifier">p</span> <span class="ruby-identifier">serv</span>.<span class="ruby-identifier">addr</span> <span class="ruby-comment">#=&gt; [&quot;AF_UNIX&quot;, &quot;/tmp/sock&quot;]</span>
</pre>
          
          

          
          <div class="method-source-code" id="addr-source">
            <pre>static VALUE
unix_addr(VALUE sock)
{
    rb_io_t *fptr;
    struct sockaddr_un addr;
    socklen_t len = (socklen_t)sizeof addr;
    socklen_t len0 = len;

    GetOpenFile(sock, fptr);

    if (getsockname(fptr-&gt;fd, (struct sockaddr*)&amp;addr, &amp;len) &lt; 0)
        rsock_sys_fail_path(&quot;getsockname(2)&quot;, fptr-&gt;pathv);
    if (len0 &lt; len) len = len0;
    return rsock_unixaddr(&amp;addr, len);
}</pre>
          </div>
          
        </div>

        

        
      </div>

    
      <div id="method-i-path" class="method-detail ">
        
        
        <div class="method-heading">
          <span class="method-callseq">
            path &rarr; path
          </span>
          
          <span class="method-click-advice">click to toggle source</span>
          
        </div>
        
        

        <div class="method-description">
          
          <p>Returns the path of the local address of unixsocket.</p>

<pre class="ruby"><span class="ruby-identifier">s</span> = <span class="ruby-constant">UNIXServer</span>.<span class="ruby-identifier">new</span>(<span class="ruby-string">&quot;/tmp/sock&quot;</span>)
<span class="ruby-identifier">p</span> <span class="ruby-identifier">s</span>.<span class="ruby-identifier">path</span> <span class="ruby-comment">#=&gt; &quot;/tmp/sock&quot;</span>
</pre>
          
          

          
          <div class="method-source-code" id="path-source">
            <pre>static VALUE
unix_path(VALUE sock)
{
    rb_io_t *fptr;

    GetOpenFile(sock, fptr);
    if (NIL_P(fptr-&gt;pathv)) {
        struct sockaddr_un addr;
        socklen_t len = (socklen_t)sizeof(addr);
        socklen_t len0 = len;
        if (getsockname(fptr-&gt;fd, (struct sockaddr*)&amp;addr, &amp;len) &lt; 0)
            rsock_sys_fail_path(&quot;getsockname(2)&quot;, fptr-&gt;pathv);
        if (len0 &lt; len) len = len0;
        fptr-&gt;pathv = rb_obj_freeze(rsock_unixpath_str(&amp;addr, len));
    }
    return rb_str_dup(fptr-&gt;pathv);
}</pre>
          </div>
          
        </div>

        

        
      </div>

    
      <div id="method-i-peeraddr" class="method-detail ">
        
        
        <div class="method-heading">
          <span class="method-callseq">
            peeraddr &rarr; [address_family, unix_path]
          </span>
          
          <span class="method-click-advice">click to toggle source</span>
          
        </div>
        
        

        <div class="method-description">
          
          <p>Returns the remote address as an array which contains address_family and unix_path.</p>

<p>Example</p>

<pre class="ruby"><span class="ruby-identifier">serv</span> = <span class="ruby-constant">UNIXServer</span>.<span class="ruby-identifier">new</span>(<span class="ruby-string">&quot;/tmp/sock&quot;</span>)
<span class="ruby-identifier">c</span> = <span class="ruby-constant">UNIXSocket</span>.<span class="ruby-identifier">new</span>(<span class="ruby-string">&quot;/tmp/sock&quot;</span>)
<span class="ruby-identifier">p</span> <span class="ruby-identifier">c</span>.<span class="ruby-identifier">peeraddr</span> <span class="ruby-comment">#=&gt; [&quot;AF_UNIX&quot;, &quot;/tmp/sock&quot;]</span>
</pre>
          
          

          
          <div class="method-source-code" id="peeraddr-source">
            <pre>static VALUE
unix_peeraddr(VALUE sock)
{
    rb_io_t *fptr;
    struct sockaddr_un addr;
    socklen_t len = (socklen_t)sizeof addr;
    socklen_t len0 = len;

    GetOpenFile(sock, fptr);

    if (getpeername(fptr-&gt;fd, (struct sockaddr*)&amp;addr, &amp;len) &lt; 0)
        rsock_sys_fail_path(&quot;getpeername(2)&quot;, fptr-&gt;pathv);
    if (len0 &lt; len) len = len0;
    return rsock_unixaddr(&amp;addr, len);
}</pre>
          </div>
          
        </div>

        

        
      </div>

    
      <div id="method-i-recv_io" class="method-detail ">
        
        
        <div class="method-heading">
          <span class="method-callseq">
            recv_io([klass [, mode]]) &rarr; io
          </span>
          
          <span class="method-click-advice">click to toggle source</span>
          
        </div>
        
        

        <div class="method-description">
          
          <p>Example</p>

<pre class="ruby"><span class="ruby-constant">UNIXServer</span>.<span class="ruby-identifier">open</span>(<span class="ruby-string">&quot;/tmp/sock&quot;</span>) {<span class="ruby-operator">|</span><span class="ruby-identifier">serv</span><span class="ruby-operator">|</span>
  <span class="ruby-constant">UNIXSocket</span>.<span class="ruby-identifier">open</span>(<span class="ruby-string">&quot;/tmp/sock&quot;</span>) {<span class="ruby-operator">|</span><span class="ruby-identifier">c</span><span class="ruby-operator">|</span>
    <span class="ruby-identifier">s</span> = <span class="ruby-identifier">serv</span>.<span class="ruby-identifier">accept</span>

    <span class="ruby-identifier">c</span>.<span class="ruby-identifier">send_io</span> <span class="ruby-constant">STDOUT</span>
    <span class="ruby-identifier">stdout</span> = <span class="ruby-identifier">s</span>.<span class="ruby-identifier">recv_io</span>

    <span class="ruby-identifier">p</span> <span class="ruby-constant">STDOUT</span>.<span class="ruby-identifier">fileno</span> <span class="ruby-comment">#=&gt; 1</span>
    <span class="ruby-identifier">p</span> <span class="ruby-identifier">stdout</span>.<span class="ruby-identifier">fileno</span> <span class="ruby-comment">#=&gt; 7</span>

    <span class="ruby-identifier">stdout</span>.<span class="ruby-identifier">puts</span> <span class="ruby-string">&quot;hello&quot;</span> <span class="ruby-comment"># outputs &quot;hello\n&quot; to standard output.</span>
  }
}
</pre>

<p><em>klass</em> will determine the class of <em>io</em> returned (using the <a href="IO.html#method-c-for_fd"><code>IO.for_fd</code></a> singleton method or similar). If <em>klass</em> is <code>nil</code>, an integer file descriptor is returned.</p>

<p><em>mode</em> is the same as the argument passed to <a href="IO.html#method-c-for_fd"><code>IO.for_fd</code></a></p>
          
          

          
          <div class="method-source-code" id="recv_io-source">
            <pre>static VALUE
unix_recv_io(int argc, VALUE *argv, VALUE sock)
{
    VALUE klass, mode;
    rb_io_t *fptr;
    struct iomsg_arg arg;
    struct iovec vec[2];
    char buf[1];
    unsigned int gc_reason = 0;
    enum {
        GC_REASON_EMSGSIZE = 0x1,
        GC_REASON_TRUNCATE = 0x2,
        GC_REASON_ENOMEM = 0x4
    };

    int fd;
#if FD_PASSING_BY_MSG_CONTROL
    union {
        struct cmsghdr hdr;
        char pad[sizeof(struct cmsghdr)+8+sizeof(int)+8];
    } cmsg;
#endif

    rb_scan_args(argc, argv, &quot;02&quot;, &amp;klass, &amp;mode);
    if (argc == 0)
        klass = rb_cIO;
    if (argc &lt;= 1)
        mode = Qnil;

retry:
    GetOpenFile(sock, fptr);

    arg.msg.msg_name = NULL;
    arg.msg.msg_namelen = 0;

    vec[0].iov_base = buf;
    vec[0].iov_len = sizeof(buf);
    arg.msg.msg_iov = vec;
    arg.msg.msg_iovlen = 1;

#if FD_PASSING_BY_MSG_CONTROL
    arg.msg.msg_control = (caddr_t)&amp;cmsg;
    arg.msg.msg_controllen = (socklen_t)CMSG_SPACE(sizeof(int));
    arg.msg.msg_flags = 0;
    cmsg.hdr.cmsg_len = (socklen_t)CMSG_LEN(sizeof(int));
    cmsg.hdr.cmsg_level = SOL_SOCKET;
    cmsg.hdr.cmsg_type = SCM_RIGHTS;
    fd = -1;
    memcpy(CMSG_DATA(&amp;cmsg.hdr), &amp;fd, sizeof(int));
#else
    arg.msg.msg_accrights = (caddr_t)&amp;fd;
    arg.msg.msg_accrightslen = sizeof(fd);
    fd = -1;
#endif

    arg.fd = fptr-&gt;fd;
    while ((int)BLOCKING_REGION_FD(recvmsg_blocking, &amp;arg) == -1) {
        int e = errno;
        if (e == EMSGSIZE &amp;&amp; !(gc_reason &amp; GC_REASON_EMSGSIZE)) {
            /* FreeBSD gets here when we&#39;re out of FDs */
            gc_reason |= GC_REASON_EMSGSIZE;
            rb_gc_for_fd(EMFILE);
            goto retry;
        }
        else if (e == ENOMEM &amp;&amp; !(gc_reason &amp; GC_REASON_ENOMEM)) {
            /* ENOMEM is documented in recvmsg manpages */
            gc_reason |= GC_REASON_ENOMEM;
            rb_gc_for_fd(e);
            goto retry;
        }
        if (!rb_io_wait_readable(arg.fd))
            rsock_syserr_fail_path(e, &quot;recvmsg(2)&quot;, fptr-&gt;pathv);
    }

#if FD_PASSING_BY_MSG_CONTROL
    if (arg.msg.msg_controllen &lt; (socklen_t)sizeof(struct cmsghdr)) {
        /* FreeBSD and Linux both get here when we&#39;re out of FDs */
        if (!(gc_reason &amp; GC_REASON_TRUNCATE)) {
            gc_reason |= GC_REASON_TRUNCATE;
            rb_gc_for_fd(EMFILE);
            goto retry;
        }
        rb_raise(rb_eSocket,
                 &quot;file descriptor was not passed (msg_controllen=%d smaller than sizeof(struct cmsghdr)=%d)&quot;,
                 (int)arg.msg.msg_controllen, (int)sizeof(struct cmsghdr));
    }
    if (cmsg.hdr.cmsg_level != SOL_SOCKET) {
        rb_raise(rb_eSocket,
                 &quot;file descriptor was not passed (cmsg_level=%d, %d expected)&quot;,
                 cmsg.hdr.cmsg_level, SOL_SOCKET);
    }
    if (cmsg.hdr.cmsg_type != SCM_RIGHTS) {
        rb_raise(rb_eSocket,
                 &quot;file descriptor was not passed (cmsg_type=%d, %d expected)&quot;,
                 cmsg.hdr.cmsg_type, SCM_RIGHTS);
    }
    if (arg.msg.msg_controllen &lt; (socklen_t)CMSG_LEN(sizeof(int))) {
        rb_raise(rb_eSocket,
                 &quot;file descriptor was not passed (msg_controllen=%d smaller than CMSG_LEN(sizeof(int))=%d)&quot;,
                 (int)arg.msg.msg_controllen, (int)CMSG_LEN(sizeof(int)));
    }
    if ((socklen_t)CMSG_SPACE(sizeof(int)) &lt; arg.msg.msg_controllen) {
        rb_raise(rb_eSocket,
                 &quot;file descriptor was not passed (msg_controllen=%d bigger than CMSG_SPACE(sizeof(int))=%d)&quot;,
                 (int)arg.msg.msg_controllen, (int)CMSG_SPACE(sizeof(int)));
    }
    if (cmsg.hdr.cmsg_len != CMSG_LEN(sizeof(int))) {
        rsock_discard_cmsg_resource(&amp;arg.msg, 0);
        rb_raise(rb_eSocket,
                 &quot;file descriptor was not passed (cmsg_len=%d, %d expected)&quot;,
                 (int)cmsg.hdr.cmsg_len, (int)CMSG_LEN(sizeof(int)));
    }
#else
    if (arg.msg.msg_accrightslen != sizeof(fd)) {
        rb_raise(rb_eSocket,
                 &quot;file descriptor was not passed (accrightslen=%d, %d expected)&quot;,
                 arg.msg.msg_accrightslen, (int)sizeof(fd));
    }
#endif

#if FD_PASSING_BY_MSG_CONTROL
    memcpy(&amp;fd, CMSG_DATA(&amp;cmsg.hdr), sizeof(int));
#endif

    rb_update_max_fd(fd);

    if (rsock_cmsg_cloexec_state &lt; 0)
        rsock_cmsg_cloexec_state = rsock_detect_cloexec(fd);
    if (rsock_cmsg_cloexec_state == 0 || fd &lt;= 2)
        rb_maygvl_fd_fix_cloexec(fd);

    if (klass == Qnil)
        return INT2FIX(fd);
    else {
        ID for_fd;
        int ff_argc;
        VALUE ff_argv[2];
        CONST_ID(for_fd, &quot;for_fd&quot;);
        ff_argc = mode == Qnil ? 1 : 2;
        ff_argv[0] = INT2FIX(fd);
        ff_argv[1] = mode;
        return rb_funcallv(klass, for_fd, ff_argc, ff_argv);
    }
}</pre>
          </div>
          
        </div>

        

        
      </div>

    
      <div id="method-i-recvfrom" class="method-detail ">
        
        
        <div class="method-heading">
          <span class="method-callseq">
            recvfrom(maxlen [, flags[, outbuf]]) &rarr; [mesg, unixaddress]
          </span>
          
          <span class="method-click-advice">click to toggle source</span>
          
        </div>
        
        

        <div class="method-description">
          
          <p>Receives a message via <em>unixsocket</em>.</p>

<p><em>maxlen</em> is the maximum number of bytes to receive.</p>

<p><em>flags</em> should be a bitwise OR of Socket::MSG_* constants.</p>

<p><em>outbuf</em> will contain only the received data after the method call even if it is not empty at the beginning.</p>

<pre class="ruby"><span class="ruby-identifier">s1</span> = <span class="ruby-constant">Socket</span>.<span class="ruby-identifier">new</span>(<span class="ruby-value">:UNIX</span>, <span class="ruby-value">:DGRAM</span>, <span class="ruby-value">0</span>)
<span class="ruby-identifier">s1_ai</span> = <span class="ruby-constant">Addrinfo</span>.<span class="ruby-identifier">unix</span>(<span class="ruby-string">&quot;/tmp/sock1&quot;</span>)
<span class="ruby-identifier">s1</span>.<span class="ruby-identifier">bind</span>(<span class="ruby-identifier">s1_ai</span>)

<span class="ruby-identifier">s2</span> = <span class="ruby-constant">Socket</span>.<span class="ruby-identifier">new</span>(<span class="ruby-value">:UNIX</span>, <span class="ruby-value">:DGRAM</span>, <span class="ruby-value">0</span>)
<span class="ruby-identifier">s2_ai</span> = <span class="ruby-constant">Addrinfo</span>.<span class="ruby-identifier">unix</span>(<span class="ruby-string">&quot;/tmp/sock2&quot;</span>)
<span class="ruby-identifier">s2</span>.<span class="ruby-identifier">bind</span>(<span class="ruby-identifier">s2_ai</span>)
<span class="ruby-identifier">s3</span> = <span class="ruby-constant">UNIXSocket</span>.<span class="ruby-identifier">for_fd</span>(<span class="ruby-identifier">s2</span>.<span class="ruby-identifier">fileno</span>)

<span class="ruby-identifier">s1</span>.<span class="ruby-identifier">send</span> <span class="ruby-string">&quot;a&quot;</span>, <span class="ruby-value">0</span>, <span class="ruby-identifier">s2_ai</span>
<span class="ruby-identifier">p</span> <span class="ruby-identifier">s3</span>.<span class="ruby-identifier">recvfrom</span>(<span class="ruby-value">10</span>) <span class="ruby-comment">#=&gt; [&quot;a&quot;, [&quot;AF_UNIX&quot;, &quot;/tmp/sock1&quot;]]</span>
</pre>
          
          

          
          <div class="method-source-code" id="recvfrom-source">
            <pre>static VALUE
unix_recvfrom(int argc, VALUE *argv, VALUE sock)
{
    return rsock_s_recvfrom(sock, argc, argv, RECV_UNIX);
}</pre>
          </div>
          
        </div>

        

        
      </div>

    
      <div id="method-i-send_io" class="method-detail ">
        
        
        <div class="method-heading">
          <span class="method-callseq">
            send_io(io) &rarr; nil
          </span>
          
          <span class="method-click-advice">click to toggle source</span>
          
        </div>
        
        

        <div class="method-description">
          
          <p>Sends <em>io</em> as file descriptor passing.</p>

<pre class="ruby"><span class="ruby-identifier">s1</span>, <span class="ruby-identifier">s2</span> = <span class="ruby-constant">UNIXSocket</span>.<span class="ruby-identifier">pair</span>

<span class="ruby-identifier">s1</span>.<span class="ruby-identifier">send_io</span> <span class="ruby-constant">STDOUT</span>
<span class="ruby-identifier">stdout</span> = <span class="ruby-identifier">s2</span>.<span class="ruby-identifier">recv_io</span>

<span class="ruby-identifier">p</span> <span class="ruby-constant">STDOUT</span>.<span class="ruby-identifier">fileno</span> <span class="ruby-comment">#=&gt; 1</span>
<span class="ruby-identifier">p</span> <span class="ruby-identifier">stdout</span>.<span class="ruby-identifier">fileno</span> <span class="ruby-comment">#=&gt; 6</span>

<span class="ruby-identifier">stdout</span>.<span class="ruby-identifier">puts</span> <span class="ruby-string">&quot;hello&quot;</span> <span class="ruby-comment"># outputs &quot;hello\n&quot; to standard output.</span>
</pre>

<p><em>io</em> may be any kind of <a href="IO.html"><code>IO</code></a> object or integer file descriptor.</p>
          
          

          
          <div class="method-source-code" id="send_io-source">
            <pre>static VALUE
unix_send_io(VALUE sock, VALUE val)
{
    int fd;
    rb_io_t *fptr;
    struct iomsg_arg arg;
    struct iovec vec[1];
    char buf[1];

#if FD_PASSING_BY_MSG_CONTROL
    union {
        struct cmsghdr hdr;
        char pad[sizeof(struct cmsghdr)+8+sizeof(int)+8];
    } cmsg;
#endif

    if (rb_obj_is_kind_of(val, rb_cIO)) {
        rb_io_t *valfptr;
        GetOpenFile(val, valfptr);
        fd = valfptr-&gt;fd;
    }
    else if (FIXNUM_P(val)) {
        fd = FIX2INT(val);
    }
    else {
        rb_raise(rb_eTypeError, &quot;neither IO nor file descriptor&quot;);
    }

    GetOpenFile(sock, fptr);

    arg.msg.msg_name = NULL;
    arg.msg.msg_namelen = 0;

    /* Linux and Solaris doesn&#39;t work if msg_iov is NULL. */
    buf[0] = &#39;\0&#39;;
    vec[0].iov_base = buf;
    vec[0].iov_len = 1;
    arg.msg.msg_iov = vec;
    arg.msg.msg_iovlen = 1;

#if FD_PASSING_BY_MSG_CONTROL
    arg.msg.msg_control = (caddr_t)&amp;cmsg;
    arg.msg.msg_controllen = (socklen_t)CMSG_LEN(sizeof(int));
    arg.msg.msg_flags = 0;
    MEMZERO((char*)&amp;cmsg, char, sizeof(cmsg));
    cmsg.hdr.cmsg_len = (socklen_t)CMSG_LEN(sizeof(int));
    cmsg.hdr.cmsg_level = SOL_SOCKET;
    cmsg.hdr.cmsg_type = SCM_RIGHTS;
    memcpy(CMSG_DATA(&amp;cmsg.hdr), &amp;fd, sizeof(int));
#else
    arg.msg.msg_accrights = (caddr_t)&amp;fd;
    arg.msg.msg_accrightslen = sizeof(fd);
#endif

    arg.fd = fptr-&gt;fd;
    while ((int)BLOCKING_REGION_FD(sendmsg_blocking, &amp;arg) == -1) {
        if (!rb_io_wait_writable(arg.fd))
            rsock_sys_fail_path(&quot;sendmsg(2)&quot;, fptr-&gt;pathv);
    }

    return Qnil;
}</pre>
          </div>
          
        </div>

        

        
      </div>

    
    </section>
  
  </section>

</main>


<footer id="validator-badges" role="contentinfo">
  <p><a href="https://validator.w3.org/check/referer">Validate</a>
  <p>Generated by <a href="https://ruby.github.io/rdoc/">RDoc</a> 6.2.1.1.
  <p>Based on <a href="http://deveiate.org/projects/Darkfish-RDoc/">Darkfish</a> by <a href="http://deveiate.org">Michael Granger</a>.
</footer>