accept_nonblock()
public
Accepts an incoming connection using accept(2) after O_NONBLOCK is set for
the underlying file descriptor. It returns an array containg the accpeted
socket for the incoming connection, client_socket, and a string
that contains the struct sockaddr information about the caller,
client_sockaddr.
Example
require 'socket'
include Socket::Constants
socket = Socket.new(AF_INET, SOCK_STREAM, 0)
sockaddr = Socket.sockaddr_in(2200, 'localhost')
socket.bind(sockaddr)
socket.listen(5)
begin
client_socket, client_sockaddr = socket.accept_nonblock
rescue Errno::EAGAIN, Errno::EWOULDBLOCK, Errno::ECONNABORTED, Errno::EPROTO, Errno::EINTR
IO.select([socket])
retry
end
puts "The client said, '#{client_socket.readline.chomp}'"
client_socket.puts "Hello from script one!"
socket.close
require 'socket'
include Socket::Constants
socket = Socket.new(AF_INET, SOCK_STREAM, 0)
sockaddr = Socket.sockaddr_in(2200, 'localhost')
socket.connect(sockaddr)
socket.puts "Hello from script 2."
puts "The server said, '#{socket.readline.chomp}'"
socket.close
Refer to Socket#accept for the exceptions
that may be thrown if the call to accept_nonblock fails.
Socket#accept_nonblock may raise
any error corresponding to accept(2) failure, including Errno::EAGAIN.
See
Show source
/*
* call-seq:
* socket.accept_nonblock => [client_socket, client_sockaddr]
*
* Accepts an incoming connection using accept(2) after
* O_NONBLOCK is set for the underlying file descriptor.
* It returns an array containg the accpeted socket
* for the incoming connection, _client_socket_,
* and a string that contains the +struct+ sockaddr information
* about the caller, _client_sockaddr_.
*
* === Example
* # In one script, start this first
* require 'socket'
* include Socket::Constants
* socket = Socket.new(AF_INET, SOCK_STREAM, 0)
* sockaddr = Socket.sockaddr_in(2200, 'localhost')
* socket.bind(sockaddr)
* socket.listen(5)
* begin
* client_socket, client_sockaddr = socket.accept_nonblock
* rescue Errno::EAGAIN, Errno::EWOULDBLOCK, Errno::ECONNABORTED, Errno::EPROTO, Errno::EINTR
* IO.select([socket])
* retry
* end
* 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.sockaddr_in(2200, 'localhost')
* socket.connect(sockaddr)
* socket.puts "Hello from script 2."
* puts "The server said, '#{socket.readline.chomp}'"
* socket.close
*
* Refer to Socket#accept for the exceptions that may be thrown if the call
* to _accept_nonblock_ fails.
*
* Socket#accept_nonblock may raise any error corresponding to accept(2) failure,
* including Errno::EAGAIN.
*
* === See
* * Socket#accept
*/
static VALUE
sock_accept_nonblock(sock)
VALUE sock;
{
rb_io_t *fptr;
VALUE sock2;
char buf[1024];
socklen_t len = sizeof buf;
GetOpenFile(sock, fptr);
sock2 = s_accept_nonblock(rb_cSocket, fptr, (struct sockaddr *)buf, &len);
return rb_assoc_new(sock2, rb_str_new(buf, len));
}