Upgrade to Pro — share decks privately, control downloads, hide ads and more …

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

Sebastian Sogamoso

September 16, 2016
Tweet

More Decks by Sebastian Sogamoso

Other Decks in Programming

Transcript

  1. API

  2. Create an endpoint for communication Prepare a socket for incoming

    connections Assign a socket to an address socket(2) listen(2) bind(2)
  3. 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)
  4. Receive socket’s incoming stream End connection with peer socket Perform

    synchronous I/O multiplexing read(2) close(2) select(2)
  5. require ‘socket' # Create a new TCP socket socket =

    Socket.new(:INET, :STREAM, 0) Domain
  6. require ‘socket' # Create a new TCP socket socket =

    Socket.new(:INET, :STREAM, 0) Type
  7. require ‘socket' # Create a new TCP socket socket =

    Socket.new(:INET, :STREAM, 0) Protocol
  8. 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)
  9. 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
  10. 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
  11. 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
  12. 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
  13. 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
  14. 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
  15. 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
  16. 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
  17. 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
  18. 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
  19. 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
  20. 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
  21. 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
  22. 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
  23. 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
  24. 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
  25. 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
  26. 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
  27. • Buffering • Non blocking I/O • Multiplexing connections •

    Timeouts • Network architecture patterns