Sockets

TCPServer

A TCP server socket listens on a port and waits for clients to connect.

The classic overview also shows sockets with a minimal example, and this lesson builds the full flow step by step.

require "socket" server = TCPServer.new(25110) puts server.addr.inspect

ruby tcp_server.rb

["AF_INET", 25110, "0.0.0.0", "0.0.0.0"]

Accept a client

Accept blocks until a client connects, then returns a client socket for talking to that peer.

Write one line with puts on the client socket and close it when the conversation ends. The example starts its own client in a thread, so the program accepts a real connection and ends without a second terminal.

require "socket" server = TCPServer.new("127.0.0.1", 0) port = server.addr[1] writer = Thread.new do socket = TCPSocket.new("127.0.0.1", port) puts socket.gets socket.close end client = server.accept client.puts("Hello from Ruby") client.close server.close writer.join

ruby accept_client.rb

Hello from Ruby

Bind IPv4 explicitly

In Ruby a bare port already binds every IPv4 interface, so TCPServer.new(25110) listens on 0.0.0.0. Passing an IPv6 address instead binds that interface; 0.0.0.0 pins the listener to IPv4, and naming one address binds only that interface.

require "socket" server = TCPServer.new("0.0.0.0", 25111) puts server.addr.inspect

ruby tcp_bind.rb

["AF_INET", 25111, "0.0.0.0", "0.0.0.0"]

Close cleanly

Sockets hold operating system resources, so close both the client and the server when done.

An ensure block guarantees cleanup even when accept or the conversation raises.

require "socket" server = TCPServer.new("127.0.0.1", 0) port = server.addr[1] reader = Thread.new do socket = TCPSocket.new("127.0.0.1", port) puts socket.gets socket.close end client = nil begin client = server.accept client.puts("Bye") ensure client.close if client server.close end reader.join

ruby close_cleanly.rb

Bye

Questions

A new set of questions in a new order every time. Choose one answer for each, then check.

  1. What does this program print? require "socket" server = TCPServer.new("127.0.0.1", 0) puts server.addr[0] server.close

    Answer

    AF_INET

    addr returns the address family, the port, the local address and the peer address, so element 0 is the family name.

  2. What does this program print? require "socket" server = TCPServer.new("0.0.0.0", 0) puts server.addr[2] server.close

    Answer

    0.0.0.0

    The third element of addr is the local address the server bound, which is the host that was passed.

  3. What does this program print? require "socket" a, b = UNIXSocket.pair a.puts("ping") puts b.gets a.close b.close

    Answer

    ping

    The pair is connected in both directions, so the line written on one side is read on the other.

  4. What does server.accept return?

    Answer

    A new client socket connected to the peer

    accept waits for a connection and hands back the socket that talks to that one client.

  5. What does TCPServer.new(25110) bind in Ruby?

    Answer

    Every IPv4 interface, 0.0.0.0

    The bare port already binds 0.0.0.0, and an explicit host is what narrows the listener.

  6. What does this program print? require "socket" server = TCPServer.new("127.0.0.1", 0) port = server.addr[1] Thread.new do socket = TCPSocket.new("127.0.0.1", port) socket.puts("hi") socket.close end client = server.accept puts client.gets client.close server.close

    Answer

    hi

    The client connects from a thread, accept returns its socket, and gets reads the line the client wrote.

  7. Why does the close example use an ensure block?

    Answer

    To close the sockets even when accept or the conversation raises

    ensure runs whether the body finishes or raises, which is what resource cleanup needs.

  8. What does this program print? require "socket" a, b = UNIXSocket.pair a.close begin a.puts("x") rescue => e puts e.class end b.close

    Answer

    IOError

    Writing to a closed socket raises an IOError, which is why the close calls belong in ensure.

  9. Which call starts a client connection to a server on the local machine?

    Answer

    TCPSocket.new("127.0.0.1", port)

    TCPSocket is the client side; TCPServer is the listener that accept reads from.

  10. Why close the server socket as well as the client socket?

    Answer

    The server socket holds an operating system resource and keeps the port bound

    Both are file descriptors, and the listener keeps the port until it is closed.

Article author: Arthur Isaev