On this page
Preview Ruby 4.1 is a preview. It can change before the final release. Switch to Ruby 4.0 →
TCPSocket
TCPSocket represents a TCP/IP client socket.
A simple client may look like:
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
Class methods 1
Ruby version changes 2
-
4.0
-
Socket.tcp&TCPSocket.newaccepts anopen_timeoutkeyword argument to specify the timeout for the initial connection. [Feature #21347] -
When a user-specified timeout occurred in
TCPSocket.new, eitherErrno::ETIMEDOUTorIO::TimeoutErrorcould previously be raised depending on the situation. This behavior has been unified so thatIO::TimeoutErroris now consistently raised. (Please note that, inSocket.tcp, there are still cases whereErrno::ETIMEDOUTmay be raised in similar situations, and that in both casesErrno::ETIMEDOUTmay be raised when the timeout occurs at the OS level.)
-