Class: BasicSocket

Inherits:
IO
  • Object
show all
Defined in:
socket.c

Direct Known Subclasses

IPSocket, Socket, UNIXSocket

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.do_not_reverse_lookupObject



763
764
765
766
767
# File 'socket.c', line 763

static VALUE
bsock_do_not_rev_lookup()
{
    return do_not_reverse_lookup?Qtrue:Qfalse;
}

.do_not_reverse_lookup=Object



769
770
771
# File 'socket.c', line 769

static VALUE
bsock_do_not_rev_lookup_set(self, val)
VALUE self, val;

.for_fdObject



258
259
260
# File 'socket.c', line 258

static VALUE
bsock_s_for_fd(klass, fd)
VALUE klass, fd;

Instance Method Details

#close_readObject



299
300
301
# File 'socket.c', line 299

static VALUE
bsock_close_read(sock)
VALUE sock;

#close_writeObject



318
319
320
# File 'socket.c', line 318

static VALUE
bsock_close_write(sock)
VALUE sock;

#getpeernameObject



502
503
504
# File 'socket.c', line 502

static VALUE
bsock_getpeername(sock)
VALUE sock;

#getsocknameObject



488
489
490
# File 'socket.c', line 488

static VALUE
bsock_getsockname(sock)
VALUE sock;

#getsockopt(level, optname) ⇒ Object

Gets a socket option. These are protocol and system specific, see your local sytem documentation for details. The option is returned as a String with the data being the binary value of the socket option.

Parameters

  • level is an integer, usually one of the SOL_ constants such as Socket::SOL_SOCKET, or a protocol level.
  • optname is an integer, usually one of the SO_ constants, such as Socket::SO_REUSEADDR.

Examples

Some socket options are integers with boolean values, in this case #getsockopt could be called like this: optval = sock.getsockopt(Socket::SOL_SOCKET,Socket::SO_REUSEADDR) optval = optval.unpack "i" reuseaddr = optval == 0 ? false : true

Some socket options are integers with numeric values, in this case #getsockopt could be called like this: optval = sock.getsockopt(Socket::IPPROTO_IP, Socket::IP_TTL) ipttl = optval.unpack("i")[0]

Option values may be structs. Decoding them can be complex as it involves examining your system headers to determine the correct definition. An example is a struct linger, which may be defined in your system headers as: struct linger { int l_onoff; int l_linger; };

In this case #getsockopt could be called like this: optval = sock.getsockopt(Socket::SOL_SOCKET, Socket::SO_LINGER) onoff, linger = optval.unpack "ii"



462
463
464
# File 'socket.c', line 462

static VALUE
bsock_getsockopt(sock, lev, optname)
VALUE sock, lev, optname;

#recvObject



708
709
710
# File 'socket.c', line 708

static VALUE
bsock_recv(argc, argv, sock)
int argc;

#recv_nonblock(maxlen) ⇒ Object #recv_nonblock(maxlen, flags) ⇒ Object

Receives up to maxlen bytes from socket using recvfrom(2) after O_NONBLOCK is set for the underlying file descriptor. flags is zero or more of the MSG_ options. The result, mesg, is the data received.

When recvfrom(2) returns 0, Socket#recv_nonblock returns an empty string as data. The meaning depends on the socket: EOF on TCP, empty packet on UDP, etc.

Parameters

  • maxlen - the number of bytes to receive from the socket
  • flags - zero or more of the MSG_ options

Example

serv = TCPServer.new("127.0.0.1", 0)
af, port, host, addr = serv.addr
c = TCPSocket.new(addr, port)
s = serv.accept
c.send "aaa", 0
IO.select([s])
p s.recv_nonblock(10) #=> "aaa"

Refer to Socket#recvfrom for the exceptions that may be thrown if the call to recv_nonblock fails.

BasicSocket#recv_nonblock may raise any error corresponding to recvfrom(2) failure, including Errno::EAGAIN.

See

  • Socket#recvfrom


754
755
756
# File 'socket.c', line 754

static VALUE
bsock_recv_nonblock(argc, argv, sock)
int argc;

#sendObject



516
517
518
# File 'socket.c', line 516

static VALUE
bsock_send(argc, argv, sock)
int argc;

#setsockopt(level, optname, optval) ⇒ Object

Sets a socket option. These are protocol and system specific, see your local sytem documentation for details.

Parameters

  • level is an integer, usually one of the SOL_ constants such as Socket::SOL_SOCKET, or a protocol level.
  • optname is an integer, usually one of the SO_ constants, such as Socket::SO_REUSEADDR.
  • optval is the value of the option, it is passed to the underlying setsockopt() as a pointer to a certain number of bytes. How this is done depends on the type:
    • Fixnum: value is assigned to an int, and a pointer to the int is passed, with length of sizeof(int).
    • true or false: 1 or 0 (respectively) is assigned to an int, and the int is passed as for a Fixnum. Note that false must be passed, not nil.
    • String: the string's data and length is passed to the socket.

Examples

Some socket options are integers with boolean values, in this case #setsockopt could be called like this: sock.setsockopt(Socket::SOL_SOCKET,Socket::SO_REUSEADDR, true)

Some socket options are integers with numeric values, in this case #setsockopt could be called like this: sock.setsockopt(Socket::IPPROTO_IP, Socket::IP_TTL, 255)

Option values may be structs. Passing them can be complex as it involves examining your system headers to determine the correct definition. An example is an ip_mreq, which may be defined in your system headers as: struct ip_mreq { struct in_addr imr_multiaddr; struct in_addr imr_interface; };

In this case #setsockopt could be called like this: optval = IPAddr.new("224.0.0.251") + Socket::INADDR_ANY sock.setsockopt(Socket::IPPROTO_IP, Socket::IP_ADD_MEMBERSHIP, optval)



382
383
384
# File 'socket.c', line 382

static VALUE
bsock_setsockopt(sock, lev, optname, val)
VALUE sock, lev, optname, val;

#shutdownObject



270
271
272
# File 'socket.c', line 270

static VALUE
bsock_shutdown(argc, argv, sock)
int argc;