connect_nonblock(p1)
public
Requests a connection to be made on the given server_sockaddr
after O_NONBLOCK is set for the underlying file descriptor. Returns 0 if
successful, otherwise an exception is raised.
Parameter
Example:
require 'socket'
include Socket::Constants
socket = Socket.new(AF_INET, SOCK_STREAM, 0)
sockaddr = Socket.sockaddr_in(80, 'www.google.com')
begin
socket.connect_nonblock(sockaddr)
rescue Errno::EINPROGRESS
IO.select(nil, [socket])
begin
socket.connect_nonblock(sockaddr)
rescue Errno::EISCONN
end
end
socket.write("GET / HTTP/1.0\r\n\r\n")
results = socket.read
Refer to Socket#connect for the
exceptions that may be thrown if the call to connect_nonblock fails.
Socket#connect_nonblock may
raise any error corresponding to connect(2) failure, including
Errno::EINPROGRESS.
See
Show source
/*
* call-seq:
* socket.connect_nonblock(server_sockaddr) => 0
*
* Requests a connection to be made on the given +server_sockaddr+ after
* O_NONBLOCK is set for the underlying file descriptor.
* Returns 0 if successful, otherwise an exception is raised.
*
* === Parameter
* * +server_sockaddr+ - the +struct+ sockaddr contained in a string
*
* === Example:
* # Pull down Google's web page
* require 'socket'
* include Socket::Constants
* socket = Socket.new(AF_INET, SOCK_STREAM, 0)
* sockaddr = Socket.sockaddr_in(80, 'www.google.com')
* begin
* socket.connect_nonblock(sockaddr)
* rescue Errno::EINPROGRESS
* IO.select(nil, [socket])
* begin
* socket.connect_nonblock(sockaddr)
* rescue Errno::EISCONN
* end
* end
* socket.write("GET / HTTP/1.0\r\n\r\n")
* results = socket.read
*
* Refer to Socket#connect for the exceptions that may be thrown if the call
* to _connect_nonblock_ fails.
*
* Socket#connect_nonblock may raise any error corresponding to connect(2) failure,
* including Errno::EINPROGRESS.
*
* === See
* * Socket#connect
*/
static VALUE
sock_connect_nonblock(sock, addr)
VALUE sock, addr;
{
rb_io_t *fptr;
int n;
StringValue(addr);
addr = rb_str_new4(addr);
GetOpenFile(sock, fptr);
rb_io_set_nonblock(fptr);
n = connect(fileno(fptr->f), (struct sockaddr*)RSTRING(addr)->ptr, RSTRING(addr)->len);
if (n < 0) {
rb_sys_fail("connect(2)");
}
return INT2FIX(n);
}