On Wed, May 28, 2008 at 1:31 PM, Eleanor McHugh <
[email protected]> wrote:
raise ArgumentError unless @reality.responds_to? :reason
Well, as I said earlier, I found these pieces of code in a website named
games-with-brains.com, which I think you have something to do with it,
and
perhaps it has your picture.
It looks like some nice pieces of code, which I’ve been trying to make
it
work for me so I can incorporate it’s functionality on my script. I am
specifically interested in the way your code establish the connection
and
keeps it.
Today I use TCP on a simple client/server (The client issues a request
to a
remote server, which is listening on a port. The server processes the
request and sends the outcome back to the client).
However my new requirements include the need for the client/server to
exchange some handshaking. Once the initial handshaking is done, the
server
will process the requested operation and returns any output to the
client.
The code I found on the web is able to establish and keep the connection
for
what I can see. The problem is that the client only receives 1 line of
data.
I played a bit displaying the data on the server side prior to sending
it to
the client and it looks good. But, pardon the repetition, the client
only
gets 1 line.
The host file which I am using as my scape goat to send it across is
actually about 300 lines.
Attached is the code I lifted from the website:
*THE CLIENT
cat gc22.rb
#!/usr/local/ruby-1.8.6/bin/ruby -W0
require ‘tcpc.rb’
class TimeClient < TCPClient
def send
message = “#{Time.now}”
puts “sending message: #{message}”
super message
end
def on_response
line = receive
print "#{line}"
end
end
client = TimeClient.new
(1…2).each do |i|
client.connect(“localhost”, 12341955)
client.send
client.on_response
puts “==========”
client.disconnect
sleep 3
end
THE SERVER:
cat gs2.rb
#!/usr/local/ruby-1.8.6/bin/ruby -W0
require ‘gserver’
class TestServer < GServer
def serve( io )
begin
io.puts “>> WELCOME <<”
loop do
begin
telnet_input = io.gets
puts “The input: #{telnet_input}”
break if telnet_input =~ /\Aquit\b/
cmd = cat /etc/hosts
io.puts cmd
if telnet_input.chomp.include? "shutdown"
self.stop
end
io.puts eval(telnet_input)
rescue Exception
io.puts "Oops - " + $!
end
log "Rec'd " + telnet_input
end
io.puts ">> GOODBYE <<"
io.close
rescue Exception
puts "OOPS - " + $!
raise
end
end
end
ts = TestServer.new 12341955
ts.start
ts.audit = true
ts.join
THE GLUE THAT KEEPS EVERYTHING TOGETHER, I THINK. Although I don’t
understand it well.
cat tcpc.rb
#!/usr/local/ruby-1.8.6/bin/ruby -W0
require ‘socket’
EndPoint = Struct.new(:host, :port)
class TCPClient
attr_reader :remote, :status
def connect remote_host, remote_port
raise if @socket
puts "starting client"
@remote = EndPoint.new(remote_host, remote_port)
@socket = TCPSocket.new(@remote.host, @remote.port)
end
def send message
@socket.puts(message)
on_response
end
def on_response; end
def receive
raise unless @socket
begin
response = @socket.gets
end until response
response
end
def disconnect
@socket.close
@socket = nil
end
end
===============================
SAMPLE SESSION
gs2.rb
[Wed May 28 15:04:42 2008] TestServer 127.0.0.1:21187 client:38691
loopback<
127.0.0.1> connect
The input: Wed May 28 15:04:42 -0400 2008
OOPS - Broken pipe
[Wed May 28 15:04:42 2008] TestServer 127.0.0.1:21187 client:38691
disconnect
[Wed May 28 15:04:45 2008] TestServer 127.0.0.1:21187 client:38692
loopback<
127.0.0.1> connect
The input: Wed May 28 15:04:45 -0400 2008
[Wed May 28 15:04:45 2008] Rec’d Wed May 28 15:04:45 -0400 2008
The input:
OOPS - Broken pipe
[Wed May 28 15:04:45 2008] TestServer 127.0.0.1:21187 client:38692
disconnect
gc22.rb
starting client
sending message: Wed May 28 15:04:42 -0400 2008
WELCOME <<
127.0.0.1 loopback localhost # loopback (lo0) name/address
==========
starting client
sending message: Wed May 28 15:04:45 -0400 2008
WELCOME <<
127.0.0.1 loopback localhost # loopback (lo0) name/address
==========