Over a million developers have joined DZone.

Simple Ruby Web Service

DZone's Guide to

Simple Ruby Web Service

Free Resource
Simple ruby web service with socket


require 'socket'                				# Get sockets from stdlib
server = TCPServer.open(2000)   				# Socket to listen on port 2000
loop {                          				# Servers run forever
  Thread.start(server.accept) do |client|
    client.puts(Time.now.ctime) 				# Send the time to the client
	client.puts "Closing the connection. Bye!"
    client.close                				# Disconnect from the client


require 'socket'      					# Sockets are in standard library

hostname = 'localhost'
port = 2000
s = TCPSocket.open(hostname, port)

while line = s.gets   					# Read lines from the socket
  puts line.chop      					# And print with platform line terminator
s.close               					# Close the socket when done


Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}