TCP Socket programming in Ruby

TCP Socket programming in Ruby

As software developers, a lot of the time we’re building applications that rely on some sort of network connection. Due to Ruby’s great abstractions we take most of the network related stuff for granted. We think we know how all of that works, but do we? Let’s go over the fundamentals together, learn about how Ruby models TCP Sockets and how we can make a good use of it.

Presented at: WindyCityRails 2016

92d08794b535e41a4082c57ea547546e?s=128

Sebastian Sogamoso

September 16, 2016
Tweet

Transcript

  1. 3.
  2. 4.
  3. 5.
  4. 6.

    API

  5. 12.
  6. 13.
  7. 14.
  8. 15.
  9. 21.
  10. 25.
  11. 26.
  12. 27.
  13. 29.
  14. 30.
  15. 32.

    Create an endpoint for communication Prepare a socket for incoming

    connections Assign a socket to an address socket(2) listen(2) bind(2)
  16. 33.

    Connect to a socket in the given address Send a

    stream to the connected socket Manage new incoming connections connect(2) write(2) accept(2)
  17. 34.

    Receive socket’s incoming stream End connection with peer socket Perform

    synchronous I/O multiplexing read(2) close(2) select(2)
  18. 35.
  19. 36.
  20. 37.
  21. 38.
  22. 41.
  23. 70.
  24. 71.

    require ‘socket' # Create a new TCP socket socket =

    Socket.new(:INET, :STREAM, 0) Domain
  25. 75.

    require ‘socket' # Create a new TCP socket socket =

    Socket.new(:INET, :STREAM, 0) Type
  26. 79.

    require ‘socket' # Create a new TCP socket socket =

    Socket.new(:INET, :STREAM, 0) Protocol
  27. 82.

    require ‘socket' # Create a new TCP socket socket =

    Socket.new(:INET, :STREAM) # Create a C struct to hold the address for listening address = Socket.pack_sockaddr_in(5000,‘127.0.0.1’) socket.bind(address)
  28. 83.

    require ‘socket' # Create a new TCP socket socket =

    Socket.new(:INET, :STREAM) # Create a C struct to hold the address for listening address = Socket.pack_sockaddr_in(5000,‘127.0.0.1’) socket.bind(address) # Start listening for incoming connections socket.listen(5) client.close
  29. 84.

    require ‘socket' # Create a new TCP socket socket =

    Socket.new(:INET, :STREAM) # Create a C struct to hold the address for listening address = Socket.pack_sockaddr_in(5000,‘127.0.0.1’) socket.bind(address) # Start listening for incoming connections socket.listen(5) client.close
  30. 85.

    require ‘socket' # Create a new TCP socket socket =

    Socket.new(:INET, :STREAM) # Create a C struct to hold the address for listening address = Socket.pack_sockaddr_in(5000,‘127.0.0.1’) socket.bind(address) # Start listening for incoming connections socket.listen(Socket::SOMAXCONN) client.close
  31. 86.

    require ‘socket' # Create a new TCP socket socket =

    Socket.new(:INET, :STREAM) # Create a C struct to hold the address for listening address = Socket.pack_sockaddr_in(5000,‘127.0.0.1’) socket.bind(address) # Start listening for incoming connections socket.listen(Socket::SOMAXCONN) client.close
  32. 89.

    require ‘socket’ # Create a new TCP socket socket =

    Socket.new(:INET, :STREAM) address = Socket.pack_sockaddr_in(5000,’127.0.0.1’) # Use connect to get the server client.connect(address) client.close
  33. 90.

    require ‘socket’ # Create a new TCP socket socket =

    Socket.new(:INET, :STREAM) address = Socket.pack_sockaddr_in(5000,’127.0.0.1’) # Use connect to get the server client.connect(address) client.close
  34. 93.

    require 'socket' module WCR class Server def start @server =

    ::TCPServer.new(port) ::Socket.accept_loop(@server) do |socket| # Read from the connection until EOF request = socket.read # Process the request response = process(request) # Write back the result of the hash operation socket.write(response) socket.close end end end
  35. 94.

    require 'socket' module WCR class Server def start server =

    ::TCPServer.new(port) ::Socket.accept_loop(server) do |socket| # Read from the connection until EOF request = socket.read # Process the request response = process(request) # Write back the result of the hash operation socket.write(response) socket.close end end end
  36. 95.

    require 'socket' module WCR class Server def start server =

    ::TCPServer.new(port) ::Socket.accept_loop(server) do |socket| # Read from the connection until EOF request = socket.read # Process the request response = process(request) # Write back the result of the hash operation socket.write(response) socket.close end end end
  37. 96.

    require 'socket' module WCR class Server def start server =

    ::TCPServer.new(port) ::Socket.accept_loop(server) do |socket| # Read from the connection until EOF request = socket.read # Process the request response = process(request) # Write back the result of the hash operation socket.write(response) socket.close end end end
  38. 97.

    require 'socket' module WCR class Server def start server =

    ::TCPServer.new(port) ::Socket.accept_loop(server) do |socket| # Read from the connection until EOF request = socket.read # Process the request response = process(request) # Write back the result of the hash operation socket.write(response) socket.close end end end
  39. 98.

    require 'socket' module WCR class Server def start server =

    ::TCPServer.new(port) ::Socket.accept_loop(server) do |socket| # Read from the connection until EOF request = socket.read # Process the request response = process(request) # Write back the result of the hash operation socket.write(response) socket.close end end end
  40. 99.

    require 'socket' module WCR class Server def start server =

    ::TCPServer.new(port) ::Socket.accept_loop(server) do |socket| # Read from the connection until EOF request = socket.read # Process the request response = process(request) # Write back the result of the hash operation socket.write(response) socket.close end end end
  41. 100.

    require 'socket' module WCR class Client atrr_accessor :host, :port def

    request(string) # Create a new connection for each operation socket = ::TCPSocket.new(host, port) socket.write(string) # Send EOF after writing the request socket.close_write # Read until EOF to get the response socket.read end end end
  42. 101.

    require 'socket' module WCR class Client atrr_accessor :host, :port def

    request(string) # Create a new connection for each operation socket = ::TCPSocket.new(host, port) socket.write(string) # Send EOF after writing the request socket.close_write # Read until EOF to get the response socket.read end end end
  43. 102.

    require 'socket' module WCR class Client atrr_accessor :host, :port def

    request(string) # Create a new connection for each operation socket = ::TCPSocket.new(host, port) socket.write(string) # Send EOF after writing the request socket.close_write # Read until EOF to get the response socket.read end end end
  44. 103.

    require 'socket' module WCR class Client atrr_accessor :host, :port def

    request(string) # Create a new connection for each operation socket = ::TCPSocket.new(host, port) socket.write(string) # Send EOF after writing the request socket.close_write # Read until EOF to get the response socket.read end end end
  45. 104.

    require 'socket' module WCR class Client atrr_accessor :host, :port def

    request(string) # Create a new connection for each operation socket = ::TCPSocket.new(host, port) socket.write(string) # Send EOF after writing the request socket.close_write # Read until EOF to get the response socket.read end end end
  46. 106.

    • Buffering • Non blocking I/O • Multiplexing connections •

    Timeouts • Network architecture patterns
  47. 108.
  48. 110.