TcpIp, Sockets, Gserver question

I’m writing a simple pair of client and server apps to demonstrate a
concept and it’s doing something weird. I will show the source code
below but basically there seems to be a syncing problem or a cache
flushing problem. When text is sent from the client to the server it
doesn’t seem to receive it in one shot, or vice versa when the server
responds the client doesn’t see it when I expect, and as a result,
things get out of sync in weird ways. Again I think it’s some kind of
cache flushing problem. I hope someone on this list has tried something
like this and might be able to help me explain what’s going on.

Thanks a lot for taking a look. I hope it’s something really simple like
some flag or setting somewhere. Thanks!!

Pito

Here’s the problem code reduced to a simple test case:

CLIENT:

require ‘socket’

tcp_socket = TCPSocket.new(‘0.0.0.0’, 8888)
tcp_socket.puts “Hi there server”
servers_resp = tcp_socket.gets.chomp
puts servers_resp

loop do
print "> "
command_string = gets
break if command_string =~ /x/
tcp_socket.puts command_string
servers_resp = tcp_socket.gets.chomp
puts servers_resp
end

SERVER:

require ‘gserver’

class MyServer < GServer
def serve(io_object)
# Increment the client ID so each client gets a unique ID
io_object.sync = true

io_object.puts("Welcome to the server")
puts "client attached."
loop do
  line = io_object.readline
  case line
  when /^t/
      io_object.puts "The time of day is #{Time.now}"
  when /^x/
    puts "Exiting!"
    break
  when /^f/
  else
    puts "received line #{line}"
    io_object.puts "What does #{line} mean, anyway?"
  end
end

end
end

puts “Starting to listen for a connection on port 8888”
server = MyServer.new(8888)
server.start
server.join

SHELL TRANSCRIPT ON SERVER SIDE:

ruby deleteme.rb
Starting to listen for a connection on port 8888
client attached.
received line Hi there server
received line lalal
received line dadad
received line wowowow
received line
received line
received line
received line
received line
received line
received line
received line
received line

AND ON THE CLIENT SIDE:
$ ruby myclient.rb
Welcome to the server

lalal
What does Hi there server
dadad
mean, anyway?
wowowow
What does lalal

mean, anyway?

What does dadad

mean, anyway?

What does wowowow

mean, anyway?

What does

mean, anyway?

What does

mean, anyway?

things get out of sync in weird ways. Again I think it’s some kind of
cache flushing problem. I hope someone on this list has tried something
like this and might be able to help me explain what’s going on.

in general if you were to a recv(1024) it might receive “half” of an
incoming string, however, doing a gets should buffer up till the next
newline and return it.

changing the client to puts servers_resp.inspect
yields this:

Welcome to the server

“What does Hi there server\n”

" mean, anyway?\n"

which may be a useful clue for ya.
GL.
-rp