Let me make a pitch for TCP. UDP is a "throw the packet to the wind" protocol which provides no reliability. It finds uses in, for example, voice over IP where the odd dropped packet is acceptable. By using a TCP socket, you can still avoiding using HTTP, plus TCP stack will handle retries & etc. for you.
A TCP server which is launched by inetd is the simplest thing in the world: you write a program that reads from stdin and writes to stdout, then tell inetd to run that program whenever requests come in on a certain port. You won't have to write any network code.
Here's a simple inetd tcp server:
#!/usr/bin/ruby1.8
input = gets
puts "You said: #{input}"
Here's how to tell inetd to run it:
1024 stream tcp nowait wconrad /tmp/tcpserver.rb
This means to listen for tcp connections on port 1024. When they occur, launch /tmp/tcpserver.rb as user wconrad. man inetd.conf
for more information.
You can test it with telnet:
$ telnet localhost 1024
Trying 127.0.0.1...
Connected to ceres.
Escape character is '^]'.
Howdy!
You said: Howdy!
Connection closed by foreign host.
$
Or you can use a simple client:
$ cat /tmp/tcpclient.rb
#!/usr/bin/ruby1.8
require 'socket'
t = TCPSocket.new('localhost', 1024)
t.puts "Hello"
t.close_write
puts t.read
t.close
$ /tmp/tcpclient.rb
You said: Hello
and just to show that tcp servers in Ruby are easy:
!/usr/bin/ruby1.8
require 'socket'
def handle_session(socket)
s = socket.gets
socket.puts "You said: #{s}"
socket.close
end
server = TCPServer.new('localhost', 1024)
while (session = server.accept)
Thread.new do
handle_session(session)
end
end
With TCP being so easy, you need a compelling reason to bother with UDP.