PageRenderTime 62ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 1ms

/htdocs/ruby-stdlib/Socket.html

https://github.com/JerryWho/railsdoccompilation
HTML | 5176 lines | 4043 code | 1133 blank | 0 comment | 0 complexity | f30f5a039cae6e5da2887c37ac5af6ec MD5 | raw file
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  2. "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  3. <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
  4. <head>
  5. <meta name="Content-Type" content="text/html; charset=utf-8" />
  6. <title>Class: Socket</title>
  7. <link rel="stylesheet" href="css/style.css" type="text/css" media="screen" charset="utf-8" />
  8. <link rel="stylesheet" href="css/common.css" type="text/css" media="screen" charset="utf-8" />
  9. <script type="text/javascript" charset="utf-8">
  10. relpath = '';
  11. if (relpath != '') relpath += '/';
  12. </script>
  13. <script type="text/javascript" charset="utf-8" src="js/jquery.js"></script>
  14. <script type="text/javascript" charset="utf-8" src="js/app.js"></script>
  15. </head>
  16. <body>
  17. <script type="text/javascript" charset="utf-8">
  18. if (window.top.frames.main) document.body.className = 'frames';
  19. </script>
  20. <div id="header">
  21. <div id="menu">
  22. <a href="_index.html">Index (S)</a> &raquo;
  23. <span class="title">Socket</span>
  24. <div class="noframes"><span class="title">(</span><a href="." target="_top">no frames</a><span class="title">)</span></div>
  25. </div>
  26. <div id="search">
  27. <a id="class_list_link" href="#">Class List</a>
  28. <a id="method_list_link" href="#">Method List</a>
  29. <a id ="file_list_link" href="#">File List</a>
  30. </div>
  31. <div class="clear"></div>
  32. </div>
  33. <iframe id="search_frame"></iframe>
  34. <div id="content"><h1>Class: Socket
  35. </h1>
  36. <dl class="box">
  37. <dt class="r1">Inherits:</dt>
  38. <dd class="r1">
  39. <span class="inheritName"><span class='object_link'><a href="BasicSocket.html" title="BasicSocket (class)">BasicSocket</a></span></span>
  40. <ul class="fullTree">
  41. <li><span class='object_link'><a href="Object.html" title="Object (class)">Object</a></span></li>
  42. <li class="next"><span class='object_link'><a href="IO.html" title="IO (class)">IO</a></span></li>
  43. <li class="next"><span class='object_link'><a href="BasicSocket.html" title="BasicSocket (class)">BasicSocket</a></span></li>
  44. <li class="next">Socket</li>
  45. </ul>
  46. <a href="#" class="inheritanceTree">show all</a>
  47. </dd>
  48. <dt class="r2 last">Defined in:</dt>
  49. <dd class="r2 last">lib/ipaddr.rb<span class="defines">,<br />
  50. ext/socket/socket.c</span>
  51. </dd>
  52. </dl>
  53. <div class="clear"></div>
  54. <h2>Defined Under Namespace</h2>
  55. <p class="children">
  56. <strong class="modules">Modules:</strong> <span class='object_link'><a href="Socket/Constants.html" title="Socket::Constants (module)">Constants</a></span>
  57. <strong class="classes">Classes:</strong> <span class='object_link'><a href="Socket/AncillaryData.html" title="Socket::AncillaryData (class)">AncillaryData</a></span>, <span class='object_link'><a href="Socket/Option.html" title="Socket::Option (class)">Option</a></span>
  58. </p>
  59. <h2>Constant Summary</h2>
  60. <dl class="constants">
  61. <dt id="AF_INET6-constant" class="">AF_INET6 =
  62. </dt>
  63. <dd><pre class="code"><span class='const'>Object</span><span class='period'>.</span><span class='id new'>new</span></pre></dd>
  64. </dl>
  65. <h2>
  66. Class Method Summary
  67. <small>(<a href="#" class="summary_toggle">collapse</a>)</small>
  68. </h2>
  69. <ul class="summary">
  70. <li class="public ">
  71. <span class="summary_signature">
  72. <a href="#getaddrinfo-class_method" title="getaddrinfo (class method)">+ (Array) <strong>getaddrinfo</strong>(nodename, servname[, family[, socktype[, protocol[, flags[, reverse_lookup]]]]]) </a>
  73. </span>
  74. <span class="summary_desc"><div class='inline'><p>
  75. Obtains address information for <em>nodename</em>:<em>servname</em>.
  76. </p>
  77. </div></span>
  78. </li>
  79. <li class="public ">
  80. <span class="summary_signature">
  81. <a href="#gethostbyaddr-class_method" title="gethostbyaddr (class method)">+ (Object) <strong>gethostbyaddr</strong>(address_string[, address_family]) </a>
  82. </span>
  83. <span class="summary_desc"><div class='inline'><p>
  84. Obtains the host information for <em>address</em>.
  85. </p>
  86. </div></span>
  87. </li>
  88. <li class="public ">
  89. <span class="summary_signature">
  90. <a href="#gethostbyname-class_method" title="gethostbyname (class method)">+ (Array) <strong>gethostbyname</strong>(hostname) </a>
  91. </span>
  92. <span class="summary_desc"><div class='inline'><p>
  93. Obtains the host information for <em>hostname</em>.
  94. </p>
  95. </div></span>
  96. </li>
  97. <li class="public ">
  98. <span class="summary_signature">
  99. <a href="#gethostname-class_method" title="gethostname (class method)">+ (Object) <strong>gethostname</strong> </a>
  100. </span>
  101. <span class="summary_desc"><div class='inline'><p>
  102. Returns the hostname.
  103. </p>
  104. </div></span>
  105. </li>
  106. <li class="public ">
  107. <span class="summary_signature">
  108. <a href="#getnameinfo-class_method" title="getnameinfo (class method)">+ (Array) <strong>getnameinfo</strong>(sockaddr[, flags]) </a>
  109. </span>
  110. <span class="summary_desc"><div class='inline'><p>
  111. Obtains name information for <em>sockaddr</em>.
  112. </p>
  113. </div></span>
  114. </li>
  115. <li class="public ">
  116. <span class="summary_signature">
  117. <a href="#getservbyname-class_method" title="getservbyname (class method)">+ (Object) <strong>getservbyname</strong> </a>
  118. </span>
  119. <span class="summary_desc"><div class='inline'><p>
  120. Obtains the port number for <em>service_name</em>.
  121. </p>
  122. </div></span>
  123. </li>
  124. <li class="public ">
  125. <span class="summary_signature">
  126. <a href="#getservbyport-class_method" title="getservbyport (class method)">+ (Object) <strong>getservbyport</strong>(port[, protocol_name]) </a>
  127. </span>
  128. <span class="summary_desc"><div class='inline'><p>
  129. Obtains the port number for <em>port</em>.
  130. </p>
  131. </div></span>
  132. </li>
  133. <li class="public ">
  134. <span class="summary_signature">
  135. <a href="#ip_address_list-class_method" title="ip_address_list (class method)">+ (Array) <strong>ip_address_list</strong> </a>
  136. </span>
  137. <span class="summary_desc"><div class='inline'><p>
  138. Returns local IP addresses as an array.
  139. </p>
  140. </div></span>
  141. </li>
  142. <li class="public ">
  143. <span class="summary_signature">
  144. <a href="#pack_sockaddr_in-class_method" title="pack_sockaddr_in (class method)">+ (Object) <strong>pack_sockaddr_in</strong> </a>
  145. </span>
  146. <span class="summary_desc"><div class='inline'><p>
  147. Packs <em>port</em> and <em>host</em> as an AF_INET/AF_INET6 sockaddr
  148. string.
  149. </p>
  150. </div></span>
  151. </li>
  152. <li class="public ">
  153. <span class="summary_signature">
  154. <a href="#pack_sockaddr_un-class_method" title="pack_sockaddr_un (class method)">+ (Object) <strong>pack_sockaddr_un</strong> </a>
  155. </span>
  156. <span class="summary_desc"><div class='inline'><p>
  157. Packs <em>path</em> as an AF_UNIX sockaddr string.
  158. </p>
  159. </div></span>
  160. </li>
  161. <li class="public ">
  162. <span class="summary_signature">
  163. <a href="#pair-class_method" title="pair (class method)">+ (Object) <strong>pair</strong> </a>
  164. </span>
  165. <span class="summary_desc"><div class='inline'><p>
  166. Creates a pair of sockets connected each other.
  167. </p>
  168. </div></span>
  169. </li>
  170. <li class="public ">
  171. <span class="summary_signature">
  172. <a href="#sockaddr_in-class_method" title="sockaddr_in (class method)">+ (Object) <strong>sockaddr_in</strong> </a>
  173. </span>
  174. <span class="summary_desc"><div class='inline'><p>
  175. Packs <em>port</em> and <em>host</em> as an AF_INET/AF_INET6 sockaddr
  176. string.
  177. </p>
  178. </div></span>
  179. </li>
  180. <li class="public ">
  181. <span class="summary_signature">
  182. <a href="#sockaddr_un-class_method" title="sockaddr_un (class method)">+ (Object) <strong>sockaddr_un</strong> </a>
  183. </span>
  184. <span class="summary_desc"><div class='inline'><p>
  185. Packs <em>path</em> as an AF_UNIX sockaddr string.
  186. </p>
  187. </div></span>
  188. </li>
  189. <li class="public ">
  190. <span class="summary_signature">
  191. <a href="#socketpair-class_method" title="socketpair (class method)">+ (Object) <strong>socketpair</strong> </a>
  192. </span>
  193. <span class="summary_desc"><div class='inline'><p>
  194. Creates a pair of sockets connected each other.
  195. </p>
  196. </div></span>
  197. </li>
  198. <li class="public ">
  199. <span class="summary_signature">
  200. <a href="#unpack_sockaddr_in-class_method" title="unpack_sockaddr_in (class method)">+ (Array) <strong>unpack_sockaddr_in</strong>(sockaddr) </a>
  201. </span>
  202. <span class="summary_desc"><div class='inline'><p>
  203. Unpacks <em>sockaddr</em> into port and ip_address.
  204. </p>
  205. </div></span>
  206. </li>
  207. <li class="public ">
  208. <span class="summary_signature">
  209. <a href="#unpack_sockaddr_un-class_method" title="unpack_sockaddr_un (class method)">+ (Object) <strong>unpack_sockaddr_un</strong>(sockaddr) </a>
  210. </span>
  211. <span class="summary_desc"><div class='inline'><p>
  212. Unpacks <em>sockaddr</em> into path.
  213. </p>
  214. </div></span>
  215. </li>
  216. </ul>
  217. <h2>
  218. Instance Method Summary
  219. <small>(<a href="#" class="summary_toggle">collapse</a>)</small>
  220. </h2>
  221. <ul class="summary">
  222. <li class="public ">
  223. <span class="summary_signature">
  224. <a href="#accept-instance_method" title="#accept (instance method)">- (Array) <strong>accept</strong> </a>
  225. </span>
  226. <span class="summary_desc"><div class='inline'><p>
  227. Accepts a next connection.
  228. </p>
  229. </div></span>
  230. </li>
  231. <li class="public ">
  232. <span class="summary_signature">
  233. <a href="#accept_nonblock-instance_method" title="#accept_nonblock (instance method)">- (Array) <strong>accept_nonblock</strong> </a>
  234. </span>
  235. <span class="summary_desc"><div class='inline'><p>
  236. Accepts an incoming connection using accept(2) after O_NONBLOCK is set for
  237. the underlying file descriptor.
  238. </p>
  239. </div></span>
  240. </li>
  241. <li class="public ">
  242. <span class="summary_signature">
  243. <a href="#bind-instance_method" title="#bind (instance method)">- (0) <strong>bind</strong>(local_sockaddr) </a>
  244. </span>
  245. <span class="summary_desc"><div class='inline'><p>
  246. Binds to the given local address.
  247. </p>
  248. </div></span>
  249. </li>
  250. <li class="public ">
  251. <span class="summary_signature">
  252. <a href="#connect-instance_method" title="#connect (instance method)">- (0) <strong>connect</strong>(remote_sockaddr) </a>
  253. </span>
  254. <span class="summary_desc"><div class='inline'><p>
  255. Requests a connection to be made on the given <tt>remote_sockaddr</tt>.
  256. </p>
  257. </div></span>
  258. </li>
  259. <li class="public ">
  260. <span class="summary_signature">
  261. <a href="#connect_nonblock-instance_method" title="#connect_nonblock (instance method)">- (0) <strong>connect_nonblock</strong>(remote_sockaddr) </a>
  262. </span>
  263. <span class="summary_desc"><div class='inline'><p>
  264. Requests a connection to be made on the given <tt>remote_sockaddr</tt>
  265. after O_NONBLOCK is set for the underlying file descriptor.
  266. </p>
  267. </div></span>
  268. </li>
  269. <li class="public ">
  270. <span class="summary_signature">
  271. <a href="#new-instance_method" title="#new (instance method)">- (Object) <strong>new</strong>(domain, socktype[, protocol]) </a>
  272. </span>
  273. <span class="note title constructor">constructor</span>
  274. <span class="summary_desc"><div class='inline'><p>
  275. Creates a new socket object.
  276. </p>
  277. </div></span>
  278. </li>
  279. <li class="public ">
  280. <span class="summary_signature">
  281. <a href="#listen-instance_method" title="#listen (instance method)">- (0) <strong>listen</strong>(int) </a>
  282. </span>
  283. <span class="summary_desc"><div class='inline'><p>
  284. Listens for connections, using the specified <tt>int</tt> as the backlog.
  285. </p>
  286. </div></span>
  287. </li>
  288. <li class="public ">
  289. <span class="summary_signature">
  290. <a href="#recvfrom-instance_method" title="#recvfrom (instance method)">- (Object) <strong>recvfrom</strong> </a>
  291. </span>
  292. <span class="summary_desc"><div class='inline'><p>
  293. Receives up to <em>maxlen</em> bytes from <tt>socket</tt>.
  294. </p>
  295. </div></span>
  296. </li>
  297. <li class="public ">
  298. <span class="summary_signature">
  299. <a href="#recvfrom_nonblock-instance_method" title="#recvfrom_nonblock (instance method)">- (Object) <strong>recvfrom_nonblock</strong> </a>
  300. </span>
  301. <span class="summary_desc"><div class='inline'><p>
  302. Receives up to <em>maxlen</em> bytes from <tt>socket</tt> using recvfrom(2)
  303. after O_NONBLOCK is set for the underlying file descriptor.
  304. </p>
  305. </div></span>
  306. </li>
  307. <li class="public ">
  308. <span class="summary_signature">
  309. <a href="#sysaccept-instance_method" title="#sysaccept (instance method)">- (Array) <strong>sysaccept</strong> </a>
  310. </span>
  311. <span class="summary_desc"><div class='inline'><p>
  312. Accepts an incoming connection returning an array containing the (integer)
  313. file descriptor for the incoming connection, <em>client_socket_fd</em>, and
  314. an Addrinfo, <em>client_addrinfo</em>.
  315. </p>
  316. </div></span>
  317. </li>
  318. </ul>
  319. <h3 class="inherited">Methods inherited from <span class='object_link'><a href="BasicSocket.html" title="BasicSocket (class)">BasicSocket</a></span></h3>
  320. <p class="inherited"><span class='object_link'><a href="BasicSocket.html#close_read-instance_method" title="BasicSocket#close_read (method)">#close_read</a></span>, <span class='object_link'><a href="BasicSocket.html#close_write-instance_method" title="BasicSocket#close_write (method)">#close_write</a></span>, <span class='object_link'><a href="BasicSocket.html#do_not_reverse_lookup-instance_method" title="BasicSocket#do_not_reverse_lookup (method)">#do_not_reverse_lookup</a></span>, <span class='object_link'><a href="BasicSocket.html#do_not_reverse_lookup-class_method" title="BasicSocket.do_not_reverse_lookup (method)">do_not_reverse_lookup</a></span>, <span class='object_link'><a href="BasicSocket.html#do_not_reverse_lookup%3D-class_method" title="BasicSocket.do_not_reverse_lookup= (method)">do_not_reverse_lookup=</a></span>, <span class='object_link'><a href="BasicSocket.html#do_not_reverse_lookup%3D-instance_method" title="BasicSocket#do_not_reverse_lookup= (method)">#do_not_reverse_lookup=</a></span>, <span class='object_link'><a href="BasicSocket.html#for_fd-class_method" title="BasicSocket.for_fd (method)">for_fd</a></span>, <span class='object_link'><a href="BasicSocket.html#getpeereid-instance_method" title="BasicSocket#getpeereid (method)">#getpeereid</a></span>, <span class='object_link'><a href="BasicSocket.html#getpeername-instance_method" title="BasicSocket#getpeername (method)">#getpeername</a></span>, <span class='object_link'><a href="BasicSocket.html#getsockname-instance_method" title="BasicSocket#getsockname (method)">#getsockname</a></span>, <span class='object_link'><a href="BasicSocket.html#getsockopt-instance_method" title="BasicSocket#getsockopt (method)">#getsockopt</a></span>, <span class='object_link'><a href="BasicSocket.html#local_address-instance_method" title="BasicSocket#local_address (method)">#local_address</a></span>, <span class='object_link'><a href="BasicSocket.html#recv-instance_method" title="BasicSocket#recv (method)">#recv</a></span>, <span class='object_link'><a href="BasicSocket.html#recv_nonblock-instance_method" title="BasicSocket#recv_nonblock (method)">#recv_nonblock</a></span>, <span class='object_link'><a href="BasicSocket.html#recvmsg-instance_method" title="BasicSocket#recvmsg (method)">#recvmsg</a></span>, <span class='object_link'><a href="BasicSocket.html#recvmsg_nonblock-instance_method" title="BasicSocket#recvmsg_nonblock (method)">#recvmsg_nonblock</a></span>, <span class='object_link'><a href="BasicSocket.html#remote_address-instance_method" title="BasicSocket#remote_address (method)">#remote_address</a></span>, <span class='object_link'><a href="BasicSocket.html#send-instance_method" title="BasicSocket#send (method)">#send</a></span>, <span class='object_link'><a href="BasicSocket.html#sendmsg-instance_method" title="BasicSocket#sendmsg (method)">#sendmsg</a></span>, <span class='object_link'><a href="BasicSocket.html#sendmsg_nonblock-instance_method" title="BasicSocket#sendmsg_nonblock (method)">#sendmsg_nonblock</a></span>, <span class='object_link'><a href="BasicSocket.html#setsockopt-instance_method" title="BasicSocket#setsockopt (method)">#setsockopt</a></span>, <span class='object_link'><a href="BasicSocket.html#shutdown-instance_method" title="BasicSocket#shutdown (method)">#shutdown</a></span></p>
  321. <h3 class="inherited">Methods inherited from <span class='object_link'><a href="IO.html" title="IO (class)">IO</a></span></h3>
  322. <p class="inherited"><span class='object_link'><a href="IO.html#nread-instance_method" title="IO#nread (method)">#nread</a></span>, <span class='object_link'><a href="IO.html#ready%3F-instance_method" title="IO#ready? (method)">#ready?</a></span>, <span class='object_link'><a href="IO.html#scanf-instance_method" title="IO#scanf (method)">#scanf</a></span>, <span class='object_link'><a href="IO.html#wait-instance_method" title="IO#wait (method)">#wait</a></span></p>
  323. <div id="constructor_details" class="method_details_list">
  324. <h2>Constructor Details</h2>
  325. <div class="method_details first">
  326. <p class="signature first" id="initialize-instance_method">
  327. - (<tt><span class='object_link'><a href="Object.html" title="Object (class)">Object</a></span></tt>) <strong>new</strong>(domain, socktype[, protocol])
  328. </p><div class="docstring">
  329. <div class="discussion">
  330. <p>
  331. Creates a new socket object.
  332. </p>
  333. <p>
  334. <em>domain</em> should be a communications domain such as: :INET, :INET6,
  335. :UNIX, etc.
  336. </p>
  337. <p>
  338. <em>socktype</em> should be a socket type such as: :STREAM, :DGRAM, :RAW,
  339. etc.
  340. </p>
  341. <p>
  342. <em>protocol</em> should be a protocol defined in the domain. This is
  343. optional. If it is not given, 0 is used internally.
  344. </p>
  345. <pre class="code">
  346. Socket.new(:INET, :STREAM) # TCP socket
  347. Socket.new(:INET, :DGRAM) # UDP socket
  348. Socket.new(:UNIX, :STREAM) # UNIX stream socket
  349. Socket.new(:UNIX, :DGRAM) # UNIX datagram socket</pre>
  350. </div>
  351. </div>
  352. <div class="tags">
  353. </div><table class="source_code">
  354. <tr>
  355. <td>
  356. <pre class="lines">
  357. </pre>
  358. </td>
  359. <td>
  360. <pre class="code"><span class="info file"># File 'ext/socket/socket.c'</span>
  361. /*
  362. * call-seq:
  363. * Socket.new(domain, socktype [, protocol]) =&gt; socket
  364. *
  365. * Creates a new socket object.
  366. *
  367. * _domain_ should be a communications domain such as: :INET, :INET6, :UNIX, etc.
  368. *
  369. * _socktype_ should be a socket type such as: :STREAM, :DGRAM, :RAW, etc.
  370. *
  371. * _protocol_ should be a protocol defined in the domain.
  372. * This is optional.
  373. * If it is not given, 0 is used internally.
  374. *
  375. * Socket.new(:INET, :STREAM) # TCP socket
  376. * Socket.new(:INET, :DGRAM) # UDP socket
  377. * Socket.new(:UNIX, :STREAM) # UNIX stream socket
  378. * Socket.new(:UNIX, :DGRAM) # UNIX datagram socket
  379. */
  380. static VALUE
  381. sock_initialize(int argc, VALUE *argv, VALUE sock)
  382. {
  383. VALUE domain, type, protocol;
  384. int fd;
  385. int d, t;
  386. rb_scan_args(argc, argv, &quot;21&quot;, &amp;domain, &amp;type, &amp;protocol);
  387. if (NIL_P(protocol))
  388. protocol = INT2FIX(0);
  389. rb_secure(3);
  390. setup_domain_and_type(domain, &amp;d, type, &amp;t);
  391. fd = rsock_socket(d, t, NUM2INT(protocol));
  392. if (fd &lt; 0) rb_sys_fail(&quot;socket(2)&quot;);
  393. return rsock_init_sock(sock, fd);
  394. }</pre>
  395. </td>
  396. </tr>
  397. </table>
  398. </div>
  399. </div>
  400. <div id="class_method_details" class="method_details_list">
  401. <h2>Class Method Details</h2>
  402. <div class="method_details first">
  403. <p class="signature first" id="getaddrinfo-class_method">
  404. + (<tt><span class='object_link'><a href="Array.html" title="Array (class)">Array</a></span></tt>) <strong>getaddrinfo</strong>(nodename, servname[, family[, socktype[, protocol[, flags[, reverse_lookup]]]]])
  405. </p><div class="docstring">
  406. <div class="discussion">
  407. <p>
  408. Obtains address information for <em>nodename</em>:<em>servname</em>.
  409. </p>
  410. <p>
  411. <em>family</em> should be an address family such as: :INET, :INET6, :UNIX,
  412. etc.
  413. </p>
  414. <p>
  415. <em>socktype</em> should be a socket type such as: :STREAM, :DGRAM, :RAW,
  416. etc.
  417. </p>
  418. <p>
  419. <em>protocol</em> should be a protocol defined in the family. 0 is default
  420. protocol for the family.
  421. </p>
  422. <p>
  423. <em>flags</em> should be bitwise OR of Socket::AI_* constants.
  424. </p>
  425. <pre class="code">
  426. Socket.getaddrinfo(&quot;www.ruby-lang.org&quot;, &quot;http&quot;, nil, :STREAM)
  427. #=&gt; [[&quot;AF_INET&quot;, 80, &quot;carbon.ruby-lang.org&quot;, &quot;221.186.184.68&quot;, 2, 1, 6]] # PF_INET/SOCK_STREAM/IPPROTO_TCP
  428. Socket.getaddrinfo(&quot;localhost&quot;, nil)
  429. #=&gt; [[&quot;AF_INET&quot;, 0, &quot;localhost&quot;, &quot;127.0.0.1&quot;, 2, 1, 6], # PF_INET/SOCK_STREAM/IPPROTO_TCP
  430. # [&quot;AF_INET&quot;, 0, &quot;localhost&quot;, &quot;127.0.0.1&quot;, 2, 2, 17], # PF_INET/SOCK_DGRAM/IPPROTO_UDP
  431. # [&quot;AF_INET&quot;, 0, &quot;localhost&quot;, &quot;127.0.0.1&quot;, 2, 3, 0]] # PF_INET/SOCK_RAW/IPPROTO_IP
  432. </pre>
  433. <p>
  434. <em>reverse_lookup</em> directs the form of the third element, and has to
  435. be one of below. If it is ommitted, the default value is <tt>nil</tt>.
  436. </p>
  437. <pre class="code">
  438. +true+, +:hostname+: hostname is obtained from numeric address using reverse lookup, which may take a time.
  439. +false+, +:numeric+: hostname is same as numeric address.
  440. +nil+: obey to the current +do_not_reverse_lookup+ flag.
  441. </pre>
  442. <p>
  443. If Addrinfo object is preferred, use Addrinfo.getaddrinfo.
  444. </p>
  445. </div>
  446. </div>
  447. <div class="tags">
  448. <div class="docstring">
  449. <div class="discussion">
  450. </div>
  451. </div>
  452. <div class="tags">
  453. <h3>Returns:</h3>
  454. <ul class="return">
  455. <li>
  456. <span class='type'>(<tt><span class='object_link'><a href="Array.html" title="Array (class)">Array</a></span></tt>)</span>
  457. </li>
  458. </ul>
  459. </div>
  460. </div><table class="source_code">
  461. <tr>
  462. <td>
  463. <pre class="lines">
  464. </pre>
  465. </td>
  466. <td>
  467. <pre class="code"><span class="info file"># File 'ext/socket/socket.c'</span>
  468. /*
  469. * call-seq:
  470. * Socket.getaddrinfo(nodename, servname[, family[, socktype[, protocol[, flags[, reverse_lookup]]]]]) =&gt; array
  471. *
  472. * Obtains address information for _nodename_:_servname_.
  473. *
  474. * _family_ should be an address family such as: :INET, :INET6, :UNIX, etc.
  475. *
  476. * _socktype_ should be a socket type such as: :STREAM, :DGRAM, :RAW, etc.
  477. *
  478. * _protocol_ should be a protocol defined in the family.
  479. * 0 is default protocol for the family.
  480. *
  481. * _flags_ should be bitwise OR of Socket::AI_* constants.
  482. *
  483. * Socket.getaddrinfo(&quot;www.ruby-lang.org&quot;, &quot;http&quot;, nil, :STREAM)
  484. * #=&gt; [[&quot;AF_INET&quot;, 80, &quot;carbon.ruby-lang.org&quot;, &quot;221.186.184.68&quot;, 2, 1, 6]] # PF_INET/SOCK_STREAM/IPPROTO_TCP
  485. *
  486. * Socket.getaddrinfo(&quot;localhost&quot;, nil)
  487. * #=&gt; [[&quot;AF_INET&quot;, 0, &quot;localhost&quot;, &quot;127.0.0.1&quot;, 2, 1, 6], # PF_INET/SOCK_STREAM/IPPROTO_TCP
  488. * # [&quot;AF_INET&quot;, 0, &quot;localhost&quot;, &quot;127.0.0.1&quot;, 2, 2, 17], # PF_INET/SOCK_DGRAM/IPPROTO_UDP
  489. * # [&quot;AF_INET&quot;, 0, &quot;localhost&quot;, &quot;127.0.0.1&quot;, 2, 3, 0]] # PF_INET/SOCK_RAW/IPPROTO_IP
  490. *
  491. * _reverse_lookup_ directs the form of the third element, and has to
  492. * be one of below.
  493. * If it is ommitted, the default value is +nil+.
  494. *
  495. * +true+, +:hostname+: hostname is obtained from numeric address using reverse lookup, which may take a time.
  496. * +false+, +:numeric+: hostname is same as numeric address.
  497. * +nil+: obey to the current +do_not_reverse_lookup+ flag.
  498. *
  499. * If Addrinfo object is preferred, use Addrinfo.getaddrinfo.
  500. */
  501. static VALUE
  502. sock_s_getaddrinfo(int argc, VALUE *argv)
  503. {
  504. VALUE host, port, family, socktype, protocol, flags, ret, revlookup;
  505. struct addrinfo hints, *res;
  506. int norevlookup;
  507. rb_scan_args(argc, argv, &quot;25&quot;, &amp;host, &amp;port, &amp;family, &amp;socktype, &amp;protocol, &amp;flags, &amp;revlookup);
  508. MEMZERO(&amp;hints, struct addrinfo, 1);
  509. hints.ai_family = NIL_P(family) ? PF_UNSPEC : rsock_family_arg(family);
  510. if (!NIL_P(socktype)) {
  511. hints.ai_socktype = rsock_socktype_arg(socktype);
  512. }
  513. if (!NIL_P(protocol)) {
  514. hints.ai_protocol = NUM2INT(protocol);
  515. }
  516. if (!NIL_P(flags)) {
  517. hints.ai_flags = NUM2INT(flags);
  518. }
  519. if (NIL_P(revlookup) || !rsock_revlookup_flag(revlookup, &amp;norevlookup)) {
  520. norevlookup = rsock_do_not_reverse_lookup;
  521. }
  522. res = rsock_getaddrinfo(host, port, &amp;hints, 0);
  523. ret = make_addrinfo(res, norevlookup);
  524. freeaddrinfo(res);
  525. return ret;
  526. }</pre>
  527. </td>
  528. </tr>
  529. </table>
  530. </div>
  531. <div class="method_details ">
  532. <p class="signature " id="gethostbyaddr-class_method">
  533. + (<tt><span class='object_link'><a href="Object.html" title="Object (class)">Object</a></span></tt>) <strong>gethostbyaddr</strong>(address_string[, address_family])
  534. </p><div class="docstring">
  535. <div class="discussion">
  536. <p>
  537. Obtains the host information for <em>address</em>.
  538. </p>
  539. <pre class="code">
  540. p Socket.gethostbyaddr([221,186,184,68].pack(&quot;CCCC&quot;))
  541. #=&gt; [&quot;carbon.ruby-lang.org&quot;, [], 2, &quot;\xDD\xBA\xB8D&quot;]</pre>
  542. </div>
  543. </div>
  544. <div class="tags">
  545. </div><table class="source_code">
  546. <tr>
  547. <td>
  548. <pre class="lines">
  549. </pre>
  550. </td>
  551. <td>
  552. <pre class="code"><span class="info file"># File 'ext/socket/socket.c'</span>
  553. /*
  554. * call-seq:
  555. * Socket.gethostbyaddr(address_string [, address_family]) =&gt; hostent
  556. *
  557. * Obtains the host information for _address_.
  558. *
  559. * p Socket.gethostbyaddr([221,186,184,68].pack(&quot;CCCC&quot;))
  560. * #=&gt; [&quot;carbon.ruby-lang.org&quot;, [], 2, &quot;\xDD\xBA\xB8D&quot;]
  561. */
  562. static VALUE
  563. sock_s_gethostbyaddr(int argc, VALUE *argv)
  564. {
  565. VALUE addr, family;
  566. struct hostent *h;
  567. struct sockaddr *sa;
  568. char **pch;
  569. VALUE ary, names;
  570. int t = AF_INET;
  571. rb_scan_args(argc, argv, &quot;11&quot;, &amp;addr, &amp;family);
  572. sa = (struct sockaddr*)StringValuePtr(addr);
  573. if (!NIL_P(family)) {
  574. t = rsock_family_arg(family);
  575. }
  576. #ifdef AF_INET6
  577. else if (RSTRING_LEN(addr) == 16) {
  578. t = AF_INET6;
  579. }
  580. #endif
  581. h = gethostbyaddr(RSTRING_PTR(addr), RSTRING_LENINT(addr), t);
  582. if (h == NULL) {
  583. #ifdef HAVE_HSTRERROR
  584. extern int h_errno;
  585. rb_raise(rb_eSocket, &quot;%s&quot;, (char*)hstrerror(h_errno));
  586. #else
  587. rb_raise(rb_eSocket, &quot;host not found&quot;);
  588. #endif
  589. }
  590. ary = rb_ary_new();
  591. rb_ary_push(ary, rb_str_new2(h-&gt;h_name));
  592. names = rb_ary_new();
  593. rb_ary_push(ary, names);
  594. if (h-&gt;h_aliases != NULL) {
  595. for (pch = h-&gt;h_aliases; *pch; pch++) {
  596. rb_ary_push(names, rb_str_new2(*pch));
  597. }
  598. }
  599. rb_ary_push(ary, INT2NUM(h-&gt;h_addrtype));
  600. #ifdef h_addr
  601. for (pch = h-&gt;h_addr_list; *pch; pch++) {
  602. rb_ary_push(ary, rb_str_new(*pch, h-&gt;h_length));
  603. }
  604. #else
  605. rb_ary_push(ary, rb_str_new(h-&gt;h_addr, h-&gt;h_length));
  606. #endif
  607. return ary;
  608. }</pre>
  609. </td>
  610. </tr>
  611. </table>
  612. </div>
  613. <div class="method_details ">
  614. <p class="signature " id="gethostbyname-class_method">
  615. + (<tt><span class='object_link'><a href="Array.html" title="Array (class)">Array</a></span></tt>) <strong>gethostbyname</strong>(hostname)
  616. </p><div class="docstring">
  617. <div class="discussion">
  618. <p>
  619. Obtains the host information for <em>hostname</em>.
  620. </p>
  621. <pre class="code">
  622. p Socket.gethostbyname(&quot;hal&quot;) #=&gt; [&quot;localhost&quot;, [&quot;hal&quot;], 2, &quot;\x7F\x00\x00\x01&quot;]</pre>
  623. </div>
  624. </div>
  625. <div class="tags">
  626. <div class="docstring">
  627. <div class="discussion">
  628. </div>
  629. </div>
  630. <div class="tags">
  631. <h3>Returns:</h3>
  632. <ul class="return">
  633. <li>
  634. <span class='type'>(<tt><span class='object_link'><a href="Array.html" title="Array (class)">Array</a></span></tt>)</span>
  635. </li>
  636. </ul>
  637. </div>
  638. </div><table class="source_code">
  639. <tr>
  640. <td>
  641. <pre class="lines">
  642. </pre>
  643. </td>
  644. <td>
  645. <pre class="code"><span class="info file"># File 'ext/socket/socket.c'</span>
  646. /*
  647. * call-seq:
  648. * Socket.gethostbyname(hostname) =&gt; [official_hostname, alias_hostnames, address_family, *address_list]
  649. *
  650. * Obtains the host information for _hostname_.
  651. *
  652. * p Socket.gethostbyname(&quot;hal&quot;) #=&gt; [&quot;localhost&quot;, [&quot;hal&quot;], 2, &quot;\x7F\x00\x00\x01&quot;]
  653. *
  654. */
  655. static VALUE
  656. sock_s_gethostbyname(VALUE obj, VALUE host)
  657. {
  658. rb_secure(3);
  659. return rsock_make_hostent(host, rsock_addrinfo(host, Qnil, SOCK_STREAM, AI_CANONNAME), sock_sockaddr);
  660. }</pre>
  661. </td>
  662. </tr>
  663. </table>
  664. </div>
  665. <div class="method_details ">
  666. <p class="signature " id="gethostname-class_method">
  667. + (<tt><span class='object_link'><a href="Object.html" title="Object (class)">Object</a></span></tt>) <strong>gethostname</strong>
  668. </p><div class="docstring">
  669. <div class="discussion">
  670. <p>
  671. Returns the hostname.
  672. </p>
  673. <pre class="code">
  674. p Socket.gethostname #=&gt; &quot;hal&quot;
  675. </pre>
  676. <p>
  677. Note that it is not guaranteed to be able to convert to IP address using
  678. gethostbyname, getaddrinfo, etc. If you need local IP address, use
  679. Socket.ip_address_list.
  680. </p>
  681. </div>
  682. </div>
  683. <div class="tags">
  684. </div><table class="source_code">
  685. <tr>
  686. <td>
  687. <pre class="lines">
  688. </pre>
  689. </td>
  690. <td>
  691. <pre class="code"><span class="info file"># File 'ext/socket/socket.c'</span>
  692. /*
  693. * call-seq:
  694. * Socket.gethostname =&gt; hostname
  695. *
  696. * Returns the hostname.
  697. *
  698. * p Socket.gethostname #=&gt; &quot;hal&quot;
  699. *
  700. * Note that it is not guaranteed to be able to convert to IP address using gethostbyname, getaddrinfo, etc.
  701. * If you need local IP address, use Socket.ip_address_list.
  702. */
  703. static VALUE
  704. sock_gethostname(VALUE obj)
  705. {
  706. #ifndef HOST_NAME_MAX
  707. # define HOST_NAME_MAX 1024
  708. #endif
  709. char buf[HOST_NAME_MAX+1];
  710. rb_secure(3);
  711. if (gethostname(buf, (int)sizeof buf - 1) &lt; 0)
  712. rb_sys_fail(&quot;gethostname&quot;);
  713. buf[sizeof buf - 1] = '\0';
  714. return rb_str_new2(buf);
  715. }</pre>
  716. </td>
  717. </tr>
  718. </table>
  719. </div>
  720. <div class="method_details ">
  721. <p class="signature " id="getnameinfo-class_method">
  722. + (<tt><span class='object_link'><a href="Array.html" title="Array (class)">Array</a></span></tt>) <strong>getnameinfo</strong>(sockaddr[, flags])
  723. </p><div class="docstring">
  724. <div class="discussion">
  725. <p>
  726. Obtains name information for <em>sockaddr</em>.
  727. </p>
  728. <p>
  729. <em>sockaddr</em> should be one of follows.
  730. </p>
  731. <ul>
  732. <li><p>
  733. packed sockaddr string such as Socket.sockaddr_in(80,
  734. &#8220;127.0.0.1&#8221;)
  735. </p>
  736. </li>
  737. <li><p>
  738. 3-elements array such as [&#8220;AF_INET&#8221;, 80,
  739. &#8220;127.0.0.1&#8221;]
  740. </p>
  741. </li>
  742. <li><p>
  743. 4-elements array such as [&#8220;AF_INET&#8221;, 80, ignored,
  744. &#8220;127.0.0.1&#8221;]
  745. </p>
  746. </li>
  747. </ul>
  748. <p>
  749. <em>flags</em> should be bitwise OR of Socket::NI_* constants.
  750. </p>
  751. <p>
  752. Note that the last form is compatible with IPSocket#addr,peeraddr.
  753. </p>
  754. <pre class="code">
  755. Socket.getnameinfo(Socket.sockaddr_in(80, &quot;127.0.0.1&quot;)) #=&gt; [&quot;localhost&quot;, &quot;www&quot;]
  756. Socket.getnameinfo([&quot;AF_INET&quot;, 80, &quot;127.0.0.1&quot;]) #=&gt; [&quot;localhost&quot;, &quot;www&quot;]
  757. Socket.getnameinfo([&quot;AF_INET&quot;, 80, &quot;localhost&quot;, &quot;127.0.0.1&quot;]) #=&gt; [&quot;localhost&quot;, &quot;www&quot;]
  758. </pre>
  759. <p>
  760. If Addrinfo object is preferred, use Addrinfo#getnameinfo.
  761. </p>
  762. </div>
  763. </div>
  764. <div class="tags">
  765. <div class="docstring">
  766. <div class="discussion">
  767. </div>
  768. </div>
  769. <div class="tags">
  770. <h3>Returns:</h3>
  771. <ul class="return">
  772. <li>
  773. <span class='type'>(<tt><span class='object_link'><a href="Array.html" title="Array (class)">Array</a></span></tt>)</span>
  774. </li>
  775. </ul>
  776. </div>
  777. </div><table class="source_code">
  778. <tr>
  779. <td>
  780. <pre class="lines">
  781. </pre>
  782. </td>
  783. <td>
  784. <pre class="code"><span class="info file"># File 'ext/socket/socket.c'</span>
  785. /*
  786. * call-seq:
  787. * Socket.getnameinfo(sockaddr [, flags]) =&gt; [hostname, servicename]
  788. *
  789. * Obtains name information for _sockaddr_.
  790. *
  791. * _sockaddr_ should be one of follows.
  792. * - packed sockaddr string such as Socket.sockaddr_in(80, &quot;127.0.0.1&quot;)
  793. * - 3-elements array such as [&quot;AF_INET&quot;, 80, &quot;127.0.0.1&quot;]
  794. * - 4-elements array such as [&quot;AF_INET&quot;, 80, ignored, &quot;127.0.0.1&quot;]
  795. *
  796. * _flags_ should be bitwise OR of Socket::NI_* constants.
  797. *
  798. * Note that the last form is compatible with IPSocket#{addr,peeraddr}.
  799. *
  800. * Socket.getnameinfo(Socket.sockaddr_in(80, &quot;127.0.0.1&quot;)) #=&gt; [&quot;localhost&quot;, &quot;www&quot;]
  801. * Socket.getnameinfo([&quot;AF_INET&quot;, 80, &quot;127.0.0.1&quot;]) #=&gt; [&quot;localhost&quot;, &quot;www&quot;]
  802. * Socket.getnameinfo([&quot;AF_INET&quot;, 80, &quot;localhost&quot;, &quot;127.0.0.1&quot;]) #=&gt; [&quot;localhost&quot;, &quot;www&quot;]
  803. *
  804. * If Addrinfo object is preferred, use Addrinfo#getnameinfo.
  805. */
  806. static VALUE
  807. sock_s_getnameinfo(int argc, VALUE *argv)
  808. {
  809. VALUE sa, af = Qnil, host = Qnil, port = Qnil, flags, tmp;
  810. char *hptr, *pptr;
  811. char hbuf[1024], pbuf[1024];
  812. int fl;
  813. struct addrinfo hints, *res = NULL, *r;
  814. int error;
  815. struct sockaddr_storage ss;
  816. struct sockaddr *sap;
  817. sa = flags = Qnil;
  818. rb_scan_args(argc, argv, &quot;11&quot;, &amp;sa, &amp;flags);
  819. fl = 0;
  820. if (!NIL_P(flags)) {
  821. fl = NUM2INT(flags);
  822. }
  823. tmp = rb_check_sockaddr_string_type(sa);
  824. if (!NIL_P(tmp)) {
  825. sa = tmp;
  826. if (sizeof(ss) &lt; (size_t)RSTRING_LEN(sa)) {
  827. rb_raise(rb_eTypeError, &quot;sockaddr length too big&quot;);
  828. }
  829. memcpy(&amp;ss, RSTRING_PTR(sa), RSTRING_LEN(sa));
  830. if ((size_t)RSTRING_LEN(sa) != SS_LEN(&amp;ss)) {
  831. rb_raise(rb_eTypeError, &quot;sockaddr size differs - should not happen&quot;);
  832. }
  833. sap = (struct sockaddr*)&amp;ss;
  834. goto call_nameinfo;
  835. }
  836. tmp = rb_check_array_type(sa);
  837. if (!NIL_P(tmp)) {
  838. sa = tmp;
  839. MEMZERO(&amp;hints, struct addrinfo, 1);
  840. if (RARRAY_LEN(sa) == 3) {
  841. af = RARRAY_PTR(sa)[0];
  842. port = RARRAY_PTR(sa)[1];
  843. host = RARRAY_PTR(sa)[2];
  844. }
  845. else if (RARRAY_LEN(sa) &gt;= 4) {
  846. af = RARRAY_PTR(sa)[0];
  847. port = RARRAY_PTR(sa)[1];
  848. host = RARRAY_PTR(sa)[3];
  849. if (NIL_P(host)) {
  850. host = RARRAY_PTR(sa)[2];
  851. }
  852. else {
  853. /*
  854. * 4th element holds numeric form, don't resolve.
  855. * see rsock_ipaddr().
  856. */
  857. #ifdef AI_NUMERICHOST /* AIX 4.3.3 doesn't have AI_NUMERICHOST. */
  858. hints.ai_flags |= AI_NUMERICHOST;
  859. #endif
  860. }
  861. }
  862. else {
  863. rb_raise(rb_eArgError, &quot;array size should be 3 or 4, %ld given&quot;,
  864. RARRAY_LEN(sa));
  865. }
  866. /* host */
  867. if (NIL_P(host)) {
  868. hptr = NULL;
  869. }
  870. else {
  871. strncpy(hbuf, StringValuePtr(host), sizeof(hbuf));
  872. hbuf[sizeof(hbuf) - 1] = '\0';
  873. hptr = hbuf;
  874. }
  875. /* port */
  876. if (NIL_P(port)) {
  877. strcpy(pbuf, &quot;0&quot;);
  878. pptr = NULL;
  879. }
  880. else if (FIXNUM_P(port)) {
  881. snprintf(pbuf, sizeof(pbuf), &quot;%ld&quot;, NUM2LONG(port));
  882. pptr = pbuf;
  883. }
  884. else {
  885. strncpy(pbuf, StringValuePtr(port), sizeof(pbuf));
  886. pbuf[sizeof(pbuf) - 1] = '\0';
  887. pptr = pbuf;
  888. }
  889. hints.ai_socktype = (fl &amp; NI_DGRAM) ? SOCK_DGRAM : SOCK_STREAM;
  890. /* af */
  891. hints.ai_family = NIL_P(af) ? PF_UNSPEC : rsock_family_arg(af);
  892. error = rb_getaddrinfo(hptr, pptr, &amp;hints, &amp;res);
  893. if (error) goto error_exit_addr;
  894. sap = res-&gt;ai_addr;
  895. }
  896. else {
  897. rb_raise(rb_eTypeError, &quot;expecting String or Array&quot;);
  898. }
  899. call_nameinfo:
  900. error = rb_getnameinfo(sap, SA_LEN(sap), hbuf, sizeof(hbuf),
  901. pbuf, sizeof(pbuf), fl);
  902. if (error) goto error_exit_name;
  903. if (res) {
  904. for (r = res-&gt;ai_next; r; r = r-&gt;ai_next) {
  905. char hbuf2[1024], pbuf2[1024];
  906. sap = r-&gt;ai_addr;
  907. error = rb_getnameinfo(sap, SA_LEN(sap), hbuf2, sizeof(hbuf2),
  908. pbuf2, sizeof(pbuf2), fl);
  909. if (error) goto error_exit_name;
  910. if (strcmp(hbuf, hbuf2) != 0|| strcmp(pbuf, pbuf2) != 0) {
  911. freeaddrinfo(res);
  912. rb_raise(rb_eSocket, &quot;sockaddr resolved to multiple nodename&quot;);
  913. }
  914. }
  915. freeaddrinfo(res);
  916. }
  917. return rb_assoc_new(rb_str_new2(hbuf), rb_str_new2(pbuf));
  918. error_exit_addr:
  919. if (res) freeaddrinfo(res);
  920. rsock_raise_socket_error(&quot;getaddrinfo&quot;, error);
  921. error_exit_name:
  922. if (res) freeaddrinfo(res);
  923. rsock_raise_socket_error(&quot;getnameinfo&quot;, error);
  924. }</pre>
  925. </td>
  926. </tr>
  927. </table>
  928. </div>
  929. <div class="method_details ">
  930. <p class="signature " id="getservbyname-class_method">
  931. <span class="overload">+ (<tt><span class='object_link'><a href="Object.html" title="Object (class)">Object</a></span></tt>) <strong>getservbyname</strong>(service_name) </span>
  932. <span class="overload">+ (<tt><span class='object_link'><a href="Object.html" title="Object (class)">Object</a></span></tt>) <strong>getservbyname</strong>(service_name, protocol_name) </span>
  933. </p><div class="docstring">
  934. <div class="discussion">
  935. <p>
  936. Obtains the port number for <em>service_name</em>.
  937. </p>
  938. <p>
  939. If <em>protocol_name</em> is not given, &#8220;tcp&#8221; is assumed.
  940. </p>
  941. <pre class="code">
  942. Socket.getservbyname(&quot;smtp&quot;) #=&gt; 25
  943. Socket.getservbyname(&quot;shell&quot;) #=&gt; 514
  944. Socket.getservbyname(&quot;syslog&quot;, &quot;udp&quot;) #=&gt; 514</pre>
  945. </div>
  946. </div>
  947. <div class="tags">
  948. </div><table class="source_code">
  949. <tr>
  950. <td>
  951. <pre class="lines">
  952. </pre>
  953. </td>
  954. <td>
  955. <pre class="code"><span class="info file"># File 'ext/socket/socket.c'</span>
  956. /*
  957. * call-seq:
  958. * Socket.getservbyname(service_name) =&gt; port_number
  959. * Socket.getservbyname(service_name, protocol_name) =&gt; port_number
  960. *
  961. * Obtains the port number for _service_name_.
  962. *
  963. * If _protocol_name_ is not given, &quot;tcp&quot; is assumed.
  964. *
  965. * Socket.getservbyname(&quot;smtp&quot;) #=&gt; 25
  966. * Socket.getservbyname(&quot;shell&quot;) #=&gt; 514
  967. * Socket.getservbyname(&quot;syslog&quot;, &quot;udp&quot;) #=&gt; 514
  968. */
  969. static VALUE
  970. sock_s_getservbyname(int argc, VALUE *argv)
  971. {
  972. VALUE service, proto;
  973. struct servent *sp;
  974. long port;
  975. const char *servicename, *protoname = &quot;tcp&quot;;
  976. rb_scan_args(argc, argv, &quot;11&quot;, &amp;service, &amp;proto);
  977. StringValue(service);
  978. if (!NIL_P(proto)) StringValue(proto);
  979. servicename = StringValueCStr(service);
  980. if (!NIL_P(proto)) protoname = StringValueCStr(proto);
  981. sp = getservbyname(servicename, protoname);
  982. if (sp) {
  983. port = ntohs(sp-&gt;s_port);
  984. }
  985. else {
  986. char *end;
  987. port = STRTOUL(servicename, &amp;end, 0);
  988. if (*end != '\0') {
  989. rb_raise(rb_eSocket, &quot;no such service %s/%s&quot;, servicename, protoname);
  990. }
  991. }
  992. return INT2FIX(port);
  993. }</pre>
  994. </td>
  995. </tr>
  996. </table>
  997. </div>
  998. <div class="method_details ">
  999. <p class="signature " id="getservbyport-class_method">
  1000. + (<tt><span class='object_link'><a href="Object.html" title="Object (class)">Object</a></span></tt>) <strong>getservbyport</strong>(port[, protocol_name])
  1001. </p><div class="docstring">
  1002. <div class="discussion">
  1003. <p>
  1004. Obtains the port number for <em>port</em>.
  1005. </p>
  1006. <p>
  1007. If <em>protocol_name</em> is not given, &#8220;tcp&#8221; is assumed.
  1008. </p>
  1009. <pre class="code">
  1010. Socket.getservbyport(80) #=&gt; &quot;www&quot;
  1011. Socket.getservbyport(514, &quot;tcp&quot;) #=&gt; &quot;shell&quot;
  1012. Socket.getservbyport(514, &quot;udp&quot;) #=&gt; &quot;syslog&quot;</pre>
  1013. </div>
  1014. </div>
  1015. <div class="tags">
  1016. </div><table class="source_code">
  1017. <tr>
  1018. <td>
  1019. <pre class="lines">
  1020. </pre>
  1021. </td>
  1022. <td>
  1023. <pre class="code"><span class="info file"># File 'ext/socket/socket.c'</span>
  1024. /*
  1025. * call-seq:
  1026. * Socket.getservbyport(port [, protocol_name]) =&gt; service
  1027. *
  1028. * Obtains the port number for _port_.
  1029. *
  1030. * If _protocol_name_ is not given, &quot;tcp&quot; is assumed.
  1031. *
  1032. * Socket.getservbyport(80) #=&gt; &quot;www&quot;
  1033. * Socket.getservbyport(514, &quot;tcp&quot;) #=&gt; &quot;shell&quot;
  1034. * Socket.getservbyport(514, &quot;udp&quot;) #=&gt; &quot;syslog&quot;
  1035. *
  1036. */
  1037. static VALUE
  1038. sock_s_getservbyport(int argc, VALUE *argv)
  1039. {
  1040. VALUE port, proto;
  1041. struct servent *sp;
  1042. long portnum;
  1043. const char *protoname = &quot;tcp&quot;;
  1044. rb_scan_args(argc, argv, &quot;11&quot;, &amp;port, &amp;proto);
  1045. portnum = NUM2LONG(port);
  1046. if (portnum != (uint16_t)portnum) {
  1047. const char *s = portnum &gt; 0 ? &quot;big&quot; : &quot;small&quot;;
  1048. rb_raise(rb_eRangeError, &quot;integer %ld too %s to convert into `int16_t'&quot;, portnum, s);
  1049. }
  1050. if (!NIL_P(proto)) protoname = StringValueCStr(proto);
  1051. sp = getservbyport((int)htons((uint16_t)portnum), protoname);
  1052. if (!sp) {
  1053. rb_raise(rb_eSocket, &quot;no such service for port %d/%s&quot;, (int)portnum, protoname);
  1054. }
  1055. return rb_tainted_str_new2(sp-&gt;s_name);
  1056. }</pre>
  1057. </td>
  1058. </tr>
  1059. </table>
  1060. </div>
  1061. <div class="method_details ">
  1062. <p class="signature " id="ip_address_list-class_method">
  1063. + (<tt><span class='object_link'><a href="Array.html" title="Array (class)">Array</a></span></tt>) <strong>ip_address_list</strong>
  1064. </p><div class="docstring">
  1065. <div class="discussion">
  1066. <p>
  1067. Returns local IP addresses as an array.
  1068. </p>
  1069. <p>
  1070. The array contains Addrinfo objects.
  1071. </p>
  1072. <pre class="code">
  1073. pp Socket.ip_address_list
  1074. #=&gt; [#&lt;Addrinfo: 127.0.0.1&gt;,
  1075. #&lt;Addrinfo: 192.168.0.128&gt;,
  1076. #&lt;Addrinfo: ::1&gt;,
  1077. ...]</pre>
  1078. </div>
  1079. </div>
  1080. <div class="tags">
  1081. <div class="docstring">
  1082. <div class="discussion">
  1083. </div>
  1084. </div>
  1085. <div class="tags">
  1086. <h3>Returns:</h3>
  1087. <ul class="return">
  1088. <li>
  1089. <span class='type'>(<tt><span class='object_link'><a href="Array.html" title="Array (class)">Array</a></span></tt>)</span>
  1090. </li>
  1091. </ul>
  1092. </div>
  1093. </div><table class="source_code">
  1094. <tr>
  1095. <td>
  1096. <pre class="lines">
  1097. </pre>
  1098. </td>
  1099. <td>
  1100. <pre class="code"><span class="info file"># File 'ext/socket/socket.c'</span>
  1101. /*
  1102. * call-seq:
  1103. * Socket.ip_address_list =&gt; array
  1104. *
  1105. * Returns local IP addresses as an array.
  1106. *
  1107. * The array contains Addrinfo objects.
  1108. *
  1109. * pp Socket.ip_address_list
  1110. * #=&gt; [#&lt;Addrinfo: 127.0.0.1&gt;,
  1111. * #&lt;Addrinfo: 192.168.0.128&gt;,
  1112. * #&lt;Addrinfo: ::1&gt;,
  1113. * ...]
  1114. *
  1115. */
  1116. static VALUE
  1117. socket_s_ip_address_list(VALUE self)
  1118. {
  1119. #if defined(HAVE_GETIFADDRS)
  1120. struct ifaddrs *ifp = NULL;
  1121. struct ifaddrs *p;
  1122. int ret;
  1123. VALUE list;
  1124. ret = getifaddrs(&amp;ifp);
  1125. if (ret == -1) {
  1126. rb_sys_fail(&quot;getifaddrs&quot;);
  1127. }
  1128. list = rb_ary_new();
  1129. for (p = ifp; p; p = p-&gt;ifa_next) {
  1130. if (p-&gt;ifa_addr != NULL &amp;&amp; IS_IP_FAMILY(p-&gt;ifa_addr-&gt;sa_family)) {
  1131. rb_ary_push(list, sockaddr_obj(p-&gt;ifa_addr));
  1132. }
  1133. }
  1134. freeifaddrs(ifp);
  1135. return list;
  1136. #elif defined(SIOCGLIFCONF) &amp;&amp; defined(SIOCGLIFNUM) &amp;&amp; !defined(__hpux)
  1137. /* Solaris if_tcp(7P) */
  1138. /* HP-UX has SIOCGLIFCONF too. But it uses different struct */
  1139. int fd = -1;
  1140. int ret;
  1141. struct lifnum ln;
  1142. struct lifconf lc;
  1143. char *reason = NULL;
  1144. int save_errno;
  1145. int i;
  1146. VALUE list = Qnil;
  1147. lc.lifc_buf = NULL;
  1148. fd = socket(AF_INET, SOCK_DGRAM, 0);
  1149. if (fd == -1)
  1150. rb_sys_fail(&quot;socket&quot;);
  1151. memset(&amp;ln, 0, sizeof(ln));
  1152. ln.lifn_family = AF_UNSPEC;
  1153. ret = ioctl(fd, SIOCGLIFNUM, &amp;ln);
  1154. if (ret == -1) {
  1155. reason = &quot;SIOCGLIFNUM&quot;;
  1156. goto finish;
  1157. }
  1158. memset(&amp;lc, 0, sizeof(lc));
  1159. lc.lifc_family = AF_UNSPEC;
  1160. lc.lifc_flags = 0;
  1161. lc.lifc_len = sizeof(struct lifreq) * ln.lifn_count;
  1162. lc.lifc_req = xmalloc(lc.lifc_len);
  1163. ret = ioctl(fd, SIOCGLIFCONF, &amp;lc);
  1164. if (ret == -1) {
  1165. reason = &quot;SIOCGLIFCONF&quot;;
  1166. goto finish;
  1167. }
  1168. list = rb_ary_new();
  1169. for (i = 0; i &lt; ln.lifn_count; i++) {
  1170. struct lifreq *req = &amp;lc.lifc_req[i];
  1171. if (IS_IP_FAMILY(req-&gt;lifr_addr.ss_family)) {
  1172. if (req-&gt;lifr_addr.ss_family == AF_INET6 &amp;&amp;
  1173. IN6_IS_ADDR_LINKLOCAL(&amp;((struct sockaddr_in6 *)(&amp;req-&gt;lifr_addr))-&gt;sin6_addr) &amp;&amp;
  1174. ((struct sockaddr_in6 *)(&amp;req-&gt;lifr_addr))-&gt;sin6_scope_id == 0) {
  1175. struct lifreq req2;
  1176. memcpy(req2.lifr_name, req-&gt;lifr_name, LIFNAMSIZ);
  1177. ret = ioctl(fd, SIOCGLIFINDEX, &amp;req2);
  1178. if (ret == -1) {
  1179. reason = &quot;SIOCGLIFINDEX&quot;;
  1180. goto finish;
  1181. }
  1182. ((struct sockaddr_in6 *)(&amp;req-&gt;lifr_addr))-&gt;sin6_scope_id = req2.lifr_index;
  1183. }
  1184. rb_ary_push(list, sockaddr_obj((struct sockaddr *)&amp;req-&gt;lifr_addr));
  1185. }
  1186. }
  1187. finish:
  1188. save_errno = errno;
  1189. if (lc.lifc_buf != NULL)
  1190. xfree(lc.lifc_req);
  1191. if (fd != -1)
  1192. close(fd);
  1193. errno = save_errno;
  1194. if (reason)
  1195. rb_sys_fail(reason);
  1196. return list;
  1197. #elif defined(SIOCGIFCONF)
  1198. int fd = -1;
  1199. int ret;
  1200. #define EXTRA_SPACE (sizeof(struct ifconf) + sizeof(struct sockaddr_storage))
  1201. char initbuf[4096+EXTRA_SPACE];
  1202. char *buf = initbuf;
  1203. int bufsize;
  1204. struct ifconf conf;
  1205. struct ifreq *req;
  1206. VALUE list = Qnil;
  1207. const char *reason = NULL;
  1208. int save_errno;
  1209. fd = socket(AF_INET, SOCK_DGRAM, 0);
  1210. if (fd == -1)
  1211. rb_sys_fail(&quot;socket&quot;);
  1212. bufsize = sizeof(initbuf);
  1213. buf = initbuf;
  1214. retry:
  1215. conf.ifc_len = bufsize;
  1216. conf.ifc_req = (struct ifreq *)buf;
  1217. /* fprintf(stderr, &quot;bufsize: %d\n&quot;, bufsize); */
  1218. ret = ioctl(fd, SIOCGIFCONF, &amp;conf);
  1219. if (ret == -1) {
  1220. reason = &quot;SIOCGIFCONF&quot;;
  1221. goto finish;
  1222. }
  1223. /* fprintf(stderr, &quot;conf.ifc_len: %d\n&quot;, conf.ifc_len); */
  1224. if (bufsize - EXTRA_SPACE &lt; conf.ifc_len) {
  1225. if (bufsize &lt; conf.ifc_len) {
  1226. /* NetBSD returns required size for all interfaces. */
  1227. bufsize = conf.ifc_len + EXTRA_SPACE;
  1228. }
  1229. else {
  1230. bufsize = bufsize &lt;&lt; 1;
  1231. }
  1232. if (buf == initbuf)
  1233. buf = NULL;
  1234. buf = xrealloc(buf, bufsize);
  1235. goto retry;
  1236. }
  1237. close(fd);
  1238. fd = -1;
  1239. list = rb_ary_new();
  1240. req = conf.ifc_req;
  1241. while ((char*)req &lt; (char*)conf.ifc_req + conf.ifc_len) {
  1242. struct sockaddr *addr = &amp;req-&gt;ifr_addr;
  1243. if (IS_IP_FAMILY(addr-&gt;sa_family)) {
  1244. rb_ary_push(list, sockaddr_obj(addr));
  1245. }
  1246. #ifdef HAVE_SA_LEN
  1247. # ifndef _SIZEOF_ADDR_IFREQ
  1248. # define _SIZEOF_ADDR_IFREQ(r) \
  1249. (sizeof(struct ifreq) + \
  1250. (sizeof(struct sockaddr) &lt; (r).ifr_addr.sa_len ? \
  1251. (r).ifr_addr.sa_len - sizeof(struct sockaddr) : \
  1252. 0))
  1253. # endif
  1254. req = (struct ifreq *)((char*)req + _SIZEOF_ADDR_IFREQ(*req));
  1255. #else
  1256. req = (struct ifreq *)((char*)req + sizeof(struct ifreq));
  1257. #endif
  1258. }
  1259. finish:
  1260. save_errno = errno;
  1261. if (buf != initbuf)
  1262. xfree(buf);
  1263. if (fd != -1)
  1264. close(fd);
  1265. errno = save_errno;
  1266. if (reason)
  1267. rb_sys_fail(reason);
  1268. return list;
  1269. #undef EXTRA_SPACE
  1270. #elif defined(_WIN32)
  1271. typedef struct ip_adapter_unicast_address_st {
  1272. unsigned LONG_LONG dummy0;
  1273. struct ip_adapter_unicast_address_st *Next;
  1274. struct {
  1275. struct sockaddr *lpSockaddr;
  1276. int iSockaddrLength;
  1277. } Address;
  1278. int dummy1;
  1279. int dummy2;
  1280. int dummy3;
  1281. long dummy4;
  1282. long dummy5;
  1283. long dummy6;
  1284. } ip_adapter_unicast_address_t;
  1285. typedef struct ip_adapter_anycast_address_st {
  1286. unsigned LONG_LONG dummy0;
  1287. struct ip_adapter_anycast_address_st *Next;
  1288. struct {
  1289. struct sockaddr *lpSockaddr;
  1290. int iSockaddrLength;
  1291. } Address;
  1292. } ip_adapter_anycast_address_t;
  1293. typedef struct ip_adapter_addresses_st {
  1294. unsigned LONG_LONG dummy0;
  1295. struct ip_adapter_addresses_st *Next;
  1296. void *dummy1;
  1297. ip_adapter_unicast_address_t *FirstUnicastAddress;
  1298. ip_adapter_anycast_address_t *FirstAnycastAddress;
  1299. void *dummy2;
  1300. void *dummy3;
  1301. void *dummy4;
  1302. void *dummy5;
  1303. void *dummy6;
  1304. BYTE dummy7[8];
  1305. DWORD dummy8;
  1306. DWORD dummy9;
  1307. DWORD dummy10;
  1308. DWORD IfType;
  1309. int OperStatus;
  1310. DWORD dummy12;
  1311. DWORD dummy13[16];
  1312. void *dummy14;
  1313. } ip_adapter_addresses_t;
  1314. typedef ULONG (WINAPI *GetAdaptersAddresses_t)(ULONG, ULONG, PVOID, ip_adapter_addresses_t *, PULONG);
  1315. HMODULE h;
  1316. GetAdaptersAddresses_t pGetAdaptersAddresses;
  1317. ULONG len;
  1318. DWORD ret;
  1319. ip_adapter_addresses_t *adapters;
  1320. VALUE list;
  1321. h = LoadLibrary(&quot;iphlpapi.dll&quot;);
  1322. if (!h)
  1323. rb_notimplement();
  1324. pGetAdaptersAddresses = (GetAdaptersAddresses_t)GetProcAddress(h, &quot;GetAdaptersAddresses&quot;);
  1325. if (!pGetAdaptersAddresses) {
  1326. FreeLibrary(h);
  1327. rb_notimplement();
  1328. }
  1329. ret = pGetAdaptersAddresses(AF_UNSPEC, 0, NULL, NULL, &amp;len);
  1330. if (ret != ERROR_SUCCESS &amp;&amp; ret != ERROR_BUFFER_OVERFLOW) {
  1331. errno = rb_w32_map_errno(ret);
  1332. FreeLibrary(h);
  1333. rb_sys_fail(&quot;GetAdaptersAddresses&quot;);
  1334. }
  1335. adapters = (ip_adapter_addresses_t *)ALLOCA_N(BYTE, len);
  1336. ret = pGetAdaptersAddresses(AF_UNSPEC, 0, NULL, adapters, &amp;len);
  1337. if (ret != ERROR_SUCCESS) {
  1338. errno = rb_w32_map_errno(ret);
  1339. FreeLibrary(h);
  1340. rb_sys_fail(&quot;GetAdaptersAddresses&quot;);
  1341. }
  1342. list = rb_ary_new();
  1343. for (; adapters; adapters = adapters-&gt;Next) {
  1344. ip_adapter_unicast_address_t *uni;
  1345. ip_adapter_anycast_address_t *any;
  1346. if (adapters-&gt;OperStatus != 1) /* 1 means IfOperStatusUp */
  1347. continue;
  1348. for (uni = adapters-&gt;FirstUnicastAddress; uni; uni = uni-&gt;Next) {
  1349. #ifndef INET6
  1350. if (uni-&gt;Address.lpSockaddr-&gt;sa_family == AF_INET)
  1351. #else
  1352. if (IS_IP_FAMILY(uni-&gt;Address.lpSockaddr-&gt;sa_family))
  1353. #endif
  1354. rb_ary_push(list, sockaddr_obj(uni-&gt;Address.lpSockaddr));
  1355. }
  1356. for (any = adapters-&gt;FirstAnycastAddress; any; any = any-&gt;Next) {
  1357. #ifndef INET6
  1358. if (any-&gt;Address.lpSockaddr-&gt;sa_family == AF_INET)
  1359. #else
  1360. if (IS_IP_FAMILY(any-&gt;Address.lpSockaddr-&gt;sa_family))
  1361. #endif
  1362. rb_ary_push(list, sockaddr_obj(any-&gt;Address.lpSockaddr));
  1363. }
  1364. }
  1365. FreeLibrary(h);
  1366. return list;
  1367. #endif
  1368. }</pre>
  1369. </td>
  1370. </tr>
  1371. </table>
  1372. </div>
  1373. <div class="method_details ">
  1374. <p class="signature " id="pack_sockaddr_in-class_method">
  1375. <span class="overload">+ (<tt><span class='object_link'><a href="Object.html" title="Object (class)">Object</a></span></tt>) <strong>sockaddr_in</strong>(port, host) </span>
  1376. <span class="overload">+ (<tt><span class='object_link'><a href="Object.html" title="Object (class)">Object</a></span></tt>) <strong>pack_sockaddr_in</strong>(port, host) </span>
  1377. </p><div class="docstring">
  1378. <div class="discussion">
  1379. <p>
  1380. Packs <em>port</em> and <em>host</em> as an AF_INET/AF_INET6 sockaddr
  1381. string.
  1382. </p>
  1383. <pre class="code">
  1384. Socket.sockaddr_in(80, &quot;127.0.0.1&quot;)
  1385. #=&gt; &quot;\x02\x00\x00P\x7F\x00\x00\x01\x00\x00\x00\x00\x00\x00\x00\x00&quot;
  1386. Socket.sockaddr_in(80, &quot;::1&quot;)
  1387. #=&gt; &quot;\n\x00\x00P\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x01\x00\x00\x00\x00&quot;</pre>
  1388. </div>
  1389. </div>
  1390. <div class="tags">
  1391. </div><table class="source_code">
  1392. <tr>
  1393. <td>
  1394. <pre class="lines">
  1395. </pre>
  1396. </td>
  1397. <td>
  1398. <pre class="code"><span class="info file"># File 'ext/socket/socket.c'</span>
  1399. /*
  1400. * call-seq:
  1401. * Socket.sockaddr_in(port, host) =&gt; sockaddr
  1402. * Socket.pack_sockaddr_in(port, host) =&gt; sockaddr
  1403. *
  1404. * Packs _port_ and _host_ as an AF_INET/AF_INET6 sockaddr string.
  1405. *
  1406. * Socket.sockaddr_in(80, &quot;127.0.0.1&quot;)
  1407. * #=&gt; &quot;\x02\x00\x00P\x7F\x00\x00\x01\x00\x00\x00\x00\x00\x00\x00\x00&quot;
  1408. *
  1409. * Socket.sockaddr_in(80, &quot;::1&quot;)
  1410. * #=&gt; &quot;\n\x00\x00P\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x01\x00\x00\x00\x00&quot;
  1411. *
  1412. */
  1413. static VALUE
  1414. sock_s_pack_sockaddr_in(VALUE self, VALUE port, VALUE host)
  1415. {
  1416. struct addrinfo *res = rsock_addrinfo(host, port, 0, 0);
  1417. VALUE addr = rb_str_new((char*)res-&gt;ai_addr, res-&gt;ai_addrlen);
  1418. freeaddrinfo(res);
  1419. OBJ_INFECT(addr, port);
  1420. OBJ_INFECT(addr, host);
  1421. return addr;
  1422. }</pre>
  1423. </td>
  1424. </tr>
  1425. </table>
  1426. </div>
  1427. <div class="method_details ">
  1428. <p class="signature " id="pack_sockaddr_un-class_method">
  1429. <span class="overload">+ (<tt><span class='object_link'><a href="Object.html" title="Object (class)">Object</a></span></tt>) <strong>sockaddr_un</strong>(path) </span>
  1430. <span class="overload">+ (<tt><span class='object_link'><a href="Object.html" title="Object (class)">Object</a></span></tt>) <strong>pack_sockaddr_un</strong>(path) </span>
  1431. </p><div class="docstring">
  1432. <div class="discussion">
  1433. <p>
  1434. Packs <em>path</em> as an AF_UNIX sockaddr string.
  1435. </p>
  1436. <pre class="code">
  1437. Socket.sockaddr_un(&quot;/tmp/sock&quot;) #=&gt; &quot;\x01\x00/tmp/sock\x00\x00...&quot;</pre>
  1438. </div>
  1439. </div>
  1440. <div class="tags">
  1441. </div><table class="source_code">
  1442. <tr>
  1443. <td>
  1444. <pre class="lines">
  1445. </pre>
  1446. </td>
  1447. <td>
  1448. <pre class="code"><span class="info file"># File 'ext/socket/socket.c'</span>
  1449. /*
  1450. * call-seq:
  1451. * Socket.sockaddr_un(path) =&gt; sockaddr
  1452. * Socket.pack_sockaddr_un(path) =&gt; sockaddr
  1453. *
  1454. * Packs _path_ as an AF_UNIX sockaddr string.
  1455. *
  1456. * Socket.sockaddr_un(&quot;/tmp/sock&quot;) #=&gt; &quot;\x01\x00/tmp/sock\x00\x00...&quot;
  1457. *
  1458. */
  1459. static VALUE
  1460. sock_s_pack_sockaddr_un(VALUE self, VALUE path)
  1461. {
  1462. struct sockaddr_un sockaddr;
  1463. char *sun_path;
  1464. VALUE addr;
  1465. MEMZERO(&amp;sockaddr, struct sockaddr_un, 1);
  1466. sockaddr.sun_family = AF_UNIX;
  1467. sun_path = StringValueCStr(path);
  1468. if (sizeof(sockaddr.sun_path) &lt;= strlen(sun_path)) {
  1469. rb_raise(rb_eArgError, &quot;too long unix socket path (max: %dbytes)&quot;,
  1470. (int)sizeof(sockaddr.sun_path)-1);
  1471. }
  1472. strncpy(sockaddr.sun_path, sun_path, sizeof(sockaddr.sun_path)-1);
  1473. addr = rb_str_new((char*)&amp;sockaddr, sizeof(sockaddr));
  1474. OBJ_INFECT(addr, path);
  1475. return addr;
  1476. }</pre>
  1477. </td>
  1478. </tr>
  1479. </table>
  1480. </div>
  1481. <div class="method_details ">
  1482. <p class="signature " id="pair-class_method">
  1483. <span class="overload">+ (<tt><span class='object_link'><a href="Array.html" title="Array (class)">Array</a></span></tt>) <strong>pair</strong>(domain, type, protocol) </span>
  1484. <span class="overload">+ (<tt><span class='object_link'><a href="Array.html" title="Array (class)">Array</a></span></tt>) <strong>socketpair</strong>(domain, type, protocol) </span>
  1485. </p><div class="docstring">
  1486. <div class="discussion">
  1487. <p>
  1488. Creates a pair of sockets connected each other.
  1489. </p>
  1490. <p>
  1491. <em>domain</em> should be a communications domain such as: :INET, :INET6,
  1492. :UNIX, etc.
  1493. </p>
  1494. <p>
  1495. <em>socktype</em> should be a socket type such as: :STREAM, :DGRAM, :RAW,
  1496. etc.
  1497. </p>
  1498. <p>
  1499. <em>protocol</em> should be a protocol defined in the domain. 0 is default
  1500. protocol for the domain.
  1501. </p>
  1502. <pre class="code">
  1503. s1, s2 = Socket.pair(:UNIX, :DGRAM, 0)
  1504. s1.send &quot;a&quot;, 0
  1505. s1.send &quot;b&quot;, 0
  1506. p s2.recv(10) #=&gt; &quot;a&quot;
  1507. p s2.recv(10) #=&gt; &quot;b&quot;</pre>
  1508. </div>
  1509. </div>
  1510. <div class="tags">
  1511. <h3>Overloads:</h3>
  1512. <ul class="overload">
  1513. <li class="overload_item">
  1514. <span class="signature">+ (<tt><span class='object_link'><a href="Array.html" title="Array (class)">Array</a></span></tt>) <strong>pair</strong>(domain, type, protocol) </span>
  1515. <div class="docstring">
  1516. <div class="discussion">
  1517. </div>
  1518. </div>
  1519. <div class="tags">
  1520. <h3>Returns:</h3>
  1521. <ul class="return">
  1522. <li>
  1523. <span class='type'>(<tt><span class='object_link'><a href="Array.html" title="Array (class)">Array</a></span></tt>)</span>
  1524. </li>
  1525. </ul>
  1526. </div>
  1527. </li>
  1528. <li class="overload_item">
  1529. <span class="signature">+ (<tt><span class='object_link'><a href="Array.html" title="Array (class)">Array</a></span></tt>) <strong>socketpair</strong>(domain, type, protocol) </span>
  1530. <div class="docstring">
  1531. <div class="discussion">
  1532. </div>
  1533. </div>
  1534. <div class="tags">
  1535. <h3>Returns:</h3>
  1536. <ul class="return">
  1537. <li>
  1538. <span class='type'>(<tt><span class='object_link'><a href="Array.html" title="Array (class)">Array</a></span></tt>)</span>
  1539. </li>
  1540. </ul>
  1541. </div>
  1542. </li>
  1543. </ul>
  1544. </div><table class="source_code">
  1545. <tr>
  1546. <td>
  1547. <pre class="lines">
  1548. </pre>
  1549. </td>
  1550. <td>
  1551. <pre class="code"><span class="info file"># File 'ext/socket/socket.c'</span>
  1552. /*
  1553. * call-seq:
  1554. * Socket.pair(domain, type, protocol) =&gt; [socket1, socket2]
  1555. * Socket.socketpair(domain, type, protocol) =&gt; [socket1, socket2]
  1556. *
  1557. * Creates a pair of sockets connected each other.
  1558. *
  1559. * _domain_ should be a communications domain such as: :INET, :INET6, :UNIX, etc.
  1560. *
  1561. * _socktype_ should be a socket type such as: :STREAM, :DGRAM, :RAW, etc.
  1562. *
  1563. * _protocol_ should be a protocol defined in the domain.
  1564. * 0 is default protocol for the domain.
  1565. *
  1566. * s1, s2 = Socket.pair(:UNIX, :DGRAM, 0)
  1567. * s1.send &quot;a&quot;, 0
  1568. * s1.send &quot;b&quot;, 0
  1569. * p s2.recv(10) #=&gt; &quot;a&quot;
  1570. * p s2.recv(10) #=&gt; &quot;b&quot;
  1571. *
  1572. */
  1573. VALUE
  1574. rsock_sock_s_socketpair(int argc, VALUE *argv, VALUE klass)
  1575. {
  1576. VALUE domain, type, protocol;
  1577. int d, t, p, sp[2];
  1578. int ret;
  1579. VALUE s1, s2, r;
  1580. rb_scan_args(argc, argv, &quot;21&quot;, &amp;domain, &amp;type, &amp;protocol);
  1581. if (NIL_P(protocol))
  1582. protocol = INT2FIX(0);
  1583. setup_domain_and_type(domain, &amp;d, type, &amp;t);
  1584. p = NUM2INT(protocol);
  1585. ret = socketpair(d, t, p, sp);
  1586. if (ret &lt; 0 &amp;&amp; (errno == EMFILE || errno == ENFILE)) {
  1587. rb_gc();
  1588. ret = socketpair(d, t, p, sp);
  1589. }
  1590. if (ret &lt; 0) {
  1591. rb_sys_fail(&quot;socketpair(2)&quot;);
  1592. }
  1593. s1 = rsock_init_sock(rb_obj_alloc(klass), sp[0]);
  1594. s2 = rsock_init_sock(rb_obj_alloc(klass), sp[1]);
  1595. r = rb_assoc_new(s1, s2);
  1596. if (rb_block_given_p()) {
  1597. return rb_ensure(pair_yield, r, io_close, s1);
  1598. }
  1599. return r;
  1600. }</pre>
  1601. </td>
  1602. </tr>
  1603. </table>
  1604. </div>
  1605. <div class="method_details ">
  1606. <p class="signature " id="sockaddr_in-class_method">
  1607. <span class="overload">+ (<tt><span class='object_link'><a href="Object.html" title="Object (class)">Object</a></span></tt>) <strong>sockaddr_in</strong>(port, host) </span>
  1608. <span class="overload">+ (<tt><span class='object_link'><a href="Object.html" title="Object (class)">Object</a></span></tt>) <strong>pack_sockaddr_in</strong>(port, host) </span>
  1609. </p><div class="docstring">
  1610. <div class="discussion">
  1611. <p>
  1612. Packs <em>port</em> and <em>host</em> as an AF_INET/AF_INET6 sockaddr
  1613. string.
  1614. </p>
  1615. <pre class="code">
  1616. Socket.sockaddr_in(80, &quot;127.0.0.1&quot;)
  1617. #=&gt; &quot;\x02\x00\x00P\x7F\x00\x00\x01\x00\x00\x00\x00\x00\x00\x00\x00&quot;
  1618. Socket.sockaddr_in(80, &quot;::1&quot;)
  1619. #=&gt; &quot;\n\x00\x00P\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x01\x00\x00\x00\x00&quot;</pre>
  1620. </div>
  1621. </div>
  1622. <div class="tags">
  1623. </div><table class="source_code">
  1624. <tr>
  1625. <td>
  1626. <pre class="lines">
  1627. </pre>
  1628. </td>
  1629. <td>
  1630. <pre class="code"><span class="info file"># File 'ext/socket/socket.c'</span>
  1631. /*
  1632. * call-seq:
  1633. * Socket.sockaddr_in(port, host) =&gt; sockaddr
  1634. * Socket.pack_sockaddr_in(port, host) =&gt; sockaddr
  1635. *
  1636. * Packs _port_ and _host_ as an AF_INET/AF_INET6 sockaddr string.
  1637. *
  1638. * Socket.sockaddr_in(80, &quot;127.0.0.1&quot;)
  1639. * #=&gt; &quot;\x02\x00\x00P\x7F\x00\x00\x01\x00\x00\x00\x00\x00\x00\x00\x00&quot;
  1640. *
  1641. * Socket.sockaddr_in(80, &quot;::1&quot;)
  1642. * #=&gt; &quot;\n\x00\x00P\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x01\x00\x00\x00\x00&quot;
  1643. *
  1644. */
  1645. static VALUE
  1646. sock_s_pack_sockaddr_in(VALUE self, VALUE port, VALUE host)
  1647. {
  1648. struct addrinfo *res = rsock_addrinfo(host, port, 0, 0);
  1649. VALUE addr = rb_str_new((char*)res-&gt;ai_addr, res-&gt;ai_addrlen);
  1650. freeaddrinfo(res);
  1651. OBJ_INFECT(addr, port);
  1652. OBJ_INFECT(addr, host);
  1653. return addr;
  1654. }</pre>
  1655. </td>
  1656. </tr>
  1657. </table>
  1658. </div>
  1659. <div class="method_details ">
  1660. <p class="signature " id="sockaddr_un-class_method">
  1661. <span class="overload">+ (<tt><span class='object_link'><a href="Object.html" title="Object (class)">Object</a></span></tt>) <strong>sockaddr_un</strong>(path) </span>
  1662. <span class="overload">+ (<tt><span class='object_link'><a href="Object.html" title="Object (class)">Object</a></span></tt>) <strong>pack_sockaddr_un</strong>(path) </span>
  1663. </p><div class="docstring">
  1664. <div class="discussion">
  1665. <p>
  1666. Packs <em>path</em> as an AF_UNIX sockaddr string.
  1667. </p>
  1668. <pre class="code">
  1669. Socket.sockaddr_un(&quot;/tmp/sock&quot;) #=&gt; &quot;\x01\x00/tmp/sock\x00\x00...&quot;</pre>
  1670. </div>
  1671. </div>
  1672. <div class="tags">
  1673. </div><table class="source_code">
  1674. <tr>
  1675. <td>
  1676. <pre class="lines">
  1677. </pre>
  1678. </td>
  1679. <td>
  1680. <pre class="code"><span class="info file"># File 'ext/socket/socket.c'</span>
  1681. /*
  1682. * call-seq:
  1683. * Socket.sockaddr_un(path) =&gt; sockaddr
  1684. * Socket.pack_sockaddr_un(path) =&gt; sockaddr
  1685. *
  1686. * Packs _path_ as an AF_UNIX sockaddr string.
  1687. *
  1688. * Socket.sockaddr_un(&quot;/tmp/sock&quot;) #=&gt; &quot;\x01\x00/tmp/sock\x00\x00...&quot;
  1689. *
  1690. */
  1691. static VALUE
  1692. sock_s_pack_sockaddr_un(VALUE self, VALUE path)
  1693. {
  1694. struct sockaddr_un sockaddr;
  1695. char *sun_path;
  1696. VALUE addr;
  1697. MEMZERO(&amp;sockaddr, struct sockaddr_un, 1);
  1698. sockaddr.sun_family = AF_UNIX;
  1699. sun_path = StringValueCStr(path);
  1700. if (sizeof(sockaddr.sun_path) &lt;= strlen(sun_path)) {
  1701. rb_raise(rb_eArgError, &quot;too long unix socket path (max: %dbytes)&quot;,
  1702. (int)sizeof(sockaddr.sun_path)-1);
  1703. }
  1704. strncpy(sockaddr.sun_path, sun_path, sizeof(sockaddr.sun_path)-1);
  1705. addr = rb_str_new((char*)&amp;sockaddr, sizeof(sockaddr));
  1706. OBJ_INFECT(addr, path);
  1707. return addr;
  1708. }</pre>
  1709. </td>
  1710. </tr>
  1711. </table>
  1712. </div>
  1713. <div class="method_details ">
  1714. <p class="signature " id="socketpair-class_method">
  1715. <span class="overload">+ (<tt><span class='object_link'><a href="Array.html" title="Array (class)">Array</a></span></tt>) <strong>pair</strong>(domain, type, protocol) </span>
  1716. <span class="overload">+ (<tt><span class='object_link'><a href="Array.html" title="Array (class)">Array</a></span></tt>) <strong>socketpair</strong>(domain, type, protocol) </span>
  1717. </p><div class="docstring">
  1718. <div class="discussion">
  1719. <p>
  1720. Creates a pair of sockets connected each other.
  1721. </p>
  1722. <p>
  1723. <em>domain</em> should be a communications domain such as: :INET, :INET6,
  1724. :UNIX, etc.
  1725. </p>
  1726. <p>
  1727. <em>socktype</em> should be a socket type such as: :STREAM, :DGRAM, :RAW,
  1728. etc.
  1729. </p>
  1730. <p>
  1731. <em>protocol</em> should be a protocol defined in the domain. 0 is default
  1732. protocol for the domain.
  1733. </p>
  1734. <pre class="code">
  1735. s1, s2 = Socket.pair(:UNIX, :DGRAM, 0)
  1736. s1.send &quot;a&quot;, 0
  1737. s1.send &quot;b&quot;, 0
  1738. p s2.recv(10) #=&gt; &quot;a&quot;
  1739. p s2.recv(10) #=&gt; &quot;b&quot;</pre>
  1740. </div>
  1741. </div>
  1742. <div class="tags">
  1743. <h3>Overloads:</h3>
  1744. <ul class="overload">
  1745. <li class="overload_item">
  1746. <span class="signature">+ (<tt><span class='object_link'><a href="Array.html" title="Array (class)">Array</a></span></tt>) <strong>pair</strong>(domain, type, protocol) </span>
  1747. <div class="docstring">
  1748. <div class="discussion">
  1749. </div>
  1750. </div>
  1751. <div class="tags">
  1752. <h3>Returns:</h3>
  1753. <ul class="return">
  1754. <li>
  1755. <span class='type'>(<tt><span class='object_link'><a href="Array.html" title="Array (class)">Array</a></span></tt>)</span>
  1756. </li>
  1757. </ul>
  1758. </div>
  1759. </li>
  1760. <li class="overload_item">
  1761. <span class="signature">+ (<tt><span class='object_link'><a href="Array.html" title="Array (class)">Array</a></span></tt>) <strong>socketpair</strong>(domain, type, protocol) </span>
  1762. <div class="docstring">
  1763. <div class="discussion">
  1764. </div>
  1765. </div>
  1766. <div class="tags">
  1767. <h3>Returns:</h3>
  1768. <ul class="return">
  1769. <li>
  1770. <span class='type'>(<tt><span class='object_link'><a href="Array.html" title="Array (class)">Array</a></span></tt>)</span>
  1771. </li>
  1772. </ul>
  1773. </div>
  1774. </li>
  1775. </ul>
  1776. </div><table class="source_code">
  1777. <tr>
  1778. <td>
  1779. <pre class="lines">
  1780. </pre>
  1781. </td>
  1782. <td>
  1783. <pre class="code"><span class="info file"># File 'ext/socket/socket.c'</span>
  1784. /*
  1785. * call-seq:
  1786. * Socket.pair(domain, type, protocol) =&gt; [socket1, socket2]
  1787. * Socket.socketpair(domain, type, protocol) =&gt; [socket1, socket2]
  1788. *
  1789. * Creates a pair of sockets connected each other.
  1790. *
  1791. * _domain_ should be a communications domain such as: :INET, :INET6, :UNIX, etc.
  1792. *
  1793. * _socktype_ should be a socket type such as: :STREAM, :DGRAM, :RAW, etc.
  1794. *
  1795. * _protocol_ should be a protocol defined in the domain.
  1796. * 0 is default protocol for the domain.
  1797. *
  1798. * s1, s2 = Socket.pair(:UNIX, :DGRAM, 0)
  1799. * s1.send &quot;a&quot;, 0
  1800. * s1.send &quot;b&quot;, 0
  1801. * p s2.recv(10) #=&gt; &quot;a&quot;
  1802. * p s2.recv(10) #=&gt; &quot;b&quot;
  1803. *
  1804. */
  1805. VALUE
  1806. rsock_sock_s_socketpair(int argc, VALUE *argv, VALUE klass)
  1807. {
  1808. VALUE domain, type, protocol;
  1809. int d, t, p, sp[2];
  1810. int ret;
  1811. VALUE s1, s2, r;
  1812. rb_scan_args(argc, argv, &quot;21&quot;, &amp;domain, &amp;type, &amp;protocol);
  1813. if (NIL_P(protocol))
  1814. protocol = INT2FIX(0);
  1815. setup_domain_and_type(domain, &amp;d, type, &amp;t);
  1816. p = NUM2INT(protocol);
  1817. ret = socketpair(d, t, p, sp);
  1818. if (ret &lt; 0 &amp;&amp; (errno == EMFILE || errno == ENFILE)) {
  1819. rb_gc();
  1820. ret = socketpair(d, t, p, sp);
  1821. }
  1822. if (ret &lt; 0) {
  1823. rb_sys_fail(&quot;socketpair(2)&quot;);
  1824. }
  1825. s1 = rsock_init_sock(rb_obj_alloc(klass), sp[0]);
  1826. s2 = rsock_init_sock(rb_obj_alloc(klass), sp[1]);
  1827. r = rb_assoc_new(s1, s2);
  1828. if (rb_block_given_p()) {
  1829. return rb_ensure(pair_yield, r, io_close, s1);
  1830. }
  1831. return r;
  1832. }</pre>
  1833. </td>
  1834. </tr>
  1835. </table>
  1836. </div>
  1837. <div class="method_details ">
  1838. <p class="signature " id="unpack_sockaddr_in-class_method">
  1839. + (<tt><span class='object_link'><a href="Array.html" title="Array (class)">Array</a></span></tt>) <strong>unpack_sockaddr_in</strong>(sockaddr)
  1840. </p><div class="docstring">
  1841. <div class="discussion">
  1842. <p>
  1843. Unpacks <em>sockaddr</em> into port and ip_address.
  1844. </p>
  1845. <p>
  1846. <em>sockaddr</em> should be a string or an addrinfo for AF_INET/AF_INET6.
  1847. </p>
  1848. <pre class="code">
  1849. sockaddr = Socket.sockaddr_in(80, &quot;127.0.0.1&quot;)
  1850. p sockaddr #=&gt; &quot;\x02\x00\x00P\x7F\x00\x00\x01\x00\x00\x00\x00\x00\x00\x00\x00&quot;
  1851. p Socket.unpack_sockaddr_in(sockaddr) #=&gt; [80, &quot;127.0.0.1&quot;]</pre>
  1852. </div>
  1853. </div>
  1854. <div class="tags">
  1855. <div class="docstring">
  1856. <div class="discussion">
  1857. </div>
  1858. </div>
  1859. <div class="tags">
  1860. <h3>Returns:</h3>
  1861. <ul class="return">
  1862. <li>
  1863. <span class='type'>(<tt><span class='object_link'><a href="Array.html" title="Array (class)">Array</a></span></tt>)</span>
  1864. </li>
  1865. </ul>
  1866. </div>
  1867. </div><table class="source_code">
  1868. <tr>
  1869. <td>
  1870. <pre class="lines">
  1871. </pre>
  1872. </td>
  1873. <td>
  1874. <pre class="code"><span class="info file"># File 'ext/socket/socket.c'</span>
  1875. /*
  1876. * call-seq:
  1877. * Socket.unpack_sockaddr_in(sockaddr) =&gt; [port, ip_address]
  1878. *
  1879. * Unpacks _sockaddr_ into port and ip_address.
  1880. *
  1881. * _sockaddr_ should be a string or an addrinfo for AF_INET/AF_INET6.
  1882. *
  1883. * sockaddr = Socket.sockaddr_in(80, &quot;127.0.0.1&quot;)
  1884. * p sockaddr #=&gt; &quot;\x02\x00\x00P\x7F\x00\x00\x01\x00\x00\x00\x00\x00\x00\x00\x00&quot;
  1885. * p Socket.unpack_sockaddr_in(sockaddr) #=&gt; [80, &quot;127.0.0.1&quot;]
  1886. *
  1887. */
  1888. static VALUE
  1889. sock_s_unpack_sockaddr_in(VALUE self, VALUE addr)
  1890. {
  1891. struct sockaddr_in * sockaddr;
  1892. VALUE host;
  1893. sockaddr = (struct sockaddr_in*)SockAddrStringValuePtr(addr);
  1894. if (RSTRING_LEN(addr) &lt;
  1895. (char*)&amp;((struct sockaddr *)sockaddr)-&gt;sa_family +
  1896. sizeof(((struct sockaddr *)sockaddr)-&gt;sa_family) -
  1897. (char*)sockaddr)
  1898. rb_raise(rb_eArgError, &quot;too short sockaddr&quot;);
  1899. if (((struct sockaddr *)sockaddr)-&gt;sa_family != AF_INET
  1900. #ifdef INET6
  1901. &amp;&amp; ((struct sockaddr *)sockaddr)-&gt;sa_family != AF_INET6
  1902. #endif
  1903. ) {
  1904. #ifdef INET6
  1905. rb_raise(rb_eArgError, &quot;not an AF_INET/AF_INET6 sockaddr&quot;);
  1906. #else
  1907. rb_raise(rb_eArgError, &quot;not an AF_INET sockaddr&quot;);
  1908. #endif
  1909. }
  1910. host = rsock_make_ipaddr((struct sockaddr*)sockaddr);
  1911. OBJ_INFECT(host, addr);
  1912. return rb_assoc_new(INT2NUM(ntohs(sockaddr-&gt;sin_port)), host);
  1913. }</pre>
  1914. </td>
  1915. </tr>
  1916. </table>
  1917. </div>
  1918. <div class="method_details ">
  1919. <p class="signature " id="unpack_sockaddr_un-class_method">
  1920. + (<tt><span class='object_link'><a href="Object.html" title="Object (class)">Object</a></span></tt>) <strong>unpack_sockaddr_un</strong>(sockaddr)
  1921. </p><div class="docstring">
  1922. <div class="discussion">
  1923. <p>
  1924. Unpacks <em>sockaddr</em> into path.
  1925. </p>
  1926. <p>
  1927. <em>sockaddr</em> should be a string or an addrinfo for AF_UNIX.
  1928. </p>
  1929. <pre class="code">
  1930. sockaddr = Socket.sockaddr_un(&quot;/tmp/sock&quot;)
  1931. p Socket.unpack_sockaddr_un(sockaddr) #=&gt; &quot;/tmp/sock&quot;</pre>
  1932. </div>
  1933. </div>
  1934. <div class="tags">
  1935. </div><table class="source_code">
  1936. <tr>
  1937. <td>
  1938. <pre class="lines">
  1939. </pre>
  1940. </td>
  1941. <td>
  1942. <pre class="code"><span class="info file"># File 'ext/socket/socket.c'</span>
  1943. /*
  1944. * call-seq:
  1945. * Socket.unpack_sockaddr_un(sockaddr) =&gt; path
  1946. *
  1947. * Unpacks _sockaddr_ into path.
  1948. *
  1949. * _sockaddr_ should be a string or an addrinfo for AF_UNIX.
  1950. *
  1951. * sockaddr = Socket.sockaddr_un(&quot;/tmp/sock&quot;)
  1952. * p Socket.unpack_sockaddr_un(sockaddr) #=&gt; &quot;/tmp/sock&quot;
  1953. *
  1954. */
  1955. static VALUE
  1956. sock_s_unpack_sockaddr_un(VALUE self, VALUE addr)
  1957. {
  1958. struct sockaddr_un * sockaddr;
  1959. const char *sun_path;
  1960. VALUE path;
  1961. sockaddr = (struct sockaddr_un*)SockAddrStringValuePtr(addr);
  1962. if (RSTRING_LEN(addr) &lt;
  1963. (char*)&amp;((struct sockaddr *)sockaddr)-&gt;sa_family +
  1964. sizeof(((struct sockaddr *)sockaddr)-&gt;sa_family) -
  1965. (char*)sockaddr)
  1966. rb_raise(rb_eArgError, &quot;too short sockaddr&quot;);
  1967. if (((struct sockaddr *)sockaddr)-&gt;sa_family != AF_UNIX) {
  1968. rb_raise(rb_eArgError, &quot;not an AF_UNIX sockaddr&quot;);
  1969. }
  1970. if (sizeof(struct sockaddr_un) &lt; (size_t)RSTRING_LEN(addr)) {
  1971. rb_raise(rb_eTypeError, &quot;too long sockaddr_un - %ld longer than %d&quot;,
  1972. RSTRING_LEN(addr), (int)sizeof(struct sockaddr_un));
  1973. }
  1974. sun_path = rsock_unixpath(sockaddr, RSTRING_LENINT(addr));
  1975. if (sizeof(struct sockaddr_un) == RSTRING_LEN(addr) &amp;&amp;
  1976. sun_path == sockaddr-&gt;sun_path &amp;&amp;
  1977. sun_path + strlen(sun_path) == RSTRING_PTR(addr) + RSTRING_LEN(addr)) {
  1978. rb_raise(rb_eArgError, &quot;sockaddr_un.sun_path not NUL terminated&quot;);
  1979. }
  1980. path = rb_str_new2(sun_path);
  1981. OBJ_INFECT(path, addr);
  1982. return path;
  1983. }</pre>
  1984. </td>
  1985. </tr>
  1986. </table>
  1987. </div>
  1988. </div>
  1989. <div id="instance_method_details" class="method_details_list">
  1990. <h2>Instance Method Details</h2>
  1991. <div class="method_details first">
  1992. <p class="signature first" id="accept-instance_method">
  1993. - (<tt><span class='object_link'><a href="Array.html" title="Array (class)">Array</a></span></tt>) <strong>accept</strong>
  1994. </p><div class="docstring">
  1995. <div class="discussion">
  1996. <p>
  1997. Accepts a next connection. Returns a new Socket object and Addrinfo object.
  1998. </p>
  1999. <pre class="code">
  2000. serv = Socket.new(:INET, :STREAM, 0)
  2001. serv.listen(5)
  2002. c = Socket.new(:INET, :STREAM, 0)
  2003. c.connect(serv.connect_address)
  2004. p serv.accept #=&gt; [#&lt;Socket:fd 6&gt;, #&lt;Addrinfo: 127.0.0.1:48555 TCP&gt;]</pre>
  2005. </div>
  2006. </div>
  2007. <div class="tags">
  2008. <div class="docstring">
  2009. <div class="discussion">
  2010. </div>
  2011. </div>
  2012. <div class="tags">
  2013. <h3>Returns:</h3>
  2014. <ul class="return">
  2015. <li>
  2016. <span class='type'>(<tt><span class='object_link'><a href="Array.html" title="Array (class)">Array</a></span></tt>)</span>
  2017. </li>
  2018. </ul>
  2019. </div>
  2020. </div><table class="source_code">
  2021. <tr>
  2022. <td>
  2023. <pre class="lines">
  2024. </pre>
  2025. </td>
  2026. <td>
  2027. <pre class="code"><span class="info file"># File 'ext/socket/socket.c'</span>
  2028. /*
  2029. * call-seq:
  2030. * socket.accept =&gt; [client_socket, client_addrinfo]
  2031. *
  2032. * Accepts a next connection.
  2033. * Returns a new Socket object and Addrinfo object.
  2034. *
  2035. * serv = Socket.new(:INET, :STREAM, 0)
  2036. * serv.listen(5)
  2037. * c = Socket.new(:INET, :STREAM, 0)
  2038. * c.connect(serv.connect_address)
  2039. * p serv.accept #=&gt; [#&lt;Socket:fd 6&gt;, #&lt;Addrinfo: 127.0.0.1:48555 TCP&gt;]
  2040. *
  2041. */
  2042. static VALUE
  2043. sock_accept(VALUE sock)
  2044. {
  2045. rb_io_t *fptr;
  2046. VALUE sock2;
  2047. struct sockaddr_storage buf;
  2048. socklen_t len = (socklen_t)sizeof buf;
  2049. GetOpenFile(sock, fptr);
  2050. sock2 = rsock_s_accept(rb_cSocket,fptr-&gt;fd,(struct sockaddr*)&amp;buf,&amp;len);
  2051. return rb_assoc_new(sock2, rsock_io_socket_addrinfo(sock2, (struct sockaddr*)&amp;buf, len));
  2052. }</pre>
  2053. </td>
  2054. </tr>
  2055. </table>
  2056. </div>
  2057. <div class="method_details ">
  2058. <p class="signature " id="accept_nonblock-instance_method">
  2059. - (<tt><span class='object_link'><a href="Array.html" title="Array (class)">Array</a></span></tt>) <strong>accept_nonblock</strong>
  2060. </p><div class="docstring">
  2061. <div class="discussion">
  2062. <p>
  2063. Accepts an incoming connection using accept(2) after O_NONBLOCK is set for
  2064. the underlying file descriptor. It returns an array containing the accepted
  2065. socket for the incoming connection, <em>client_socket</em>, and an
  2066. Addrinfo, <em>client_addrinfo</em>.
  2067. </p>
  2068. <h3>Example</h3>
  2069. <pre class="code">
  2070. # In one script, start this first
  2071. require 'socket'
  2072. include Socket::Constants
  2073. socket = Socket.new(AF_INET, SOCK_STREAM, 0)
  2074. sockaddr = Socket.sockaddr_in(2200, 'localhost')
  2075. socket.bind(sockaddr)
  2076. socket.listen(5)
  2077. begin # emulate blocking accept
  2078. client_socket, client_addrinfo = socket.accept_nonblock
  2079. rescue IO::WaitReadable, Errno::EINTR
  2080. IO.select([socket])
  2081. retry
  2082. end
  2083. puts &quot;The client said, '#{client_socket.readline.chomp}'&quot;
  2084. client_socket.puts &quot;Hello from script one!&quot;
  2085. socket.close
  2086. # In another script, start this second
  2087. require 'socket'
  2088. include Socket::Constants
  2089. socket = Socket.new(AF_INET, SOCK_STREAM, 0)
  2090. sockaddr = Socket.sockaddr_in(2200, 'localhost')
  2091. socket.connect(sockaddr)
  2092. socket.puts &quot;Hello from script 2.&quot;
  2093. puts &quot;The server said, '#{socket.readline.chomp}'&quot;
  2094. socket.close
  2095. </pre>
  2096. <p>
  2097. Refer to Socket#accept for the exceptions that may be thrown if the call to
  2098. <em>accept_nonblock</em> fails.
  2099. </p>
  2100. <p>
  2101. Socket#accept_nonblock may raise any error corresponding to accept(2)
  2102. failure, including Errno::EWOULDBLOCK.
  2103. </p>
  2104. <p>
  2105. If the exception is Errno::EWOULDBLOCK, Errno::AGAIN, Errno::ECONNABORTED
  2106. or Errno::EPROTO, it is extended by IO::WaitReadable. So IO::WaitReadable
  2107. can be used to rescue the exceptions for retrying accept_nonblock.
  2108. </p>
  2109. <h3>See</h3>
  2110. <ul>
  2111. <li><p>
  2112. Socket#accept
  2113. </p>
  2114. </li>
  2115. </ul>
  2116. </div>
  2117. </div>
  2118. <div class="tags">
  2119. <div class="docstring">
  2120. <div class="discussion">
  2121. </div>
  2122. </div>
  2123. <div class="tags">
  2124. <h3>Returns:</h3>
  2125. <ul class="return">
  2126. <li>
  2127. <span class='type'>(<tt><span class='object_link'><a href="Array.html" title="Array (class)">Array</a></span></tt>)</span>
  2128. </li>
  2129. </ul>
  2130. </div>
  2131. </div><table class="source_code">
  2132. <tr>
  2133. <td>
  2134. <pre class="lines">
  2135. </pre>
  2136. </td>
  2137. <td>
  2138. <pre class="code"><span class="info file"># File 'ext/socket/socket.c'</span>
  2139. /*
  2140. * call-seq:
  2141. * socket.accept_nonblock =&gt; [client_socket, client_addrinfo]
  2142. *
  2143. * Accepts an incoming connection using accept(2) after
  2144. * O_NONBLOCK is set for the underlying file descriptor.
  2145. * It returns an array containing the accepted socket
  2146. * for the incoming connection, _client_socket_,
  2147. * and an Addrinfo, _client_addrinfo_.
  2148. *
  2149. * === Example
  2150. * # In one script, start this first
  2151. * require 'socket'
  2152. * include Socket::Constants
  2153. * socket = Socket.new(AF_INET, SOCK_STREAM, 0)
  2154. * sockaddr = Socket.sockaddr_in(2200, 'localhost')
  2155. * socket.bind(sockaddr)
  2156. * socket.listen(5)
  2157. * begin # emulate blocking accept
  2158. * client_socket, client_addrinfo = socket.accept_nonblock
  2159. * rescue IO::WaitReadable, Errno::EINTR
  2160. * IO.select([socket])
  2161. * retry
  2162. * end
  2163. * puts &quot;The client said, '#{client_socket.readline.chomp}'&quot;
  2164. * client_socket.puts &quot;Hello from script one!&quot;
  2165. * socket.close
  2166. *
  2167. * # In another script, start this second
  2168. * require 'socket'
  2169. * include Socket::Constants
  2170. * socket = Socket.new(AF_INET, SOCK_STREAM, 0)
  2171. * sockaddr = Socket.sockaddr_in(2200, 'localhost')
  2172. * socket.connect(sockaddr)
  2173. * socket.puts &quot;Hello from script 2.&quot;
  2174. * puts &quot;The server said, '#{socket.readline.chomp}'&quot;
  2175. * socket.close
  2176. *
  2177. * Refer to Socket#accept for the exceptions that may be thrown if the call
  2178. * to _accept_nonblock_ fails.
  2179. *
  2180. * Socket#accept_nonblock may raise any error corresponding to accept(2) failure,
  2181. * including Errno::EWOULDBLOCK.
  2182. *
  2183. * If the exception is Errno::EWOULDBLOCK, Errno::AGAIN, Errno::ECONNABORTED or Errno::EPROTO,
  2184. * it is extended by IO::WaitReadable.
  2185. * So IO::WaitReadable can be used to rescue the exceptions for retrying accept_nonblock.
  2186. *
  2187. * === See
  2188. * * Socket#accept
  2189. */
  2190. static VALUE
  2191. sock_accept_nonblock(VALUE sock)
  2192. {
  2193. rb_io_t *fptr;
  2194. VALUE sock2;
  2195. struct sockaddr_storage buf;
  2196. socklen_t len = (socklen_t)sizeof buf;
  2197. GetOpenFile(sock, fptr);
  2198. sock2 = rsock_s_accept_nonblock(rb_cSocket, fptr, (struct sockaddr *)&amp;buf, &amp;len);
  2199. return rb_assoc_new(sock2, rsock_io_socket_addrinfo(sock2, (struct sockaddr*)&amp;buf, len));
  2200. }</pre>
  2201. </td>
  2202. </tr>
  2203. </table>
  2204. </div>
  2205. <div class="method_details ">
  2206. <p class="signature " id="bind-instance_method">
  2207. - (<tt>0</tt>) <strong>bind</strong>(local_sockaddr)
  2208. </p><div class="docstring">
  2209. <div class="discussion">
  2210. <p>
  2211. Binds to the given local address.
  2212. </p>
  2213. <h3>Parameter</h3>
  2214. <ul>
  2215. <li><p>
  2216. <tt>local_sockaddr</tt> - the <tt>struct</tt> sockaddr contained in a
  2217. string or an Addrinfo object
  2218. </p>
  2219. </li>
  2220. </ul>
  2221. <h3>Example</h3>
  2222. <pre class="code">
  2223. require 'socket'
  2224. # use Addrinfo
  2225. socket = Socket.new(:INET, :STREAM, 0)
  2226. socket.bind(Addrinfo.tcp(&quot;127.0.0.1&quot;, 2222))
  2227. p socket.local_address #=&gt; #&lt;Addrinfo: 127.0.0.1:2222 TCP&gt;
  2228. # use struct sockaddr
  2229. include Socket::Constants
  2230. socket = Socket.new( AF_INET, SOCK_STREAM, 0 )
  2231. sockaddr = Socket.pack_sockaddr_in( 2200, 'localhost' )
  2232. socket.bind( sockaddr )
  2233. </pre>
  2234. <h3>Unix-based Exceptions</h3>
  2235. <p>
  2236. On unix-based based systems the following system exceptions may be raised
  2237. if the call to <em>bind</em> fails:
  2238. </p>
  2239. <ul>
  2240. <li><p>
  2241. Errno::EACCES - the specified <em>sockaddr</em> is protected and the
  2242. current user does not have permission to bind to it
  2243. </p>
  2244. </li>
  2245. <li><p>
  2246. Errno::EADDRINUSE - the specified <em>sockaddr</em> is already in use
  2247. </p>
  2248. </li>
  2249. <li><p>
  2250. Errno::EADDRNOTAVAIL - the specified <em>sockaddr</em> is not available
  2251. from the local machine
  2252. </p>
  2253. </li>
  2254. <li><p>
  2255. Errno::EAFNOSUPPORT - the specified <em>sockaddr</em> is not a valid
  2256. address for the family of the calling <tt>socket</tt>
  2257. </p>
  2258. </li>
  2259. <li><p>
  2260. Errno::EBADF - the <em>sockaddr</em> specified is not a valid file
  2261. descriptor
  2262. </p>
  2263. </li>
  2264. <li><p>
  2265. Errno::EFAULT - the <em>sockaddr</em> argument cannot be accessed
  2266. </p>
  2267. </li>
  2268. <li><p>
  2269. Errno::EINVAL - the <tt>socket</tt> is already bound to an address, and the
  2270. protocol does not support binding to the new <em>sockaddr</em> or the
  2271. <tt>socket</tt> has been shut down.
  2272. </p>
  2273. </li>
  2274. <li><p>
  2275. Errno::EINVAL - the address length is not a valid length for the address
  2276. family
  2277. </p>
  2278. </li>
  2279. <li><p>
  2280. Errno::ENAMETOOLONG - the pathname resolved had a length which exceeded
  2281. PATH_MAX
  2282. </p>
  2283. </li>
  2284. <li><p>
  2285. Errno::ENOBUFS - no buffer space is available
  2286. </p>
  2287. </li>
  2288. <li><p>
  2289. Errno::ENOSR - there were insufficient STREAMS resources available to
  2290. complete the operation
  2291. </p>
  2292. </li>
  2293. <li><p>
  2294. Errno::ENOTSOCK - the <tt>socket</tt> does not refer to a socket
  2295. </p>
  2296. </li>
  2297. <li><p>
  2298. Errno::EOPNOTSUPP - the socket type of the <tt>socket</tt> does not support
  2299. binding to an address
  2300. </p>
  2301. </li>
  2302. </ul>
  2303. <p>
  2304. On unix-based based systems if the address family of the calling
  2305. <tt>socket</tt> is Socket::AF_UNIX the follow exceptions may be raised if
  2306. the call to <em>bind</em> fails:
  2307. </p>
  2308. <ul>
  2309. <li><p>
  2310. Errno::EACCES - search permission is denied for a component of the prefix
  2311. path or write access to the <tt>socket</tt> is denied
  2312. </p>
  2313. </li>
  2314. <li><p>
  2315. Errno::EDESTADDRREQ - the <em>sockaddr</em> argument is a null pointer
  2316. </p>
  2317. </li>
  2318. <li><p>
  2319. Errno::EISDIR - same as Errno::EDESTADDRREQ
  2320. </p>
  2321. </li>
  2322. <li><p>
  2323. Errno::EIO - an i/o error occurred
  2324. </p>
  2325. </li>
  2326. <li><p>
  2327. Errno::ELOOP - too many symbolic links were encountered in translating the
  2328. pathname in <em>sockaddr</em>
  2329. </p>
  2330. </li>
  2331. <li><p>
  2332. Errno::ENAMETOOLLONG - a component of a pathname exceeded NAME_MAX
  2333. characters, or an entire pathname exceeded PATH_MAX characters
  2334. </p>
  2335. </li>
  2336. <li><p>
  2337. Errno::ENOENT - a component of the pathname does not name an existing file
  2338. or the pathname is an empty string
  2339. </p>
  2340. </li>
  2341. <li><p>
  2342. Errno::ENOTDIR - a component of the path prefix of the pathname in
  2343. <em>sockaddr</em> is not a directory
  2344. </p>
  2345. </li>
  2346. <li><p>
  2347. Errno::EROFS - the name would reside on a read only filesystem
  2348. </p>
  2349. </li>
  2350. </ul>
  2351. <h3>Windows Exceptions</h3>
  2352. <p>
  2353. On Windows systems the following system exceptions may be raised if the
  2354. call to <em>bind</em> fails:
  2355. </p>
  2356. <ul>
  2357. <li><p>
  2358. Errno::ENETDOWN&#8212; the network is down
  2359. </p>
  2360. </li>
  2361. <li><p>
  2362. Errno::EACCES - the attempt to connect the datagram socket to the broadcast
  2363. address failed
  2364. </p>
  2365. </li>
  2366. <li><p>
  2367. Errno::EADDRINUSE - the socket&#8217;s local address is already in use
  2368. </p>
  2369. </li>
  2370. <li><p>
  2371. Errno::EADDRNOTAVAIL - the specified address is not a valid address for
  2372. this computer
  2373. </p>
  2374. </li>
  2375. <li><p>
  2376. Errno::EFAULT - the socket&#8217;s internal address or address length
  2377. parameter is too small or is not a valid part of the user space addressed
  2378. </p>
  2379. </li>
  2380. <li><p>
  2381. Errno::EINVAL - the <tt>socket</tt> is already bound to an address
  2382. </p>
  2383. </li>
  2384. <li><p>
  2385. Errno::ENOBUFS - no buffer space is available
  2386. </p>
  2387. </li>
  2388. <li><p>
  2389. Errno::ENOTSOCK - the <tt>socket</tt> argument does not refer to a socket
  2390. </p>
  2391. </li>
  2392. </ul>
  2393. <h3>See</h3>
  2394. <ul>
  2395. <li><p>
  2396. bind manual pages on unix-based systems
  2397. </p>
  2398. </li>
  2399. <li><p>
  2400. bind function in Microsoft&#8217;s Winsock functions reference
  2401. </p>
  2402. </li>
  2403. </ul>
  2404. </div>
  2405. </div>
  2406. <div class="tags">
  2407. <div class="docstring">
  2408. <div class="discussion">
  2409. </div>
  2410. </div>
  2411. <div class="tags">
  2412. <h3>Returns:</h3>
  2413. <ul class="return">
  2414. <li>
  2415. <span class='type'>(<tt>0</tt>)</span>
  2416. </li>
  2417. </ul>
  2418. </div>
  2419. </div><table class="source_code">
  2420. <tr>
  2421. <td>
  2422. <pre class="lines">
  2423. </pre>
  2424. </td>
  2425. <td>
  2426. <pre class="code"><span class="info file"># File 'ext/socket/socket.c'</span>
  2427. /*
  2428. * call-seq:
  2429. * socket.bind(local_sockaddr) =&gt; 0
  2430. *
  2431. * Binds to the given local address.
  2432. *
  2433. * === Parameter
  2434. * * +local_sockaddr+ - the +struct+ sockaddr contained in a string or an Addrinfo object
  2435. *
  2436. * === Example
  2437. * require 'socket'
  2438. *
  2439. * # use Addrinfo
  2440. * socket = Socket.new(:INET, :STREAM, 0)
  2441. * socket.bind(Addrinfo.tcp(&quot;127.0.0.1&quot;, 2222))
  2442. * p socket.local_address #=&gt; #&lt;Addrinfo: 127.0.0.1:2222 TCP&gt;
  2443. *
  2444. * # use struct sockaddr
  2445. * include Socket::Constants
  2446. * socket = Socket.new( AF_INET, SOCK_STREAM, 0 )
  2447. * sockaddr = Socket.pack_sockaddr_in( 2200, 'localhost' )
  2448. * socket.bind( sockaddr )
  2449. *
  2450. * === Unix-based Exceptions
  2451. * On unix-based based systems the following system exceptions may be raised if
  2452. * the call to _bind_ fails:
  2453. * * Errno::EACCES - the specified _sockaddr_ is protected and the current
  2454. * user does not have permission to bind to it
  2455. * * Errno::EADDRINUSE - the specified _sockaddr_ is already in use
  2456. * * Errno::EADDRNOTAVAIL - the specified _sockaddr_ is not available from the
  2457. * local machine
  2458. * * Errno::EAFNOSUPPORT - the specified _sockaddr_ is not a valid address for
  2459. * the family of the calling +socket+
  2460. * * Errno::EBADF - the _sockaddr_ specified is not a valid file descriptor
  2461. * * Errno::EFAULT - the _sockaddr_ argument cannot be accessed
  2462. * * Errno::EINVAL - the +socket+ is already bound to an address, and the
  2463. * protocol does not support binding to the new _sockaddr_ or the +socket+
  2464. * has been shut down.
  2465. * * Errno::EINVAL - the address length is not a valid length for the address
  2466. * family
  2467. * * Errno::ENAMETOOLONG - the pathname resolved had a length which exceeded
  2468. * PATH_MAX
  2469. * * Errno::ENOBUFS - no buffer space is available
  2470. * * Errno::ENOSR - there were insufficient STREAMS resources available to
  2471. * complete the operation
  2472. * * Errno::ENOTSOCK - the +socket+ does not refer to a socket
  2473. * * Errno::EOPNOTSUPP - the socket type of the +socket+ does not support
  2474. * binding to an address
  2475. *
  2476. * On unix-based based systems if the address family of the calling +socket+ is
  2477. * Socket::AF_UNIX the follow exceptions may be raised if the call to _bind_
  2478. * fails:
  2479. * * Errno::EACCES - search permission is denied for a component of the prefix
  2480. * path or write access to the +socket+ is denied
  2481. * * Errno::EDESTADDRREQ - the _sockaddr_ argument is a null pointer
  2482. * * Errno::EISDIR - same as Errno::EDESTADDRREQ
  2483. * * Errno::EIO - an i/o error occurred
  2484. * * Errno::ELOOP - too many symbolic links were encountered in translating
  2485. * the pathname in _sockaddr_
  2486. * * Errno::ENAMETOOLLONG - a component of a pathname exceeded NAME_MAX
  2487. * characters, or an entire pathname exceeded PATH_MAX characters
  2488. * * Errno::ENOENT - a component of the pathname does not name an existing file
  2489. * or the pathname is an empty string
  2490. * * Errno::ENOTDIR - a component of the path prefix of the pathname in _sockaddr_
  2491. * is not a directory
  2492. * * Errno::EROFS - the name would reside on a read only filesystem
  2493. *
  2494. * === Windows Exceptions
  2495. * On Windows systems the following system exceptions may be raised if
  2496. * the call to _bind_ fails:
  2497. * * Errno::ENETDOWN-- the network is down
  2498. * * Errno::EACCES - the attempt to connect the datagram socket to the
  2499. * broadcast address failed
  2500. * * Errno::EADDRINUSE - the socket's local address is already in use
  2501. * * Errno::EADDRNOTAVAIL - the specified address is not a valid address for this
  2502. * computer
  2503. * * Errno::EFAULT - the socket's internal address or address length parameter
  2504. * is too small or is not a valid part of the user space addressed
  2505. * * Errno::EINVAL - the +socket+ is already bound to an address
  2506. * * Errno::ENOBUFS - no buffer space is available
  2507. * * Errno::ENOTSOCK - the +socket+ argument does not refer to a socket
  2508. *
  2509. * === See
  2510. * * bind manual pages on unix-based systems
  2511. * * bind function in Microsoft's Winsock functions reference
  2512. */
  2513. static VALUE
  2514. sock_bind(VALUE sock, VALUE addr)
  2515. {
  2516. rb_io_t *fptr;
  2517. SockAddrStringValue(addr);
  2518. GetOpenFile(sock, fptr);
  2519. if (bind(fptr-&gt;fd, (struct sockaddr*)RSTRING_PTR(addr), RSTRING_LENINT(addr)) &lt; 0)
  2520. rb_sys_fail(&quot;bind(2)&quot;);
  2521. return INT2FIX(0);
  2522. }</pre>
  2523. </td>
  2524. </tr>
  2525. </table>
  2526. </div>
  2527. <div class="method_details ">
  2528. <p class="signature " id="connect-instance_method">
  2529. - (<tt>0</tt>) <strong>connect</strong>(remote_sockaddr)
  2530. </p><div class="docstring">
  2531. <div class="discussion">
  2532. <p>
  2533. Requests a connection to be made on the given <tt>remote_sockaddr</tt>.
  2534. Returns 0 if successful, otherwise an exception is raised.
  2535. </p>
  2536. <h3>Parameter</h3>
  2537. <ul>
  2538. <li><p>
  2539. <tt>remote_sockaddr</tt> - the <tt>struct</tt> sockaddr contained in a
  2540. string or Addrinfo object
  2541. </p>
  2542. </li>
  2543. </ul>
  2544. <h3>Example:</h3>
  2545. <pre class="code">
  2546. # Pull down Google's web page
  2547. require 'socket'
  2548. include Socket::Constants
  2549. socket = Socket.new( AF_INET, SOCK_STREAM, 0 )
  2550. sockaddr = Socket.pack_sockaddr_in( 80, 'www.google.com' )
  2551. socket.connect( sockaddr )
  2552. socket.write( &quot;GET / HTTP/1.0\r\n\r\n&quot; )
  2553. results = socket.read
  2554. </pre>
  2555. <h3>Unix-based Exceptions</h3>
  2556. <p>
  2557. On unix-based systems the following system exceptions may be raised if the
  2558. call to <em>connect</em> fails:
  2559. </p>
  2560. <ul>
  2561. <li><p>
  2562. Errno::EACCES - search permission is denied for a component of the prefix
  2563. path or write access to the <tt>socket</tt> is denied
  2564. </p>
  2565. </li>
  2566. <li><p>
  2567. Errno::EADDRINUSE - the <em>sockaddr</em> is already in use
  2568. </p>
  2569. </li>
  2570. <li><p>
  2571. Errno::EADDRNOTAVAIL - the specified <em>sockaddr</em> is not available
  2572. from the local machine
  2573. </p>
  2574. </li>
  2575. <li><p>
  2576. Errno::EAFNOSUPPORT - the specified <em>sockaddr</em> is not a valid
  2577. address for the address family of the specified <tt>socket</tt>
  2578. </p>
  2579. </li>
  2580. <li><p>
  2581. Errno::EALREADY - a connection is already in progress for the specified
  2582. socket
  2583. </p>
  2584. </li>
  2585. <li><p>
  2586. Errno::EBADF - the <tt>socket</tt> is not a valid file descriptor
  2587. </p>
  2588. </li>
  2589. <li><p>
  2590. Errno::ECONNREFUSED - the target <em>sockaddr</em> was not listening for
  2591. connections refused the connection request
  2592. </p>
  2593. </li>
  2594. <li><p>
  2595. Errno::ECONNRESET - the remote host reset the connection request
  2596. </p>
  2597. </li>
  2598. <li><p>
  2599. Errno::EFAULT - the <em>sockaddr</em> cannot be accessed
  2600. </p>
  2601. </li>
  2602. <li><p>
  2603. Errno::EHOSTUNREACH - the destination host cannot be reached (probably
  2604. because the host is down or a remote router cannot reach it)
  2605. </p>
  2606. </li>
  2607. <li><p>
  2608. Errno::EINPROGRESS - the O_NONBLOCK is set for the <tt>socket</tt> and the
  2609. connection cannot be immediately established; the connection will be
  2610. established asynchronously
  2611. </p>
  2612. </li>
  2613. <li><p>
  2614. Errno::EINTR - the attempt to establish the connection was interrupted by
  2615. delivery of a signal that was caught; the connection will be established
  2616. asynchronously
  2617. </p>
  2618. </li>
  2619. <li><p>
  2620. Errno::EISCONN - the specified <tt>socket</tt> is already connected
  2621. </p>
  2622. </li>
  2623. <li><p>
  2624. Errno::EINVAL - the address length used for the <em>sockaddr</em> is not a
  2625. valid length for the address family or there is an invalid family in
  2626. <em>sockaddr</em>
  2627. </p>
  2628. </li>
  2629. <li><p>
  2630. Errno::ENAMETOOLONG - the pathname resolved had a length which exceeded
  2631. PATH_MAX
  2632. </p>
  2633. </li>
  2634. <li><p>
  2635. Errno::ENETDOWN - the local interface used to reach the destination is down
  2636. </p>
  2637. </li>
  2638. <li><p>
  2639. Errno::ENETUNREACH - no route to the network is present
  2640. </p>
  2641. </li>
  2642. <li><p>
  2643. Errno::ENOBUFS - no buffer space is available
  2644. </p>
  2645. </li>
  2646. <li><p>
  2647. Errno::ENOSR - there were insufficient STREAMS resources available to
  2648. complete the operation
  2649. </p>
  2650. </li>
  2651. <li><p>
  2652. Errno::ENOTSOCK - the <tt>socket</tt> argument does not refer to a socket
  2653. </p>
  2654. </li>
  2655. <li><p>
  2656. Errno::EOPNOTSUPP - the calling <tt>socket</tt> is listening and cannot be
  2657. connected
  2658. </p>
  2659. </li>
  2660. <li><p>
  2661. Errno::EPROTOTYPE - the <em>sockaddr</em> has a different type than the
  2662. socket bound to the specified peer address
  2663. </p>
  2664. </li>
  2665. <li><p>
  2666. Errno::ETIMEDOUT - the attempt to connect time out before a connection was
  2667. made.
  2668. </p>
  2669. </li>
  2670. </ul>
  2671. <p>
  2672. On unix-based systems if the address family of the calling <tt>socket</tt>
  2673. is AF_UNIX the follow exceptions may be raised if the call to
  2674. <em>connect</em> fails:
  2675. </p>
  2676. <ul>
  2677. <li><p>
  2678. Errno::EIO - an i/o error occurred while reading from or writing to the
  2679. file system
  2680. </p>
  2681. </li>
  2682. <li><p>
  2683. Errno::ELOOP - too many symbolic links were encountered in translating the
  2684. pathname in <em>sockaddr</em>
  2685. </p>
  2686. </li>
  2687. <li><p>
  2688. Errno::ENAMETOOLLONG - a component of a pathname exceeded NAME_MAX
  2689. characters, or an entire pathname exceeded PATH_MAX characters
  2690. </p>
  2691. </li>
  2692. <li><p>
  2693. Errno::ENOENT - a component of the pathname does not name an existing file
  2694. or the pathname is an empty string
  2695. </p>
  2696. </li>
  2697. <li><p>
  2698. Errno::ENOTDIR - a component of the path prefix of the pathname in
  2699. <em>sockaddr</em> is not a directory
  2700. </p>
  2701. </li>
  2702. </ul>
  2703. <h3>Windows Exceptions</h3>
  2704. <p>
  2705. On Windows systems the following system exceptions may be raised if the
  2706. call to <em>connect</em> fails:
  2707. </p>
  2708. <ul>
  2709. <li><p>
  2710. Errno::ENETDOWN - the network is down
  2711. </p>
  2712. </li>
  2713. <li><p>
  2714. Errno::EADDRINUSE - the socket&#8217;s local address is already in use
  2715. </p>
  2716. </li>
  2717. <li><p>
  2718. Errno::EINTR - the socket was cancelled
  2719. </p>
  2720. </li>
  2721. <li><p>
  2722. Errno::EINPROGRESS - a blocking socket is in progress or the service
  2723. provider is still processing a callback function. Or a nonblocking connect
  2724. call is in progress on the <tt>socket</tt>.
  2725. </p>
  2726. </li>
  2727. <li><p>
  2728. Errno::EALREADY - see Errno::EINVAL
  2729. </p>
  2730. </li>
  2731. <li><p>
  2732. Errno::EADDRNOTAVAIL - the remote address is not a valid address, such as
  2733. ADDR_ANY TODO check ADDRANY TO INADDR_ANY
  2734. </p>
  2735. </li>
  2736. <li><p>
  2737. Errno::EAFNOSUPPORT - addresses in the specified family cannot be used with
  2738. with this <tt>socket</tt>
  2739. </p>
  2740. </li>
  2741. <li><p>
  2742. Errno::ECONNREFUSED - the target <em>sockaddr</em> was not listening for
  2743. connections refused the connection request
  2744. </p>
  2745. </li>
  2746. <li><p>
  2747. Errno::EFAULT - the socket&#8217;s internal address or address length
  2748. parameter is too small or is not a valid part of the user space address
  2749. </p>
  2750. </li>
  2751. <li><p>
  2752. Errno::EINVAL - the <tt>socket</tt> is a listening socket
  2753. </p>
  2754. </li>
  2755. <li><p>
  2756. Errno::EISCONN - the <tt>socket</tt> is already connected
  2757. </p>
  2758. </li>
  2759. <li><p>
  2760. Errno::ENETUNREACH - the network cannot be reached from this host at this
  2761. time
  2762. </p>
  2763. </li>
  2764. <li><p>
  2765. Errno::EHOSTUNREACH - no route to the network is present
  2766. </p>
  2767. </li>
  2768. <li><p>
  2769. Errno::ENOBUFS - no buffer space is available
  2770. </p>
  2771. </li>
  2772. <li><p>
  2773. Errno::ENOTSOCK - the <tt>socket</tt> argument does not refer to a socket
  2774. </p>
  2775. </li>
  2776. <li><p>
  2777. Errno::ETIMEDOUT - the attempt to connect time out before a connection was
  2778. made.
  2779. </p>
  2780. </li>
  2781. <li><p>
  2782. Errno::EWOULDBLOCK - the socket is marked as nonblocking and the connection
  2783. cannot be completed immediately
  2784. </p>
  2785. </li>
  2786. <li><p>
  2787. Errno::EACCES - the attempt to connect the datagram socket to the broadcast
  2788. address failed
  2789. </p>
  2790. </li>
  2791. </ul>
  2792. <h3>See</h3>
  2793. <ul>
  2794. <li><p>
  2795. connect manual pages on unix-based systems
  2796. </p>
  2797. </li>
  2798. <li><p>
  2799. connect function in Microsoft&#8217;s Winsock functions reference
  2800. </p>
  2801. </li>
  2802. </ul>
  2803. </div>
  2804. </div>
  2805. <div class="tags">
  2806. <div class="docstring">
  2807. <div class="discussion">
  2808. </div>
  2809. </div>
  2810. <div class="tags">
  2811. <h3>Returns:</h3>
  2812. <ul class="return">
  2813. <li>
  2814. <span class='type'>(<tt>0</tt>)</span>
  2815. </li>
  2816. </ul>
  2817. </div>
  2818. </div><table class="source_code">
  2819. <tr>
  2820. <td>
  2821. <pre class="lines">
  2822. </pre>
  2823. </td>
  2824. <td>
  2825. <pre class="code"><span class="info file"># File 'ext/socket/socket.c'</span>
  2826. /*
  2827. * call-seq:
  2828. * socket.connect(remote_sockaddr) =&gt; 0
  2829. *
  2830. * Requests a connection to be made on the given +remote_sockaddr+. Returns 0 if
  2831. * successful, otherwise an exception is raised.
  2832. *
  2833. * === Parameter
  2834. * * +remote_sockaddr+ - the +struct+ sockaddr contained in a string or Addrinfo object
  2835. *
  2836. * === Example:
  2837. * # Pull down Google's web page
  2838. * require 'socket'
  2839. * include Socket::Constants
  2840. * socket = Socket.new( AF_INET, SOCK_STREAM, 0 )
  2841. * sockaddr = Socket.pack_sockaddr_in( 80, 'www.google.com' )
  2842. * socket.connect( sockaddr )
  2843. * socket.write( &quot;GET / HTTP/1.0\r\n\r\n&quot; )
  2844. * results = socket.read
  2845. *
  2846. * === Unix-based Exceptions
  2847. * On unix-based systems the following system exceptions may be raised if
  2848. * the call to _connect_ fails:
  2849. * * Errno::EACCES - search permission is denied for a component of the prefix
  2850. * path or write access to the +socket+ is denied
  2851. * * Errno::EADDRINUSE - the _sockaddr_ is already in use
  2852. * * Errno::EADDRNOTAVAIL - the specified _sockaddr_ is not available from the
  2853. * local machine
  2854. * * Errno::EAFNOSUPPORT - the specified _sockaddr_ is not a valid address for
  2855. * the address family of the specified +socket+
  2856. * * Errno::EALREADY - a connection is already in progress for the specified
  2857. * socket
  2858. * * Errno::EBADF - the +socket+ is not a valid file descriptor
  2859. * * Errno::ECONNREFUSED - the target _sockaddr_ was not listening for connections
  2860. * refused the connection request
  2861. * * Errno::ECONNRESET - the remote host reset the connection request
  2862. * * Errno::EFAULT - the _sockaddr_ cannot be accessed
  2863. * * Errno::EHOSTUNREACH - the destination host cannot be reached (probably
  2864. * because the host is down or a remote router cannot reach it)
  2865. * * Errno::EINPROGRESS - the O_NONBLOCK is set for the +socket+ and the
  2866. * connection cannot be immediately established; the connection will be
  2867. * established asynchronously
  2868. * * Errno::EINTR - the attempt to establish the connection was interrupted by
  2869. * delivery of a signal that was caught; the connection will be established
  2870. * asynchronously
  2871. * * Errno::EISCONN - the specified +socket+ is already connected
  2872. * * Errno::EINVAL - the address length used for the _sockaddr_ is not a valid
  2873. * length for the address family or there is an invalid family in _sockaddr_
  2874. * * Errno::ENAMETOOLONG - the pathname resolved had a length which exceeded
  2875. * PATH_MAX
  2876. * * Errno::ENETDOWN - the local interface used to reach the destination is down
  2877. * * Errno::ENETUNREACH - no route to the network is present
  2878. * * Errno::ENOBUFS - no buffer space is available
  2879. * * Errno::ENOSR - there were insufficient STREAMS resources available to
  2880. * complete the operation
  2881. * * Errno::ENOTSOCK - the +socket+ argument does not refer to a socket
  2882. * * Errno::EOPNOTSUPP - the calling +socket+ is listening and cannot be connected
  2883. * * Errno::EPROTOTYPE - the _sockaddr_ has a different type than the socket
  2884. * bound to the specified peer address
  2885. * * Errno::ETIMEDOUT - the attempt to connect time out before a connection
  2886. * was made.
  2887. *
  2888. * On unix-based systems if the address family of the calling +socket+ is
  2889. * AF_UNIX the follow exceptions may be raised if the call to _connect_
  2890. * fails:
  2891. * * Errno::EIO - an i/o error occurred while reading from or writing to the
  2892. * file system
  2893. * * Errno::ELOOP - too many symbolic links were encountered in translating
  2894. * the pathname in _sockaddr_
  2895. * * Errno::ENAMETOOLLONG - a component of a pathname exceeded NAME_MAX
  2896. * characters, or an entire pathname exceeded PATH_MAX characters
  2897. * * Errno::ENOENT - a component of the pathname does not name an existing file
  2898. * or the pathname is an empty string
  2899. * * Errno::ENOTDIR - a component of the path prefix of the pathname in _sockaddr_
  2900. * is not a directory
  2901. *
  2902. * === Windows Exceptions
  2903. * On Windows systems the following system exceptions may be raised if
  2904. * the call to _connect_ fails:
  2905. * * Errno::ENETDOWN - the network is down
  2906. * * Errno::EADDRINUSE - the socket's local address is already in use
  2907. * * Errno::EINTR - the socket was cancelled
  2908. * * Errno::EINPROGRESS - a blocking socket is in progress or the service provider
  2909. * is still processing a callback function. Or a nonblocking connect call is
  2910. * in progress on the +socket+.
  2911. * * Errno::EALREADY - see Errno::EINVAL
  2912. * * Errno::EADDRNOTAVAIL - the remote address is not a valid address, such as
  2913. * ADDR_ANY TODO check ADDRANY TO INADDR_ANY
  2914. * * Errno::EAFNOSUPPORT - addresses in the specified family cannot be used with
  2915. * with this +socket+
  2916. * * Errno::ECONNREFUSED - the target _sockaddr_ was not listening for connections
  2917. * refused the connection request
  2918. * * Errno::EFAULT - the socket's internal address or address length parameter
  2919. * is too small or is not a valid part of the user space address
  2920. * * Errno::EINVAL - the +socket+ is a listening socket
  2921. * * Errno::EISCONN - the +socket+ is already connected
  2922. * * Errno::ENETUNREACH - the network cannot be reached from this host at this time
  2923. * * Errno::EHOSTUNREACH - no route to the network is present
  2924. * * Errno::ENOBUFS - no buffer space is available
  2925. * * Errno::ENOTSOCK - the +socket+ argument does not refer to a socket
  2926. * * Errno::ETIMEDOUT - the attempt to connect time out before a connection
  2927. * was made.
  2928. * * Errno::EWOULDBLOCK - the socket is marked as nonblocking and the
  2929. * connection cannot be completed immediately
  2930. * * Errno::EACCES - the attempt to connect the datagram socket to the
  2931. * broadcast address failed
  2932. *
  2933. * === See
  2934. * * connect manual pages on unix-based systems
  2935. * * connect function in Microsoft's Winsock functions reference
  2936. */
  2937. static VALUE
  2938. sock_connect(VALUE sock, VALUE addr)
  2939. {
  2940. rb_io_t *fptr;
  2941. int fd, n;
  2942. SockAddrStringValue(addr);
  2943. addr = rb_str_new4(addr);
  2944. GetOpenFile(sock, fptr);
  2945. fd = fptr-&gt;fd;
  2946. n = rsock_connect(fd, (struct sockaddr*)RSTRING_PTR(addr), RSTRING_LENINT(addr), 0);
  2947. if (n &lt; 0) {
  2948. rb_sys_fail(&quot;connect(2)&quot;);
  2949. }
  2950. return INT2FIX(n);
  2951. }</pre>
  2952. </td>
  2953. </tr>
  2954. </table>
  2955. </div>
  2956. <div class="method_details ">
  2957. <p class="signature " id="connect_nonblock-instance_method">
  2958. - (<tt>0</tt>) <strong>connect_nonblock</strong>(remote_sockaddr)
  2959. </p><div class="docstring">
  2960. <div class="discussion">
  2961. <p>
  2962. Requests a connection to be made on the given <tt>remote_sockaddr</tt>
  2963. after O_NONBLOCK is set for the underlying file descriptor. Returns 0 if
  2964. successful, otherwise an exception is raised.
  2965. </p>
  2966. <h3>Parameter</h3>
  2967. <ul>
  2968. <li><p>
  2969. <tt>remote_sockaddr</tt> - the <tt>struct</tt> sockaddr contained in a
  2970. string or Addrinfo object
  2971. </p>
  2972. </li>
  2973. </ul>
  2974. <h3>Example:</h3>
  2975. <pre class="code">
  2976. # Pull down Google's web page
  2977. require 'socket'
  2978. include Socket::Constants
  2979. socket = Socket.new(AF_INET, SOCK_STREAM, 0)
  2980. sockaddr = Socket.sockaddr_in(80, 'www.google.com')
  2981. begin # emulate blocking connect
  2982. socket.connect_nonblock(sockaddr)
  2983. rescue IO::WaitWritable
  2984. IO.select(nil, [socket]) # wait 3-way handshake completion
  2985. begin
  2986. socket.connect_nonblock(sockaddr) # check connection failure
  2987. rescue Errno::EISCONN
  2988. end
  2989. end
  2990. socket.write(&quot;GET / HTTP/1.0\r\n\r\n&quot;)
  2991. results = socket.read
  2992. </pre>
  2993. <p>
  2994. Refer to Socket#connect for the exceptions that may be thrown if the call
  2995. to <em>connect_nonblock</em> fails.
  2996. </p>
  2997. <p>
  2998. Socket#connect_nonblock may raise any error corresponding to connect(2)
  2999. failure, including Errno::EINPROGRESS.
  3000. </p>
  3001. <p>
  3002. If the exception is Errno::EINPROGRESS, it is extended by IO::WaitWritable.
  3003. So IO::WaitWritable can be used to rescue the exceptions for retrying
  3004. connect_nonblock.
  3005. </p>
  3006. <h3>See</h3>
  3007. <ul>
  3008. <li><p>
  3009. Socket#connect
  3010. </p>
  3011. </li>
  3012. </ul>
  3013. </div>
  3014. </div>
  3015. <div class="tags">
  3016. <div class="docstring">
  3017. <div class="discussion">
  3018. </div>
  3019. </div>
  3020. <div class="tags">
  3021. <h3>Returns:</h3>
  3022. <ul class="return">
  3023. <li>
  3024. <span class='type'>(<tt>0</tt>)</span>
  3025. </li>
  3026. </ul>
  3027. </div>
  3028. </div><table class="source_code">
  3029. <tr>
  3030. <td>
  3031. <pre class="lines">
  3032. </pre>
  3033. </td>
  3034. <td>
  3035. <pre class="code"><span class="info file"># File 'ext/socket/socket.c'</span>
  3036. /*
  3037. * call-seq:
  3038. * socket.connect_nonblock(remote_sockaddr) =&gt; 0
  3039. *
  3040. * Requests a connection to be made on the given +remote_sockaddr+ after
  3041. * O_NONBLOCK is set for the underlying file descriptor.
  3042. * Returns 0 if successful, otherwise an exception is raised.
  3043. *
  3044. * === Parameter
  3045. * * +remote_sockaddr+ - the +struct+ sockaddr contained in a string or Addrinfo object
  3046. *
  3047. * === Example:
  3048. * # Pull down Google's web page
  3049. * require 'socket'
  3050. * include Socket::Constants
  3051. * socket = Socket.new(AF_INET, SOCK_STREAM, 0)
  3052. * sockaddr = Socket.sockaddr_in(80, 'www.google.com')
  3053. * begin # emulate blocking connect
  3054. * socket.connect_nonblock(sockaddr)
  3055. * rescue IO::WaitWritable
  3056. * IO.select(nil, [socket]) # wait 3-way handshake completion
  3057. * begin
  3058. * socket.connect_nonblock(sockaddr) # check connection failure
  3059. * rescue Errno::EISCONN
  3060. * end
  3061. * end
  3062. * socket.write(&quot;GET / HTTP/1.0\r\n\r\n&quot;)
  3063. * results = socket.read
  3064. *
  3065. * Refer to Socket#connect for the exceptions that may be thrown if the call
  3066. * to _connect_nonblock_ fails.
  3067. *
  3068. * Socket#connect_nonblock may raise any error corresponding to connect(2) failure,
  3069. * including Errno::EINPROGRESS.
  3070. *
  3071. * If the exception is Errno::EINPROGRESS,
  3072. * it is extended by IO::WaitWritable.
  3073. * So IO::WaitWritable can be used to rescue the exceptions for retrying connect_nonblock.
  3074. *
  3075. * === See
  3076. * * Socket#connect
  3077. */
  3078. static VALUE
  3079. sock_connect_nonblock(VALUE sock, VALUE addr)
  3080. {
  3081. rb_io_t *fptr;
  3082. int n;
  3083. SockAddrStringValue(addr);
  3084. addr = rb_str_new4(addr);
  3085. GetOpenFile(sock, fptr);
  3086. rb_io_set_nonblock(fptr);
  3087. n = connect(fptr-&gt;fd, (struct sockaddr*)RSTRING_PTR(addr), RSTRING_LENINT(addr));
  3088. if (n &lt; 0) {
  3089. if (errno == EINPROGRESS)
  3090. rb_mod_sys_fail(rb_mWaitWritable, &quot;connect(2) would block&quot;);
  3091. rb_sys_fail(&quot;connect(2)&quot;);
  3092. }
  3093. return INT2FIX(n);
  3094. }</pre>
  3095. </td>
  3096. </tr>
  3097. </table>
  3098. </div>
  3099. <div class="method_details ">
  3100. <p class="signature " id="listen-instance_method">
  3101. - (<tt>0</tt>) <strong>listen</strong>(int)
  3102. </p><div class="docstring">
  3103. <div class="discussion">
  3104. <p>
  3105. Listens for connections, using the specified <tt>int</tt> as the backlog. A
  3106. call to <em>listen</em> only applies if the <tt>socket</tt> is of type
  3107. SOCK_STREAM or SOCK_SEQPACKET.
  3108. </p>
  3109. <h3>Parameter</h3>
  3110. <ul>
  3111. <li><p>
  3112. <tt>backlog</tt> - the maximum length of the queue for pending connections.
  3113. </p>
  3114. </li>
  3115. </ul>
  3116. <h3>Example 1</h3>
  3117. <pre class="code">
  3118. require 'socket'
  3119. include Socket::Constants
  3120. socket = Socket.new( AF_INET, SOCK_STREAM, 0 )
  3121. sockaddr = Socket.pack_sockaddr_in( 2200, 'localhost' )
  3122. socket.bind( sockaddr )
  3123. socket.listen( 5 )
  3124. </pre>
  3125. <h3>Example 2 (listening on an arbitrary port, unix-based systems only):</h3>
  3126. <pre class="code">
  3127. require 'socket'
  3128. include Socket::Constants
  3129. socket = Socket.new( AF_INET, SOCK_STREAM, 0 )
  3130. socket.listen( 1 )
  3131. </pre>
  3132. <h3>Unix-based Exceptions</h3>
  3133. <p>
  3134. On unix based systems the above will work because a new <tt>sockaddr</tt>
  3135. struct is created on the address ADDR_ANY, for an arbitrary port number as
  3136. handed off by the kernel. It will not work on Windows, because Windows
  3137. requires that the <tt>socket</tt> is bound by calling <em>bind</em> before
  3138. it can <em>listen</em>.
  3139. </p>
  3140. <p>
  3141. If the <em>backlog</em> amount exceeds the implementation-dependent maximum
  3142. queue length, the implementation&#8217;s maximum queue length will be used.
  3143. </p>
  3144. <p>
  3145. On unix-based based systems the following system exceptions may be raised
  3146. if the call to <em>listen</em> fails:
  3147. </p>
  3148. <ul>
  3149. <li><p>
  3150. Errno::EBADF - the <em>socket</em> argument is not a valid file descriptor
  3151. </p>
  3152. </li>
  3153. <li><p>
  3154. Errno::EDESTADDRREQ - the <em>socket</em> is not bound to a local address,
  3155. and the protocol does not support listening on an unbound socket
  3156. </p>
  3157. </li>
  3158. <li><p>
  3159. Errno::EINVAL - the <em>socket</em> is already connected
  3160. </p>
  3161. </li>
  3162. <li><p>
  3163. Errno::ENOTSOCK - the <em>socket</em> argument does not refer to a socket
  3164. </p>
  3165. </li>
  3166. <li><p>
  3167. Errno::EOPNOTSUPP - the <em>socket</em> protocol does not support listen
  3168. </p>
  3169. </li>
  3170. <li><p>
  3171. Errno::EACCES - the calling process does not have appropriate privileges
  3172. </p>
  3173. </li>
  3174. <li><p>
  3175. Errno::EINVAL - the <em>socket</em> has been shut down
  3176. </p>
  3177. </li>
  3178. <li><p>
  3179. Errno::ENOBUFS - insufficient resources are available in the system to
  3180. complete the call
  3181. </p>
  3182. </li>
  3183. </ul>
  3184. <h3>Windows Exceptions</h3>
  3185. <p>
  3186. On Windows systems the following system exceptions may be raised if the
  3187. call to <em>listen</em> fails:
  3188. </p>
  3189. <ul>
  3190. <li><p>
  3191. Errno::ENETDOWN - the network is down
  3192. </p>
  3193. </li>
  3194. <li><p>
  3195. Errno::EADDRINUSE - the socket&#8217;s local address is already in use.
  3196. This usually occurs during the execution of <em>bind</em> but could be
  3197. delayed if the call to <em>bind</em> was to a partially wildcard address
  3198. (involving ADDR_ANY) and if a specific address needs to be committed at the
  3199. time of the call to <em>listen</em>
  3200. </p>
  3201. </li>
  3202. <li><p>
  3203. Errno::EINPROGRESS - a Windows Sockets 1.1 call is in progress or the
  3204. service provider is still processing a callback function
  3205. </p>
  3206. </li>
  3207. <li><p>
  3208. Errno::EINVAL - the <tt>socket</tt> has not been bound with a call to
  3209. <em>bind</em>.
  3210. </p>
  3211. </li>
  3212. <li><p>
  3213. Errno::EISCONN - the <tt>socket</tt> is already connected
  3214. </p>
  3215. </li>
  3216. <li><p>
  3217. Errno::EMFILE - no more socket descriptors are available
  3218. </p>
  3219. </li>
  3220. <li><p>
  3221. Errno::ENOBUFS - no buffer space is available
  3222. </p>
  3223. </li>
  3224. <li><p>
  3225. Errno::ENOTSOC - <tt>socket</tt> is not a socket
  3226. </p>
  3227. </li>
  3228. <li><p>
  3229. Errno::EOPNOTSUPP - the referenced <tt>socket</tt> is not a type that
  3230. supports the <em>listen</em> method
  3231. </p>
  3232. </li>
  3233. </ul>
  3234. <h3>See</h3>
  3235. <ul>
  3236. <li><p>
  3237. listen manual pages on unix-based systems
  3238. </p>
  3239. </li>
  3240. <li><p>
  3241. listen function in Microsoft&#8217;s Winsock functions reference
  3242. </p>
  3243. </li>
  3244. </ul>
  3245. </div>
  3246. </div>
  3247. <div class="tags">
  3248. <div class="docstring">
  3249. <div class="discussion">
  3250. </div>
  3251. </div>
  3252. <div class="tags">
  3253. <h3>Returns:</h3>
  3254. <ul class="return">
  3255. <li>
  3256. <span class='type'>(<tt>0</tt>)</span>
  3257. </li>
  3258. </ul>
  3259. </div>
  3260. </div><table class="source_code">
  3261. <tr>
  3262. <td>
  3263. <pre class="lines">
  3264. </pre>
  3265. </td>
  3266. <td>
  3267. <pre class="code"><span class="info file"># File 'ext/socket/socket.c'</span>
  3268. /*
  3269. * call-seq:
  3270. * socket.listen( int ) =&gt; 0
  3271. *
  3272. * Listens for connections, using the specified +int+ as the backlog. A call
  3273. * to _listen_ only applies if the +socket+ is of type SOCK_STREAM or
  3274. * SOCK_SEQPACKET.
  3275. *
  3276. * === Parameter
  3277. * * +backlog+ - the maximum length of the queue for pending connections.
  3278. *
  3279. * === Example 1
  3280. * require 'socket'
  3281. * include Socket::Constants
  3282. * socket = Socket.new( AF_INET, SOCK_STREAM, 0 )
  3283. * sockaddr = Socket.pack_sockaddr_in( 2200, 'localhost' )
  3284. * socket.bind( sockaddr )
  3285. * socket.listen( 5 )
  3286. *
  3287. * === Example 2 (listening on an arbitrary port, unix-based systems only):
  3288. * require 'socket'
  3289. * include Socket::Constants
  3290. * socket = Socket.new( AF_INET, SOCK_STREAM, 0 )
  3291. * socket.listen( 1 )
  3292. *
  3293. * === Unix-based Exceptions
  3294. * On unix based systems the above will work because a new +sockaddr+ struct
  3295. * is created on the address ADDR_ANY, for an arbitrary port number as handed
  3296. * off by the kernel. It will not work on Windows, because Windows requires that
  3297. * the +socket+ is bound by calling _bind_ before it can _listen_.
  3298. *
  3299. * If the _backlog_ amount exceeds the implementation-dependent maximum
  3300. * queue length, the implementation's maximum queue length will be used.
  3301. *
  3302. * On unix-based based systems the following system exceptions may be raised if the
  3303. * call to _listen_ fails:
  3304. * * Errno::EBADF - the _socket_ argument is not a valid file descriptor
  3305. * * Errno::EDESTADDRREQ - the _socket_ is not bound to a local address, and
  3306. * the protocol does not support listening on an unbound socket
  3307. * * Errno::EINVAL - the _socket_ is already connected
  3308. * * Errno::ENOTSOCK - the _socket_ argument does not refer to a socket
  3309. * * Errno::EOPNOTSUPP - the _socket_ protocol does not support listen
  3310. * * Errno::EACCES - the calling process does not have appropriate privileges
  3311. * * Errno::EINVAL - the _socket_ has been shut down
  3312. * * Errno::ENOBUFS - insufficient resources are available in the system to
  3313. * complete the call
  3314. *
  3315. * === Windows Exceptions
  3316. * On Windows systems the following system exceptions may be raised if
  3317. * the call to _listen_ fails:
  3318. * * Errno::ENETDOWN - the network is down
  3319. * * Errno::EADDRINUSE - the socket's local address is already in use. This
  3320. * usually occurs during the execution of _bind_ but could be delayed
  3321. * if the call to _bind_ was to a partially wildcard address (involving
  3322. * ADDR_ANY) and if a specific address needs to be committed at the
  3323. * time of the call to _listen_
  3324. * * Errno::EINPROGRESS - a Windows Sockets 1.1 call is in progress or the
  3325. * service provider is still processing a callback function
  3326. * * Errno::EINVAL - the +socket+ has not been bound with a call to _bind_.
  3327. * * Errno::EISCONN - the +socket+ is already connected
  3328. * * Errno::EMFILE - no more socket descriptors are available
  3329. * * Errno::ENOBUFS - no buffer space is available
  3330. * * Errno::ENOTSOC - +socket+ is not a socket
  3331. * * Errno::EOPNOTSUPP - the referenced +socket+ is not a type that supports
  3332. * the _listen_ method
  3333. *
  3334. * === See
  3335. * * listen manual pages on unix-based systems
  3336. * * listen function in Microsoft's Winsock functions reference
  3337. */
  3338. VALUE
  3339. rsock_sock_listen(VALUE sock, VALUE log)
  3340. {
  3341. rb_io_t *fptr;
  3342. int backlog;
  3343. rb_secure(4);
  3344. backlog = NUM2INT(log);
  3345. GetOpenFile(sock, fptr);
  3346. if (listen(fptr-&gt;fd, backlog) &lt; 0)
  3347. rb_sys_fail(&quot;listen(2)&quot;);
  3348. return INT2FIX(0);
  3349. }</pre>
  3350. </td>
  3351. </tr>
  3352. </table>
  3353. </div>
  3354. <div class="method_details ">
  3355. <p class="signature " id="recvfrom-instance_method">
  3356. <span class="overload">- (<tt><span class='object_link'><a href="Array.html" title="Array (class)">Array</a></span></tt>) <strong>recvfrom</strong>(maxlen) </span>
  3357. <span class="overload">- (<tt><span class='object_link'><a href="Array.html" title="Array (class)">Array</a></span></tt>) <strong>recvfrom</strong>(maxlen, flags) </span>
  3358. </p><div class="docstring">
  3359. <div class="discussion">
  3360. <p>
  3361. Receives up to <em>maxlen</em> bytes from <tt>socket</tt>. <em>flags</em>
  3362. is zero or more of the <tt>MSG_</tt> options. The first element of the
  3363. results, <em>mesg</em>, is the data received. The second element,
  3364. <em>sender_addrinfo</em>, contains protocol-specific address information of
  3365. the sender.
  3366. </p>
  3367. <h3>Parameters</h3>
  3368. <ul>
  3369. <li><p>
  3370. <tt>maxlen</tt> - the maximum number of bytes to receive from the socket
  3371. </p>
  3372. </li>
  3373. <li><p>
  3374. <tt>flags</tt> - zero or more of the <tt>MSG_</tt> options
  3375. </p>
  3376. </li>
  3377. </ul>
  3378. <h3>Example</h3>
  3379. <pre class="code">
  3380. # In one file, start this first
  3381. require 'socket'
  3382. include Socket::Constants
  3383. socket = Socket.new( AF_INET, SOCK_STREAM, 0 )
  3384. sockaddr = Socket.pack_sockaddr_in( 2200, 'localhost' )
  3385. socket.bind( sockaddr )
  3386. socket.listen( 5 )
  3387. client, client_addrinfo = socket.accept
  3388. data = client.recvfrom( 20 )[0].chomp
  3389. puts &quot;I only received 20 bytes '#{data}'&quot;
  3390. sleep 1
  3391. socket.close
  3392. # In another file, start this second
  3393. require 'socket'
  3394. include Socket::Constants
  3395. socket = Socket.new( AF_INET, SOCK_STREAM, 0 )
  3396. sockaddr = Socket.pack_sockaddr_in( 2200, 'localhost' )
  3397. socket.connect( sockaddr )
  3398. socket.puts &quot;Watch this get cut short!&quot;
  3399. socket.close
  3400. </pre>
  3401. <h3>Unix-based Exceptions</h3>
  3402. <p>
  3403. On unix-based based systems the following system exceptions may be raised
  3404. if the call to <em>recvfrom</em> fails:
  3405. </p>
  3406. <ul>
  3407. <li><p>
  3408. Errno::EAGAIN - the <tt>socket</tt> file descriptor is marked as O_NONBLOCK
  3409. and no data is waiting to be received; or MSG_OOB is set and no out-of-band
  3410. data is available and either the <tt>socket</tt> file descriptor is marked
  3411. as O_NONBLOCK or the <tt>socket</tt> does not support blocking to wait for
  3412. out-of-band-data
  3413. </p>
  3414. </li>
  3415. <li><p>
  3416. Errno::EWOULDBLOCK - see Errno::EAGAIN
  3417. </p>
  3418. </li>
  3419. <li><p>
  3420. Errno::EBADF - the <tt>socket</tt> is not a valid file descriptor
  3421. </p>
  3422. </li>
  3423. <li><p>
  3424. Errno::ECONNRESET - a connection was forcibly closed by a peer
  3425. </p>
  3426. </li>
  3427. <li><p>
  3428. Errno::EFAULT - the socket&#8217;s internal buffer, address or address
  3429. length cannot be accessed or written
  3430. </p>
  3431. </li>
  3432. <li><p>
  3433. Errno::EINTR - a signal interrupted <em>recvfrom</em> before any data was
  3434. available
  3435. </p>
  3436. </li>
  3437. <li><p>
  3438. Errno::EINVAL - the MSG_OOB flag is set and no out-of-band data is
  3439. available
  3440. </p>
  3441. </li>
  3442. <li><p>
  3443. Errno::EIO - an i/o error occurred while reading from or writing to the
  3444. filesystem
  3445. </p>
  3446. </li>
  3447. <li><p>
  3448. Errno::ENOBUFS - insufficient resources were available in the system to
  3449. perform the operation
  3450. </p>
  3451. </li>
  3452. <li><p>
  3453. Errno::ENOMEM - insufficient memory was available to fulfill the request
  3454. </p>
  3455. </li>
  3456. <li><p>
  3457. Errno::ENOSR - there were insufficient STREAMS resources available to
  3458. complete the operation
  3459. </p>
  3460. </li>
  3461. <li><p>
  3462. Errno::ENOTCONN - a receive is attempted on a connection-mode socket that
  3463. is not connected
  3464. </p>
  3465. </li>
  3466. <li><p>
  3467. Errno::ENOTSOCK - the <tt>socket</tt> does not refer to a socket
  3468. </p>
  3469. </li>
  3470. <li><p>
  3471. Errno::EOPNOTSUPP - the specified flags are not supported for this socket
  3472. type
  3473. </p>
  3474. </li>
  3475. <li><p>
  3476. Errno::ETIMEDOUT - the connection timed out during connection establishment
  3477. or due to a transmission timeout on an active connection
  3478. </p>
  3479. </li>
  3480. </ul>
  3481. <h3>Windows Exceptions</h3>
  3482. <p>
  3483. On Windows systems the following system exceptions may be raised if the
  3484. call to <em>recvfrom</em> fails:
  3485. </p>
  3486. <ul>
  3487. <li><p>
  3488. Errno::ENETDOWN - the network is down
  3489. </p>
  3490. </li>
  3491. <li><p>
  3492. Errno::EFAULT - the internal buffer and from parameters on <tt>socket</tt>
  3493. are not part of the user address space, or the internal fromlen parameter
  3494. is too small to accommodate the peer address
  3495. </p>
  3496. </li>
  3497. <li><p>
  3498. Errno::EINTR - the (blocking) call was cancelled by an internal call to the
  3499. WinSock function WSACancelBlockingCall
  3500. </p>
  3501. </li>
  3502. <li><p>
  3503. Errno::EINPROGRESS - a blocking Windows Sockets 1.1 call is in progress or
  3504. the service provider is still processing a callback function
  3505. </p>
  3506. </li>
  3507. <li><p>
  3508. Errno::EINVAL - <tt>socket</tt> has not been bound with a call to
  3509. <em>bind</em>, or an unknown flag was specified, or MSG_OOB was specified
  3510. for a socket with SO_OOBINLINE enabled, or (for byte stream-style sockets
  3511. only) the internal len parameter on <tt>socket</tt> was zero or negative
  3512. </p>
  3513. </li>
  3514. <li><p>
  3515. Errno::EISCONN - <tt>socket</tt> is already connected. The call to
  3516. <em>recvfrom</em> is not permitted with a connected socket on a socket that
  3517. is connection oriented or connectionless.
  3518. </p>
  3519. </li>
  3520. <li><p>
  3521. Errno::ENETRESET - the connection has been broken due to the keep-alive
  3522. activity detecting a failure while the operation was in progress.
  3523. </p>
  3524. </li>
  3525. <li><p>
  3526. Errno::EOPNOTSUPP - MSG_OOB was specified, but <tt>socket</tt> is not
  3527. stream-style such as type SOCK_STREAM. OOB data is not supported in the
  3528. communication domain associated with <tt>socket</tt>, or <tt>socket</tt> is
  3529. unidirectional and supports only send operations
  3530. </p>
  3531. </li>
  3532. <li><p>
  3533. Errno::ESHUTDOWN - <tt>socket</tt> has been shutdown. It is not possible to
  3534. call <em>recvfrom</em> on a socket after <em>shutdown</em> has been
  3535. invoked.
  3536. </p>
  3537. </li>
  3538. <li><p>
  3539. Errno::EWOULDBLOCK - <tt>socket</tt> is marked as nonblocking and a call
  3540. to <em>recvfrom</em> would block.
  3541. </p>
  3542. </li>
  3543. <li><p>
  3544. Errno::EMSGSIZE - the message was too large to fit into the specified
  3545. buffer and was truncated.
  3546. </p>
  3547. </li>
  3548. <li><p>
  3549. Errno::ETIMEDOUT - the connection has been dropped, because of a network
  3550. failure or because the system on the other end went down without notice
  3551. </p>
  3552. </li>
  3553. <li><p>
  3554. Errno::ECONNRESET - the virtual circuit was reset by the remote side
  3555. executing a hard or abortive close. The application should close the
  3556. socket; it is no longer usable. On a UDP-datagram socket this error
  3557. indicates a previous send operation resulted in an ICMP Port Unreachable
  3558. message.
  3559. </p>
  3560. </li>
  3561. </ul>
  3562. </div>
  3563. </div>
  3564. <div class="tags">
  3565. <h3>Overloads:</h3>
  3566. <ul class="overload">
  3567. <li class="overload_item">
  3568. <span class="signature">- (<tt><span class='object_link'><a href="Array.html" title="Array (class)">Array</a></span></tt>) <strong>recvfrom</strong>(maxlen) </span>
  3569. <div class="docstring">
  3570. <div class="discussion">
  3571. </div>
  3572. </div>
  3573. <div class="tags">
  3574. <h3>Returns:</h3>
  3575. <ul class="return">
  3576. <li>
  3577. <span class='type'>(<tt><span class='object_link'><a href="Array.html" title="Array (class)">Array</a></span></tt>)</span>
  3578. </li>
  3579. </ul>
  3580. </div>
  3581. </li>
  3582. <li class="overload_item">
  3583. <span class="signature">- (<tt><span class='object_link'><a href="Array.html" title="Array (class)">Array</a></span></tt>) <strong>recvfrom</strong>(maxlen, flags) </span>
  3584. <div class="docstring">
  3585. <div class="discussion">
  3586. </div>
  3587. </div>
  3588. <div class="tags">
  3589. <h3>Returns:</h3>
  3590. <ul class="return">
  3591. <li>
  3592. <span class='type'>(<tt><span class='object_link'><a href="Array.html" title="Array (class)">Array</a></span></tt>)</span>
  3593. </li>
  3594. </ul>
  3595. </div>
  3596. </li>
  3597. </ul>
  3598. </div><table class="source_code">
  3599. <tr>
  3600. <td>
  3601. <pre class="lines">
  3602. </pre>
  3603. </td>
  3604. <td>
  3605. <pre class="code"><span class="info file"># File 'ext/socket/socket.c'</span>
  3606. /*
  3607. * call-seq:
  3608. * socket.recvfrom(maxlen) =&gt; [mesg, sender_addrinfo]
  3609. * socket.recvfrom(maxlen, flags) =&gt; [mesg, sender_addrinfo]
  3610. *
  3611. * Receives up to _maxlen_ bytes from +socket+. _flags_ is zero or more
  3612. * of the +MSG_+ options. The first element of the results, _mesg_, is the data
  3613. * received. The second element, _sender_addrinfo_, contains protocol-specific
  3614. * address information of the sender.
  3615. *
  3616. * === Parameters
  3617. * * +maxlen+ - the maximum number of bytes to receive from the socket
  3618. * * +flags+ - zero or more of the +MSG_+ options
  3619. *
  3620. * === Example
  3621. * # In one file, start this first
  3622. * require 'socket'
  3623. * include Socket::Constants
  3624. * socket = Socket.new( AF_INET, SOCK_STREAM, 0 )
  3625. * sockaddr = Socket.pack_sockaddr_in( 2200, 'localhost' )
  3626. * socket.bind( sockaddr )
  3627. * socket.listen( 5 )
  3628. * client, client_addrinfo = socket.accept
  3629. * data = client.recvfrom( 20 )[0].chomp
  3630. * puts &quot;I only received 20 bytes '#{data}'&quot;
  3631. * sleep 1
  3632. * socket.close
  3633. *
  3634. * # In another file, start this second
  3635. * require 'socket'
  3636. * include Socket::Constants
  3637. * socket = Socket.new( AF_INET, SOCK_STREAM, 0 )
  3638. * sockaddr = Socket.pack_sockaddr_in( 2200, 'localhost' )
  3639. * socket.connect( sockaddr )
  3640. * socket.puts &quot;Watch this get cut short!&quot;
  3641. * socket.close
  3642. *
  3643. * === Unix-based Exceptions
  3644. * On unix-based based systems the following system exceptions may be raised if the
  3645. * call to _recvfrom_ fails:
  3646. * * Errno::EAGAIN - the +socket+ file descriptor is marked as O_NONBLOCK and no
  3647. * data is waiting to be received; or MSG_OOB is set and no out-of-band data
  3648. * is available and either the +socket+ file descriptor is marked as
  3649. * O_NONBLOCK or the +socket+ does not support blocking to wait for
  3650. * out-of-band-data
  3651. * * Errno::EWOULDBLOCK - see Errno::EAGAIN
  3652. * * Errno::EBADF - the +socket+ is not a valid file descriptor
  3653. * * Errno::ECONNRESET - a connection was forcibly closed by a peer
  3654. * * Errno::EFAULT - the socket's internal buffer, address or address length
  3655. * cannot be accessed or written
  3656. * * Errno::EINTR - a signal interrupted _recvfrom_ before any data was available
  3657. * * Errno::EINVAL - the MSG_OOB flag is set and no out-of-band data is available
  3658. * * Errno::EIO - an i/o error occurred while reading from or writing to the
  3659. * filesystem
  3660. * * Errno::ENOBUFS - insufficient resources were available in the system to
  3661. * perform the operation
  3662. * * Errno::ENOMEM - insufficient memory was available to fulfill the request
  3663. * * Errno::ENOSR - there were insufficient STREAMS resources available to
  3664. * complete the operation
  3665. * * Errno::ENOTCONN - a receive is attempted on a connection-mode socket that
  3666. * is not connected
  3667. * * Errno::ENOTSOCK - the +socket+ does not refer to a socket
  3668. * * Errno::EOPNOTSUPP - the specified flags are not supported for this socket type
  3669. * * Errno::ETIMEDOUT - the connection timed out during connection establishment
  3670. * or due to a transmission timeout on an active connection
  3671. *
  3672. * === Windows Exceptions
  3673. * On Windows systems the following system exceptions may be raised if
  3674. * the call to _recvfrom_ fails:
  3675. * * Errno::ENETDOWN - the network is down
  3676. * * Errno::EFAULT - the internal buffer and from parameters on +socket+ are not
  3677. * part of the user address space, or the internal fromlen parameter is
  3678. * too small to accommodate the peer address
  3679. * * Errno::EINTR - the (blocking) call was cancelled by an internal call to
  3680. * the WinSock function WSACancelBlockingCall
  3681. * * Errno::EINPROGRESS - a blocking Windows Sockets 1.1 call is in progress or
  3682. * the service provider is still processing a callback function
  3683. * * Errno::EINVAL - +socket+ has not been bound with a call to _bind_, or an
  3684. * unknown flag was specified, or MSG_OOB was specified for a socket with
  3685. * SO_OOBINLINE enabled, or (for byte stream-style sockets only) the internal
  3686. * len parameter on +socket+ was zero or negative
  3687. * * Errno::EISCONN - +socket+ is already connected. The call to _recvfrom_ is
  3688. * not permitted with a connected socket on a socket that is connection
  3689. * oriented or connectionless.
  3690. * * Errno::ENETRESET - the connection has been broken due to the keep-alive
  3691. * activity detecting a failure while the operation was in progress.
  3692. * * Errno::EOPNOTSUPP - MSG_OOB was specified, but +socket+ is not stream-style
  3693. * such as type SOCK_STREAM. OOB data is not supported in the communication
  3694. * domain associated with +socket+, or +socket+ is unidirectional and
  3695. * supports only send operations
  3696. * * Errno::ESHUTDOWN - +socket+ has been shutdown. It is not possible to
  3697. * call _recvfrom_ on a socket after _shutdown_ has been invoked.
  3698. * * Errno::EWOULDBLOCK - +socket+ is marked as nonblocking and a call to
  3699. * _recvfrom_ would block.
  3700. * * Errno::EMSGSIZE - the message was too large to fit into the specified buffer
  3701. * and was truncated.
  3702. * * Errno::ETIMEDOUT - the connection has been dropped, because of a network
  3703. * failure or because the system on the other end went down without
  3704. * notice
  3705. * * Errno::ECONNRESET - the virtual circuit was reset by the remote side
  3706. * executing a hard or abortive close. The application should close the
  3707. * socket; it is no longer usable. On a UDP-datagram socket this error
  3708. * indicates a previous send operation resulted in an ICMP Port Unreachable
  3709. * message.
  3710. */
  3711. static VALUE
  3712. sock_recvfrom(int argc, VALUE *argv, VALUE sock)
  3713. {
  3714. return rsock_s_recvfrom(sock, argc, argv, RECV_SOCKET);
  3715. }</pre>
  3716. </td>
  3717. </tr>
  3718. </table>
  3719. </div>
  3720. <div class="method_details ">
  3721. <p class="signature " id="recvfrom_nonblock-instance_method">
  3722. <span class="overload">- (<tt><span class='object_link'><a href="Array.html" title="Array (class)">Array</a></span></tt>) <strong>recvfrom_nonblock</strong>(maxlen) </span>
  3723. <span class="overload">- (<tt><span class='object_link'><a href="Array.html" title="Array (class)">Array</a></span></tt>) <strong>recvfrom_nonblock</strong>(maxlen, flags) </span>
  3724. </p><div class="docstring">
  3725. <div class="discussion">
  3726. <p>
  3727. Receives up to <em>maxlen</em> bytes from <tt>socket</tt> using recvfrom(2)
  3728. after O_NONBLOCK is set for the underlying file descriptor. <em>flags</em>
  3729. is zero or more of the <tt>MSG_</tt> options. The first element of the
  3730. results, <em>mesg</em>, is the data received. The second element,
  3731. <em>sender_addrinfo</em>, contains protocol-specific address information of
  3732. the sender.
  3733. </p>
  3734. <p>
  3735. When recvfrom(2) returns 0, Socket#recvfrom_nonblock returns an empty
  3736. string as data. The meaning depends on the socket: EOF on TCP, empty packet
  3737. on UDP, etc.
  3738. </p>
  3739. <h3>Parameters</h3>
  3740. <ul>
  3741. <li><p>
  3742. <tt>maxlen</tt> - the maximum number of bytes to receive from the socket
  3743. </p>
  3744. </li>
  3745. <li><p>
  3746. <tt>flags</tt> - zero or more of the <tt>MSG_</tt> options
  3747. </p>
  3748. </li>
  3749. </ul>
  3750. <h3>Example</h3>
  3751. <pre class="code">
  3752. # In one file, start this first
  3753. require 'socket'
  3754. include Socket::Constants
  3755. socket = Socket.new(AF_INET, SOCK_STREAM, 0)
  3756. sockaddr = Socket.sockaddr_in(2200, 'localhost')
  3757. socket.bind(sockaddr)
  3758. socket.listen(5)
  3759. client, client_addrinfo = socket.accept
  3760. begin # emulate blocking recvfrom
  3761. pair = client.recvfrom_nonblock(20)
  3762. rescue IO::WaitReadable
  3763. IO.select([client])
  3764. retry
  3765. end
  3766. data = pair[0].chomp
  3767. puts &quot;I only received 20 bytes '#{data}'&quot;
  3768. sleep 1
  3769. socket.close
  3770. # In another file, start this second
  3771. require 'socket'
  3772. include Socket::Constants
  3773. socket = Socket.new(AF_INET, SOCK_STREAM, 0)
  3774. sockaddr = Socket.sockaddr_in(2200, 'localhost')
  3775. socket.connect(sockaddr)
  3776. socket.puts &quot;Watch this get cut short!&quot;
  3777. socket.close
  3778. </pre>
  3779. <p>
  3780. Refer to Socket#recvfrom for the exceptions that may be thrown if the call
  3781. to <em>recvfrom_nonblock</em> fails.
  3782. </p>
  3783. <p>
  3784. Socket#recvfrom_nonblock may raise any error corresponding to recvfrom(2)
  3785. failure, including Errno::EWOULDBLOCK.
  3786. </p>
  3787. <p>
  3788. If the exception is Errno::EWOULDBLOCK or Errno::AGAIN, it is extended by
  3789. IO::WaitReadable. So IO::WaitReadable can be used to rescue the exceptions
  3790. for retrying recvfrom_nonblock.
  3791. </p>
  3792. <h3>See</h3>
  3793. <ul>
  3794. <li><p>
  3795. Socket#recvfrom
  3796. </p>
  3797. </li>
  3798. </ul>
  3799. </div>
  3800. </div>
  3801. <div class="tags">
  3802. <h3>Overloads:</h3>
  3803. <ul class="overload">
  3804. <li class="overload_item">
  3805. <span class="signature">- (<tt><span class='object_link'><a href="Array.html" title="Array (class)">Array</a></span></tt>) <strong>recvfrom_nonblock</strong>(maxlen) </span>
  3806. <div class="docstring">
  3807. <div class="discussion">
  3808. </div>
  3809. </div>
  3810. <div class="tags">
  3811. <h3>Returns:</h3>
  3812. <ul class="return">
  3813. <li>
  3814. <span class='type'>(<tt><span class='object_link'><a href="Array.html" title="Array (class)">Array</a></span></tt>)</span>
  3815. </li>
  3816. </ul>
  3817. </div>
  3818. </li>
  3819. <li class="overload_item">
  3820. <span class="signature">- (<tt><span class='object_link'><a href="Array.html" title="Array (class)">Array</a></span></tt>) <strong>recvfrom_nonblock</strong>(maxlen, flags) </span>
  3821. <div class="docstring">
  3822. <div class="discussion">
  3823. </div>
  3824. </div>
  3825. <div class="tags">
  3826. <h3>Returns:</h3>
  3827. <ul class="return">
  3828. <li>
  3829. <span class='type'>(<tt><span class='object_link'><a href="Array.html" title="Array (class)">Array</a></span></tt>)</span>
  3830. </li>
  3831. </ul>
  3832. </div>
  3833. </li>
  3834. </ul>
  3835. </div><table class="source_code">
  3836. <tr>
  3837. <td>
  3838. <pre class="lines">
  3839. </pre>
  3840. </td>
  3841. <td>
  3842. <pre class="code"><span class="info file"># File 'ext/socket/socket.c'</span>
  3843. /*
  3844. * call-seq:
  3845. * socket.recvfrom_nonblock(maxlen) =&gt; [mesg, sender_addrinfo]
  3846. * socket.recvfrom_nonblock(maxlen, flags) =&gt; [mesg, sender_addrinfo]
  3847. *
  3848. * Receives up to _maxlen_ bytes from +socket+ using recvfrom(2) after
  3849. * O_NONBLOCK is set for the underlying file descriptor.
  3850. * _flags_ is zero or more of the +MSG_+ options.
  3851. * The first element of the results, _mesg_, is the data received.
  3852. * The second element, _sender_addrinfo_, contains protocol-specific address
  3853. * information of the sender.
  3854. *
  3855. * When recvfrom(2) returns 0, Socket#recvfrom_nonblock returns
  3856. * an empty string as data.
  3857. * The meaning depends on the socket: EOF on TCP, empty packet on UDP, etc.
  3858. *
  3859. * === Parameters
  3860. * * +maxlen+ - the maximum number of bytes to receive from the socket
  3861. * * +flags+ - zero or more of the +MSG_+ options
  3862. *
  3863. * === Example
  3864. * # In one file, start this first
  3865. * require 'socket'
  3866. * include Socket::Constants
  3867. * socket = Socket.new(AF_INET, SOCK_STREAM, 0)
  3868. * sockaddr = Socket.sockaddr_in(2200, 'localhost')
  3869. * socket.bind(sockaddr)
  3870. * socket.listen(5)
  3871. * client, client_addrinfo = socket.accept
  3872. * begin # emulate blocking recvfrom
  3873. * pair = client.recvfrom_nonblock(20)
  3874. * rescue IO::WaitReadable
  3875. * IO.select([client])
  3876. * retry
  3877. * end
  3878. * data = pair[0].chomp
  3879. * puts &quot;I only received 20 bytes '#{data}'&quot;
  3880. * sleep 1
  3881. * socket.close
  3882. *
  3883. * # In another file, start this second
  3884. * require 'socket'
  3885. * include Socket::Constants
  3886. * socket = Socket.new(AF_INET, SOCK_STREAM, 0)
  3887. * sockaddr = Socket.sockaddr_in(2200, 'localhost')
  3888. * socket.connect(sockaddr)
  3889. * socket.puts &quot;Watch this get cut short!&quot;
  3890. * socket.close
  3891. *
  3892. * Refer to Socket#recvfrom for the exceptions that may be thrown if the call
  3893. * to _recvfrom_nonblock_ fails.
  3894. *
  3895. * Socket#recvfrom_nonblock may raise any error corresponding to recvfrom(2) failure,
  3896. * including Errno::EWOULDBLOCK.
  3897. *
  3898. * If the exception is Errno::EWOULDBLOCK or Errno::AGAIN,
  3899. * it is extended by IO::WaitReadable.
  3900. * So IO::WaitReadable can be used to rescue the exceptions for retrying recvfrom_nonblock.
  3901. *
  3902. * === See
  3903. * * Socket#recvfrom
  3904. */
  3905. static VALUE
  3906. sock_recvfrom_nonblock(int argc, VALUE *argv, VALUE sock)
  3907. {
  3908. return rsock_s_recvfrom_nonblock(sock, argc, argv, RECV_SOCKET);
  3909. }</pre>
  3910. </td>
  3911. </tr>
  3912. </table>
  3913. </div>
  3914. <div class="method_details ">
  3915. <p class="signature " id="sysaccept-instance_method">
  3916. - (<tt><span class='object_link'><a href="Array.html" title="Array (class)">Array</a></span></tt>) <strong>sysaccept</strong>
  3917. </p><div class="docstring">
  3918. <div class="discussion">
  3919. <p>
  3920. Accepts an incoming connection returning an array containing the (integer)
  3921. file descriptor for the incoming connection, <em>client_socket_fd</em>, and
  3922. an Addrinfo, <em>client_addrinfo</em>.
  3923. </p>
  3924. <h3>Example</h3>
  3925. <pre class="code">
  3926. # In one script, start this first
  3927. require 'socket'
  3928. include Socket::Constants
  3929. socket = Socket.new( AF_INET, SOCK_STREAM, 0 )
  3930. sockaddr = Socket.pack_sockaddr_in( 2200, 'localhost' )
  3931. socket.bind( sockaddr )
  3932. socket.listen( 5 )
  3933. client_fd, client_addrinfo = socket.sysaccept
  3934. client_socket = Socket.for_fd( client_fd )
  3935. puts &quot;The client said, '#{client_socket.readline.chomp}'&quot;
  3936. client_socket.puts &quot;Hello from script one!&quot;
  3937. socket.close
  3938. # In another script, start this second
  3939. require 'socket'
  3940. include Socket::Constants
  3941. socket = Socket.new( AF_INET, SOCK_STREAM, 0 )
  3942. sockaddr = Socket.pack_sockaddr_in( 2200, 'localhost' )
  3943. socket.connect( sockaddr )
  3944. socket.puts &quot;Hello from script 2.&quot;
  3945. puts &quot;The server said, '#{socket.readline.chomp}'&quot;
  3946. socket.close
  3947. </pre>
  3948. <p>
  3949. Refer to Socket#accept for the exceptions that may be thrown if the call to
  3950. <em>sysaccept</em> fails.
  3951. </p>
  3952. <h3>See</h3>
  3953. <ul>
  3954. <li><p>
  3955. Socket#accept
  3956. </p>
  3957. </li>
  3958. </ul>
  3959. </div>
  3960. </div>
  3961. <div class="tags">
  3962. <div class="docstring">
  3963. <div class="discussion">
  3964. </div>
  3965. </div>
  3966. <div class="tags">
  3967. <h3>Returns:</h3>
  3968. <ul class="return">
  3969. <li>
  3970. <span class='type'>(<tt><span class='object_link'><a href="Array.html" title="Array (class)">Array</a></span></tt>)</span>
  3971. </li>
  3972. </ul>
  3973. </div>
  3974. </div><table class="source_code">
  3975. <tr>
  3976. <td>
  3977. <pre class="lines">
  3978. </pre>
  3979. </td>
  3980. <td>
  3981. <pre class="code"><span class="info file"># File 'ext/socket/socket.c'</span>
  3982. /*
  3983. * call-seq:
  3984. * socket.sysaccept =&gt; [client_socket_fd, client_addrinfo]
  3985. *
  3986. * Accepts an incoming connection returning an array containing the (integer)
  3987. * file descriptor for the incoming connection, _client_socket_fd_,
  3988. * and an Addrinfo, _client_addrinfo_.
  3989. *
  3990. * === Example
  3991. * # In one script, start this first
  3992. * require 'socket'
  3993. * include Socket::Constants
  3994. * socket = Socket.new( AF_INET, SOCK_STREAM, 0 )
  3995. * sockaddr = Socket.pack_sockaddr_in( 2200, 'localhost' )
  3996. * socket.bind( sockaddr )
  3997. * socket.listen( 5 )
  3998. * client_fd, client_addrinfo = socket.sysaccept
  3999. * client_socket = Socket.for_fd( client_fd )
  4000. * puts &quot;The client said, '#{client_socket.readline.chomp}'&quot;
  4001. * client_socket.puts &quot;Hello from script one!&quot;
  4002. * socket.close
  4003. *
  4004. * # In another script, start this second
  4005. * require 'socket'
  4006. * include Socket::Constants
  4007. * socket = Socket.new( AF_INET, SOCK_STREAM, 0 )
  4008. * sockaddr = Socket.pack_sockaddr_in( 2200, 'localhost' )
  4009. * socket.connect( sockaddr )
  4010. * socket.puts &quot;Hello from script 2.&quot;
  4011. * puts &quot;The server said, '#{socket.readline.chomp}'&quot;
  4012. * socket.close
  4013. *
  4014. * Refer to Socket#accept for the exceptions that may be thrown if the call
  4015. * to _sysaccept_ fails.
  4016. *
  4017. * === See
  4018. * * Socket#accept
  4019. */
  4020. static VALUE
  4021. sock_sysaccept(VALUE sock)
  4022. {
  4023. rb_io_t *fptr;
  4024. VALUE sock2;
  4025. struct sockaddr_storage buf;
  4026. socklen_t len = (socklen_t)sizeof buf;
  4027. GetOpenFile(sock, fptr);
  4028. sock2 = rsock_s_accept(0,fptr-&gt;fd,(struct sockaddr*)&amp;buf,&amp;len);
  4029. return rb_assoc_new(sock2, rsock_io_socket_addrinfo(sock2, (struct sockaddr*)&amp;buf, len));
  4030. }</pre>
  4031. </td>
  4032. </tr>
  4033. </table>
  4034. </div>
  4035. </div>
  4036. </div>
  4037. <div id="footer">
  4038. Generated on Thu Oct 14 00:01:30 2010 by
  4039. <a href="http://yardoc.org" title="Yay! A Ruby Documentation Tool" target="_parent">yard</a>
  4040. 0.6.1 (ruby-1.9.2).
  4041. </div>
  4042. </body>
  4043. </html>