當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


Ruby Socket.sysaccept用法及代碼示例


本文簡要介紹ruby語言中 Socket.sysaccept 的用法。

用法

sysaccept → [client_socket_fd, client_addrinfo]

接受傳入連接,返回一個數組,其中包含傳入連接的(整數)文件說明符 client_socket_fd Addrinfo client_addrinfo

示例

# In one script, start this first
require 'socket'
include Socket::Constants
socket = Socket.new( AF_INET, SOCK_STREAM, 0 )
sockaddr = Socket.pack_sockaddr_in( 2200, 'localhost' )
socket.bind( sockaddr )
socket.listen( 5 )
client_fd, client_addrinfo = socket.sysaccept
client_socket = Socket.for_fd( client_fd )
puts "The client said, '#{client_socket.readline.chomp}'"
client_socket.puts "Hello from script one!"
socket.close

# In another script, start this second
require 'socket'
include Socket::Constants
socket = Socket.new( AF_INET, SOCK_STREAM, 0 )
sockaddr = Socket.pack_sockaddr_in( 2200, 'localhost' )
socket.connect( sockaddr )
socket.puts "Hello from script 2."
puts "The server said, '#{socket.readline.chomp}'"
socket.close

如果對 sysaccept 的調用失敗,請參閱 Socket#accept 以了解可能引發的異常。

參看

相關用法


注:本文由純淨天空篩選整理自ruby-lang.org大神的英文原創作品 Socket.sysaccept。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。