Document-class: ::Socket < BasicSocket

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.

Exception Handling

Ruby’s implementation of Socket causes an exception to be raised 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 particular exception is needed please refer to the Unix manual pages or the Windows WinSock reference.

Convenient methods

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

  • TCP client socket: Socket.tcp, TCPSocket.open

  • TCP server socket: Socket.tcp_server_loop, TCPServer.open

  • UNIX client socket: Socket.unix, UNIXSocket.open

  • UNIX server socket: Socket.unix_server_loop, UNIXServer.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.


AF_INET6 = Object.new


Show files where this class is defined (3 files)
Register or log in to add new notes.