Skip to content

Instantly share code, notes, and snippets.

@e0da
Forked from RickCarlino/socket.rb
Created January 2, 2021 23:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save e0da/a2098b592eace26b5aa946c9dd733093 to your computer and use it in GitHub Desktop.
Save e0da/a2098b592eace26b5aa946c9dd733093 to your computer and use it in GitHub Desktop.
Bi-directional TCP socket in Ruby
#Jeepers, this standard library has everything!
require 'socket'
server = TCPServer.new "127.0.0.1" , 4000
while connection = server.accept
Thread.new(connection) do |client|
while (client_input = client.gets.chomp)
puts client_input
end
end
Thread.new(connection) do |client|
while (server_input = gets.chomp)
client.puts server_input
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment