Maintenance Ruby 3.4 gets fixes, but no new features. Switch to Ruby 4.0 →
class method
Socket.new
(::Symbol | ::Integer domain, ::Symbol | ::Integer socktype, ?::Integer protocol) -> ::Socket
Socket.new(domain, socktype [, protocol]) => socket
ext/socket/socket.c
View on GitHub ↗
static VALUE
sock_initialize(int argc, VALUE *argv, VALUE sock)
{
VALUE domain, type, protocol;
int fd;
int d, t;
rb_scan_args(argc, argv, "21", &domain, &type, &protocol);
if (NIL_P(protocol))
protocol = INT2FIX(0);
setup_domain_and_type(domain, &d, type, &t);
fd = rsock_socket(d, t, NUM2INT(protocol));
if (fd < 0) rb_sys_fail("socket(2)");
return rsock_init_sock(sock, fd);
}
Creates a new socket object.
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 is optional and should be a protocol defined in the domain. If protocol is not given, 0 is used internally.
Example
Socket.new(:INET, :STREAM) # TCP socket Socket.new(:INET, :DGRAM) # UDP socket Socket.new(:UNIX, :STREAM) # UNIX stream socket Socket.new(:UNIX, :DGRAM) # UNIX datagram socket
See also
.ip_address_list
.pack_sockaddr_in
.ip_address?
.pack_sockaddr_un
also in
4.1 · 4.0 · 3.3 · 3.2 · 3.1 · 3.0