当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


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。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。