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