socketpair(p1, p2, p3 = v3)
public
Creates a pair of sockets connected
each other.
domain should be a communications domain such as: :INET, :INET6,
:UNIX, etc.
socktype should be a socket type such as: :STREAM, :DGRAM, :RAW,
etc.
protocol should be a protocol defined in the domain. 0 is default
protocol for the domain.
s1, s2 = Socket.pair(:UNIX, :DGRAM, 0)
s1.send "a", 0
s1.send "b", 0
p s2.recv(10)
p s2.recv(10)
VALUE
rsock_sock_s_socketpair(int argc, VALUE *argv, VALUE klass)
{
VALUE domain, type, protocol;
int d, t, p, sp[2];
int ret;
VALUE s1, s2, r;
rb_scan_args(argc, argv, "21", &domain, &type, &protocol);
if (NIL_P(protocol))
protocol = INT2FIX(0);
setup_domain_and_type(domain, &d, type, &t);
p = NUM2INT(protocol);
ret = socketpair(d, t, p, sp);
if (ret < 0 && (errno == EMFILE || errno == ENFILE)) {
rb_gc();
ret = socketpair(d, t, p, sp);
}
if (ret < 0) {
rb_sys_fail("socketpair(2)");
}
rb_update_max_fd(sp[0]);
rb_update_max_fd(sp[1]);
s1 = rsock_init_sock(rb_obj_alloc(klass), sp[0]);
s2 = rsock_init_sock(rb_obj_alloc(klass), sp[1]);
r = rb_assoc_new(s1, s2);
if (rb_block_given_p()) {
return rb_ensure(pair_yield, r, io_close, s1);
}
return r;
}