I’m trying to make just a pass through TCP server, utilizing GServer and
some example code I am able to make something that starts up, lets me
connect multiple Clients and see what is happening. Now I want to be
able to handle data depending on the string I get, the pass should also
respond to some simple commands and return data back to the Client. But
I am getting some odd behavior on the Client when I send data, it seems
to be a step behind. What I have so far is a simple Gserver, the port I
want to be constant and ARGV[0] is basically the local hostname I want
to run on.
require ‘gserver’
port = 3472
host = ARGV[0]
class TestController < GServer
def initialize(port, host)
super(port, host)
end
def serve(io)
@data_mode = false
loop do
if IO.select([io],nil,nil,5)
data = io.readpartial(4096)
op = handle_client(data,io)
io.puts op
end
break if io.closed?
end
io.close
end
def handle_client(line,io)
if line =~ /^time/
show_time(io)
elsif line =~ /^agents/
display_agents(io)
elsif line =~ /^help/
show_help(io)
elsif line =~ /^shutdown/
io.puts "You don't want to do that Dave."
self.stop
elsif line =~ /^stop/
stop_test(io)
elsif line =~ /^HELO/
io.puts "ACK"
else
io.puts "Command not understood."
end
end
This basically checks the data string and then routes off depending on
what I am sending in from the client…either show time on the GServer,
display some help info or return an ACK if it receives a HELO; pretty
much basic stuff to understand it all. When I put some commands in I
get “Command not understood” then on the next input from the client I
get my previous command, so I see on the client:
time
Fri Mar 06 11:13:54 200
help
nil
This is the help function.
nil
agents
Command not understood.
nil
If there was a way to display agents it would be here.
nil
HELO
Command not understood.
nil
ACK
nil
Am I not flushing something right on the Client, or is this in how the
IO is being handled on the GServer?
The Client is set up to send like this:
loop do
STDOUT.print '> '
STDOUT.flush
local = STDIN.gets
break if !local
s.puts(local)
s.flush
# Print out servers response
response = s.readpartial(4096)
puts(response.chop)
end
I’m still new to this, so I apologize if its kind of basic I couldn’t
understand what is going on or find something that describes a similar
problem in the forum.
Thanks!