Type:
Class
Constants:
AF_INET6 : Object.new

IPv6 protocol family

Class Socket provides access to the underlying operating system socket implementations. It can be used to provide more operating system specific functionality than the protocol-specific socket classes.

The constants defined under Socket::Constants are also defined under Socket. For example, Socket::AF_INET is usable as well as Socket::Constants::AF_INET. See Socket::Constants for the list of constants.

What's a socket?

Sockets are endpoints of a bidirectionnal communication channel. Sockets can communicate within a process, between processes on the same machine or between different machines. There are many types of socket: TCPSocket, UDPSocket or UNIXSocket for example.

Sockets have their own vocabulary:

domain: The family of protocols:

  • Socket::PF_INET

  • Socket::PF_INET6

  • Socket::PF_UNIX

  • etc.

type: The type of communications between the two endpoints, typically

  • Socket::SOCK_STREAM

  • Socket::SOCK_DGRAM.

protocol: Typically zero. This may be used to identify a variant of a protocol.

hostname: The identifier of a network interface:

  • a string (hostname, IPv4 or IPv6 adress or broadcast which specifies a broadcast address)

  • a zero-length string which specifies INADDR_ANY

  • an integer (interpreted as binary address in host byte order).

Quick start

Many of the classes, such as TCPSocket, UDPSocket or UNIXSocket, ease the use of sockets comparatively to the equivalent C programming interface.

Let's create an internet socket using the IPv4 protocol in a C-like manner:

1
2
s = Socket.new Socket::AF_INET, Socket::SOCK_STREAM
s.connect Socket.pack_sockaddr_in(80, 'example.com')

You could also use the TCPSocket class:

1
s = TCPSocket.new 'example.com', 80

A simple server might look like this:

1
2
3
4
5
6
7
8
9
10
require 'socket'
 
server = TCPServer.new 2000 # Server bound to port 2000
 
loop do
  client = server.accept    # Wait for a client to connect
  client.puts "Hello !"
  client.puts "Time is #{Time.now}"
  client.close
end

A simple client may look like this:

1
2
3
4
5
6
7
8
9
require 'socket'
 
s = TCPSocket.new 'localhost', 2000
 
while line = s.gets # Read lines from socket
  puts line         # and print them
end
 
s.close             # close socket when done

Exception Handling

Ruby's Socket implementation raises exceptions based on the error generated by the system dependent implementation. This is why the methods are documented in a way that isolate Unix-based system exceptions from Windows based exceptions. If more information on a particular exception is needed, please refer to the Unix manual pages or the Windows WinSock reference.

Convenience methods

Although the general way to create socket is ::new, there are several methods of socket creation for most cases.

TCP client socket

::tcp, IO.open

TCP server socket

::tcp_server_loop, IO.open

UNIX client socket

::unix, IO.open

UNIX server socket

::unix_server_loop, IO.open

Documentation by

  • Zach Dennis

  • Sam Roberts

  • Programming Ruby from The Pragmatic Bookshelf.

Much material in this documentation is taken with permission from Programming Ruby from The Pragmatic Bookshelf.

int 2
  • References/Ruby on Rails/Ruby/Classes/Socket/Socket::Option

sockopt.int => integer Instance Public methods Returns the data in sockopt

2025-01-10 15:47:30
listen
  • References/Ruby on Rails/Ruby/Classes/Socket

socket.listen( int ) => 0 Instance Public methods Listens for connections

2025-01-10 15:47:30
getnameinfo
  • References/Ruby on Rails/Ruby/Classes/Socket

Socket.getnameinfo(sockaddr [, flags]) => [hostname, servicename] Class Public methods

2025-01-10 15:47:30
accept
  • References/Ruby on Rails/Ruby/Classes/Socket

socket.accept => [client_socket, client_addrinfo] Instance Public methods Accepts

2025-01-10 15:47:30
recvfrom
  • References/Ruby on Rails/Ruby/Classes/Socket

socket.recvfrom(maxlen) => [mesg, sender_addrinfo]socket.recvfrom(maxlen, flags) => [mesg, sender_addrinfo]

2025-01-10 15:47:30
udp_server_loop
  • References/Ruby on Rails/Ruby/Classes/Socket

Socket.udp_server_loop(port) {|msg, msg_src| ... }Socket.udp_server_loop(host, port) {|msg, msg_src| ... }

2025-01-10 15:47:30
tcp_server_sockets
  • References/Ruby on Rails/Ruby/Classes/Socket

tcp_server_sockets(host=nil, port) Class Public methods creates TCP/IP server

2025-01-10 15:47:30
linger 2
  • References/Ruby on Rails/Ruby/Classes/Socket/Socket::Option

sockopt.linger => [bool, seconds] Instance Public methods Returns the linger

2025-01-10 15:47:30
int
  • References/Ruby on Rails/Ruby/Classes/Socket/Socket::AncillaryData

Socket::AncillaryData.int(family, cmsg_level, cmsg_type, integer) => ancillarydata Class Public methods

2025-01-10 15:47:30
ip_address_list
  • References/Ruby on Rails/Ruby/Classes/Socket

Socket.ip_address_list => array Class Public methods Returns local IP addresses

2025-01-10 15:47:30