Networking Basics
Socket Address
A socket address combines a host and a port into an endpoint.
socket address
`Addrinfo.tcp` describes an endpoint locally; it does not connect by itself.
Socket Address
socket_address.rb
Replay: real traced execution (multi-file project)
require "socket"
port = 3000
endpoint = Addrinfo.tcp("127.0.0.1", port)
family = endpoint.afamily == Socket::AF_INET ? "ipv4" : "other"
address = endpoint.ip_address
display = "#{address}:#{endpoint.ip_port}"
puts "port=#{port}"
puts "family=#{family}"
puts "address=#{address}"
puts "display=#{display}"
require "socket"
port = 8080
endpoint = Addrinfo.tcp("127.0.0.1", port)
family = endpoint.afamily == Socket::AF_INET ? "ipv4" : "other"
address = endpoint.ip_address
display = "#{address}:#{endpoint.ip_port}"
puts "port=#{port}"
puts "family=#{family}"
puts "address=#{address}"
puts "display=#{display}"
require "socket"
port = 9292
endpoint = Addrinfo.tcp("127.0.0.1", port)
family = endpoint.afamily == Socket::AF_INET ? "ipv4" : "other"
address = endpoint.ip_address
display = "#{address}:#{endpoint.ip_port}"
puts "port=#{port}"
puts "family=#{family}"
puts "address=#{address}"
puts "display=#{display}"
port ← 3000, endpoint ← #<Addrinfo: 127.0.0.1:3000 TCP>, family ← ipv4
1require "socket"23port→ 3000 = 3000 #@port=8080, 92924endpoint→ #<Addrinfo: 127.0.0.1:3000 TCP> = Addrinfo.tcp("127.0.0.1", port)#<Addrinfo: 127.0.0.1:3000 TCP>56family→ ipv4 = endpoint.afamily2 == Socket::AF_INET2 ? "ipv4" : "other"7address→ "127.0.0.1" = endpoint.ip_address"127.0.0.1"8display→ "127.0.0.1:3000" = "#{address"127.0.0.1"}:#{endpoint.ip_port3000}"910puts "port=#{port3000}"11puts "family=#{familyipv4}"12puts "address=#{address"127.0.0.1"}"13puts "display=#{display"127.0.0.1:3000"}"outputport=3000 family=ipv4 address=127.0.0.1 display=127.0.0.1:3000
port ← 8080, endpoint ← #<Addrinfo: 127.0.0.1:8080 TCP>, family ← ipv4
1require "socket"23port→ 8080 = 80804endpoint→ #<Addrinfo: 127.0.0.1:8080 TCP> = Addrinfo.tcp("127.0.0.1", port)#<Addrinfo: 127.0.0.1:8080 TCP>56family→ ipv4 = endpoint.afamily2 == Socket::AF_INET2 ? "ipv4" : "other"7address→ "127.0.0.1" = endpoint.ip_address"127.0.0.1"8display→ "127.0.0.1:8080" = "#{address"127.0.0.1"}:#{endpoint.ip_port8080}"910puts "port=#{port8080}"11puts "family=#{familyipv4}"12puts "address=#{address"127.0.0.1"}"13puts "display=#{display"127.0.0.1:8080"}"outputport=8080 family=ipv4 address=127.0.0.1 display=127.0.0.1:8080
port ← 9292, endpoint ← #<Addrinfo: 127.0.0.1:9292 TCP>, family ← ipv4
1require "socket"23port→ 9292 = 92924endpoint→ #<Addrinfo: 127.0.0.1:9292 TCP> = Addrinfo.tcp("127.0.0.1", port)#<Addrinfo: 127.0.0.1:9292 TCP>56family→ ipv4 = endpoint.afamily2 == Socket::AF_INET2 ? "ipv4" : "other"7address→ "127.0.0.1" = endpoint.ip_address"127.0.0.1"8display→ "127.0.0.1:9292" = "#{address"127.0.0.1"}:#{endpoint.ip_port9292}"910puts "port=#{port9292}"11puts "family=#{familyipv4}"12puts "address=#{address"127.0.0.1"}"13puts "display=#{display"127.0.0.1:9292"}"outputport=9292 family=ipv4 address=127.0.0.1 display=127.0.0.1:9292