Ruby - Socket Programming




This chapter gives you an understanding on most famous concept in Networking − Socket Programming.

What are Sockets?


Sockets may be implemented over a number of different channel types: Unix domain sockets, TCP, UDP, and so on. The socket provides specific classes for handling the common transports as well as a generic interface for handling the rest.

Sockets have their own vocabulary −

Sr.No. Term & Description











The identifier of a network interface −



A zero-length string, which specifies INADDR_ANY, or





A Simple Client

Here we will write a very simple client program, which will open a connection to a given port and given host. Ruby class TCPSocket provides open function to open such a socket.




require 'socket'        # Sockets are in standard library

hostname = 'localhost'
port = 2000

s =, port)

while line = s.gets     # Read lines from the socket
   puts line.chop       # And print with platform line terminator
s.close                 # Close the socket when done

A Simple Server




require 'socket'                 # Get sockets from stdlib

server =    # Socket to listen on port 2000
loop {                           # Servers run forever
   client = server.accept        # Wait for a client to connect
   client.puts(   # Send the time to the client
   client.puts "Closing the connection. Bye!"
   client.close                  # Disconnect from the client

Now, run this server in background and then run the above client to see the result.

Multi-Client TCP Servers



require 'socket'                 # Get sockets from stdlib

server =    # Socket to listen on port 2000
loop {                           # Servers run forever
   Thread.start(server.accept) do |client|
   client.puts(   # Send the time to the client
   client.puts "Closing the connection. Bye!"
   client.close                  # Disconnect from the client



A Tiny Web Browser

We can use the socket library to implement any Internet protocol. Here, for example, is a code to fetch the content of a web page −

require 'socket'
host = ''     # The web server
port = 80                           # Default HTTP port
path = "/index.htm"                 # The file we want 

# This is the HTTP request we send to fetch a file

socket =,port)  # Connect to server
socket.print(request)               # Send request
response =              # Read complete response
# Split response at first blank line into headers and body
print body                          # And display it

To implement the similar web client, you can use a pre-built library like Net::HTTP for working with HTTP. Here is the code that does the equivalent of the previous code −

require 'net/http'                  # The library we need
host = ''     # The web server
path = '/index.htm'                 # The file we want 

http =          # Create a connection
headers, body = http.get(path)      # Request the file
if headers.code == "200"            # Check the status code   
   print body                        
   puts "#{headers.code} #{headers.message}" 


Further Readings

We have given you a quick start on Socket Programming. It is a big subject, so it is recommended that you go through Ruby Socket Library and Class Methods to find more details.