Name

IPSocket — IP socket class

Synopsis

IPSocket class is a base class of TCPSocket and UDPSocket. IPSocket class provides common behavior among Internet Protocol (IP) sockets. Sockets classes in Ruby support IPv6, if the native platform supports it.

Required Library

require ’socket’

Inherited Class

BasicSocket

Class Method

IPSocket::getaddress(host)

Returns the IP address of the specified host. The IP address is returned as a string such as 127.10.0.1 (IPv4) or ::1 (IPv6).

Instance Methods

s.addr

Returns an array containing information on the socket connection (AF_INET, port, hostname, and IP address)

s = TCPSocket.open("www.ruby-lang.org", "http")
s.addr# => ["AF_INET", 4030, "dhcp198.priv.netlab.jp", 
     "192.168.1.198"]
s.peeraddr

Returns an array containing information on the peer socket in the same format as s.addr

s = TCPSocket.open("www.ruby-lang.org", "daytime")
s.recvfrom(255)
# => ["Wed Aug  1 00:30:54 2001\r\n", ["AF_INET", 13, "www", 
      "210.251.121.214"]]
s.recvfrom(len[, flags])

Receives data and returns it in an array that also includes information on the sender’s socket in the same format as s.addr

Get Ruby in a Nutshell now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.